start-server-and-test

授权协议 Readme
开发语言 JavaScript
所属分类 应用工具、 终端/远程登录
软件类型 开源软件
地区 不详
投 递 者 罗浩然
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

start-server-and-test

Starts server, waits for URL, then runs test command; when the tests end, shuts down server

NPM

Build status

Install

Requires Node version 8.9 or above.

npm install --save-dev start-server-and-test

Use

This command is meant to be used with NPM script commands. If you have a "start server", and "test" script names for example, you can start the server, wait for a url to respond, then run tests. When the test process exits, the server is shut down.

{
    "scripts": {
        "start-server": "npm start",
        "test": "mocha e2e-spec.js",
        "ci": "start-server-and-test start-server http://localhost:8080 test"
    }
}

To execute all tests simply run npm run ci.

Commands

In addition to using NPM script names, you can pass entire commands (surround them with quotes so it is still a single string) that will be executed "as is". For example, to start globally installed http-server before running and recording Cypress.io tests you can use

# run http-server, then when port 8000 responds run Cypress tests
start-server-and-test 'http-server -c-1 --silent' 8000 './node_modules/.bin/cypress run --record'

Because npm scripts execute with ./node_modules/.bin in the $PATH, you can mix global and locally installed tools when using commands inside package.json file. For example, if you want to run a single spec file:

{
  "scripts": {
    "ci": "start-server-and-test 'http-server -c-1 --silent' 8080 'cypress run --spec cypress/integration/location.spec.js'"
  }
}

Or you can move http-server part into its own start script, which is used by default and have the equivalent JSON

{
  "scripts": {
    "start": "http-server -c-1 --silent",
    "ci": "start-server-and-test 8080 'cypress run --spec cypress/integration/location.spec.js'"
  }
}

Here is another example that uses Mocha

{
  "scripts": {
    "ci": "start-server-and-test 'http-server -c-1 --silent' 8080 'mocha e2e-spec.js'"
  }
}

Alias

You can use either start-server-and-test, server-test or start-test commands in your scripts.

You can use : in front of port number like server-test :8080, so all these are equivalent

start-server-and-test start http://localhost:8080 test
server-test start http://localhost:8080 test
server-test http://localhost:8080 test
start-test :8080 test
start-test 8080 test
start-test 8080

Options

If you use convention and name your scripts "start" and "test" you can simply provide URL

{
    "scripts": {
        "start": "npm start",
        "test": "mocha e2e-spec.js",
        "ci": "start-server-and-test http://localhost:8080"
    }
}

You can also shorten local url to just port, the code below is equivalent to checking http://localhost:8080.

{
    "scripts": {
        "start": "npm start",
        "test": "mocha e2e-spec.js",
        "ci": "server-test 8080"
    }
}

You can provide first start command, port (or url) and implicit test command

{
    "scripts": {
        "start-it": "npm start",
        "test": "mocha e2e-spec.js",
        "ci": "server-test start-it 8080"
    }
}

You can provide port number and custom test command, in that case npm start is assumed to start the server.

{
    "scripts": {
        "start": "npm start",
        "test-it": "mocha e2e-spec.js",
        "ci": "server-test :9000 test-it"
    }
}

You can provide multiple resources to wait on, separated by a pipe |. (be sure to wrap in quotes)

{
  "scripts": {
    "start": "npm start",
    "test-it": "mocha e2e-spec.js",
    "ci": "server-test \"8080|http://foo.com\""
  }
}

or for multiple ports simply: server-test '8000|9000' test.

If you want to start the server, wait for it to respond, and then run multiple test commands (and stop the server after they finish), you should be able to use && to separate the test commands:

{
  "scripts": {
    "start": "npm start",
    "test:unit": "mocha test.js",
    "test:e2e": "mocha e2e.js",
    "ci": "start-test 9000 'npm run test:unit && npm run test:e2e'"
  }
}

The above script ci after the localhost:9000 responds executes the npm run test:unit command. Then when it finishes it runs npm run test:e2e. If the first or second command fails, the ci script fails. Of course, your mileage on Windows might vary.

npx and yarn

If you have npx available, you can execute locally installed tools from the shell. For example, if the package.json has the following local tools:

{
  "devDependencies": {
    "cypress": "3.2.0",
    "http-server": "0.11.1",
    "start-server-and-test": "1.9.0"
  }
}

Then you can execute tests simply:

$ npx start-test 'http-server -c-1 .' 8080 'cypress run'
starting server using command "http-server -c-1 ."
and when url "http://localhost:8080" is responding
running tests using command "cypress run"
Starting up http-server, serving .
...

Similarly, you can use yarn to call locally installed tools

$ yarn start-test 'http-server -c-1 .' 8080 'cypress run'
yarn run v1.13.0
$ /private/tmp/test-t/node_modules/.bin/start-test 'http-server -c-1 .' 8080 'cypress run'
starting server using command "http-server -c-1 ."
and when url "http://localhost:8080" is responding
running tests using command "cypress run"
Starting up http-server, serving .
...

Note for yarn users

By default, npm is used to run scripts, however you can specify that yarn is used as follows:

"scripts": {
    "start-server": "yarn start",
    "test": "mocha e2e-spec.js",
    "ci": "start-server-and-test 'yarn start-server' http://localhost:8080 'yarn test'"
}

Note for webpack-dev-server users

Also applies to Vite users!

If you are using webpack-dev-server (directly or via angular/cli or other boilerplates) then the server does not respond to HEAD requests from start-server-and-test. You can check if the server responds to the HEAD requests by starting the server and pinging it from another terminal using curl

# from the first terminal start the server
$ npm start
# from the second terminal call the server with HEAD request
$ curl --head http://localhost:3000

If the server responds with 404, then it does not handle the HEAD requests. You have two solutions:

Use HTTP GET requests

You can force the start-server-and-test to ping the server using GET requests using the http-get:// prefix:

start-server-and-test http-get://localhost:8080

Ping a specific resource

As an alternative to using GET method to request the root page, you can try pinging a specific resource, see the discussion in the issue #4.

# maybe the server responds to HEAD requests to the HTML page
start-server-and-test http://localhost:3000/index.html
# or maybe the server responds to HEAD requests to JS resource
start-server-and-test http://localhost:8080/app.js

Explanation

You can watch the explanation in the video Debug a Problem in start-server-and-test.

Under the hood this module uses wait-on to ping the server. Wait-on uses HEAD by default, but webpack-dev-server does not respond to HEAD only to GET requests. Thus you need to use http-get:// URL format to force wait-on to use GET probe or ask for a particular resource.

Debugging

To see diagnostic messages, run with environment variable DEBUG=start-server-and-test

$ DEBUG=start-server-and-test npm run test
  start-server-and-test parsing CLI arguments: [ 'dev', '3000', 'subtask' ] +0ms
  start-server-and-test parsed args: { services: [ { start: 'npm run dev', url: [Array] } ], test: 'npm run subtask' }
...
making HTTP(S) head request to  url:http://localhost:3000 ...
  HTTP(S) error for http://localhost:3000 Error: Request failed with status code 404

Disable HTTPS certificate checks

To disable HTTPS checks for wait-on, run with environment variable START_SERVER_AND_TEST_INSECURE=1.

Timeout

This utility will wait for maximum of 5 minutes while checking for the server to respond (default). Setting an environment variable WAIT_ON_TIMEOUT=600000 (milliseconds) sets the timeout for example to 10 minutes.

Interval

This utility will check for a server response every two seconds (default). Setting an environment variable WAIT_ON_INTERVAL=600000 (milliseconds) sets the interval for example to 10 minutes.

Starting two servers

Sometimes you need to start one API server and one webserver in order to test the application. Use the syntax:

start-test <first command> <first resource> <second command> <second resource> <test command>

For example if API runs at port 3000 and server runs at port 8080:

{
  "scripts": {
    "test": "node src/test",
    "start:api": "node src/api",
    "start:server": "node src/server",
    "test:all": "start-test start:api 3000 start:server 8080 test"
  }
}

In the above example you would run npm run test:all to start the API first, then when it responds, start the server, and when the server is responding, it would run the tests. After the tests finish, it will shut down both servers. See the repo start-two-servers-example for full example

Note for Apollo Server users

When passing a simple GET request to Apollo Server it will respond with a 405 error. To get around this problem you need to pass a valid GraphQL query into the query parameter. Passing in a basic schema introspection query will work to determine the presence of an Apollo Server. You can configure your npm script like so:

{
  "scripts": {
    "ci": "start-server-and-test start 'http-get://localhost:4000/graphql?query={ __schema { queryType { name } } }' test"
  }
}

Small print

Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2017

License: MIT - do anything with the code, but don't blame me if it does not work.

Support: if you find any problems with this module, email / tweet /open issue on Github

MIT License

Copyright (c) 2017 Gleb Bahmutov <gleb.bahmutov@gmail.com>

Permission is hereby granted, free of charge, to any personobtaining a copy of this software and associated documentationfiles (the "Software"), to deal in the Software withoutrestriction, including without limitation the rights to use,copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom theSoftware is furnished to do so, subject to the followingconditions:

The above copyright notice and this permission notice shall beincluded in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIESOF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE ANDNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHTHOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISINGFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OROTHER DEALINGS IN THE SOFTWARE.

  • 使用adb时,经常遇到adb不能使用的情况,大多情况是因为电脑本身安装了手机助手之类的(例如,应用宝,豌豆荚,百度手机助手等) adb包含文件 那些手机助手通常也会包含AdbWinApi.dll文件,这就造成了启用adb时端口会被占用。 首先我们可以使用 adb nodaemon server 查看当前adb端口号 C:\Users\di.qian>adb nodaemon server nets

  • 1. start node manage(%wl_home%/common/bin/startManagedWebLogic) 2. start wlst(%wl_home%/common/bin/wlst) 3. in wlst: start('serverName','Server')

  • 在学Struts2时,遇到Tomcat报错:Server Tomcat v8.5 Server at localhost failed to start. console台部分报错信息: Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].Sta

  • 问题如下 执行 service rabbitmq-server start 出现Starting rabbitmq-server: FAILED - check /var/log/rabbitmq/startup_{log, _err} rabbitmq-server. 具体如下 [root@backbay2 rabbitmq]# service rabbitmq-server start St

  • The Test Management Process   Test management with TestDirector involves four phases:   Specify Requirements: Analyze your application and determine your testing requirements.   Plan Tests: Create a t

  • kube-apiserver.service - Kubernetes API Server Loaded: loaded (/usr/lib/systemd/system/kube-apiserver.service; enabled; vendor preset: disabled) Active: failed (Result: exit-code) since Thu 2020-11-19

  • 服务器如何重起 都不成功, systemout 连日志都没有 只在native_stdout 里见到一行错误 FATAL ERROR in native method:JDWP No transports initialized, jvmtiError=JVMTI_ERROR_INTERNAL(113) 重起 整个websphere 都没有用,google很多都说是jvm自己的错或者localho

  • 查看日志文件 cat /var/log/neutron/server.log | grep -i error 2023-01-06 12:17:46.385 22236 ERROR neutron.service [-] Unrecoverable error: please check log for details.: MissingRequiredOptions: Auth plugin

  • 源码下载(免下载积分):下载 当创建一个可以绑定的Service时,必须提供一个IBinder用于与客户端进行通信,有三种方式可以定义IBinder,  对于直接继承Binder类已经测试了,那怎么测试使用一个Messenger来提供IBinder的类呢?可以这样: 1. 继承ServiceTestCase public class BindService_MessengerTest

  • 问题: [root@localhost ~]# systemctl status neutron-server ● neutron-server.service - OpenStack Neutron Server Loaded: loaded (/usr/lib/systemd/system/neutron-server.service; disabled; vendor preset: dis

  • 严重: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].S

  • 解决方法(先不要关闭安装弹出的错误窗口): 1 运行:services.msc,打开服务管理器,找到“VisualSVNServer”。 2 在服务上,点击右键--属性,打开“登录”选项卡。将“此帐户”改成“本地系统帐户”。再回到安装程序弹出窗口处,选择"Retry"即可。

  • Automatically start VNC server on startup   If you want a moredynamic configuration and the ability to connect for multiple users then there isa better way to do this. As root create the file (and dir

 相关资料
  • This section is for those looking to implement their own ABCI Server, perhaps in a new programming language. You are expected to have read ABCI Methods and Types and ABCI Applications. Message Protoco

  • This section is for those looking to implement their own ABCI Server, perhaps in a new programming language. You are expected to have read ABCI Methods and Types and ABCI Applications. Message Protoco

  • Follow these steps to install Handsontable: Install Create Initialize Alternative installation Step 1: Install There are many ways to install Handsontable, but we suggest using npm. Just type in the f

  • TinyMCE 5.0 is a powerful and flexible rich text editor that can be embedded in the user’s web application. TinyMCE 5.0 is perfect for developers who want to see how the new version of TinyMCE integra

  • Preparation 那种一点都不环保的100多M的傻瓜安装包不会再有了。你首先需要的是下载依赖包时的一点点耐心。 Install JDK 6.0+ and set the JAVA_HOME. Install Maven 3.0.3+ and set the PATH. (如果机器不能直接上网, 请参照https://www.wenjiangs.com/doc/odsuk8sj章节设置代理或私

  • In this chapter, we will introduce the environment requirements, how to install Gio, a quick start to your first Gio globe(the 3D globe for data visualization), and explanation of the last document to