This site uses cookies for analytics. By continuing to browse this site, you agree to this use.

Test a local server

Test a local server

In this scenario you want to run webhint against a local server that is running the code you want to test. Usually the tasks to perform can be reduced to:

  1. Start server
  2. Run webhint

A solution to automate this is via npm scripts and the test task. The biggest concern is how to start both tasks and kill the other when one ends in a multiplatform environment. For example, the following will not work because npm start will start the server, it will not stop (it’ll be waiting for incoming requests), and thus never executing hint http://localhost:8080:

{
    ...
    "scripts": {
        ...
        "start": "start your web server somehow",
        "test": "npm start && hint http://localhost:8080",
        ...
    }
    ...
}

While there are some solutions like using just & or |, these don’t work in all platforms (nor is recommended to pipe the output). To solve this issue, you will need to use a package that can start multiple npm tasks simultaneously and coordinate them. npm-run-all is one of those.

As an example, the following npm script will build the project, start http-server (assuming the output of the build process is on the dist folder), run webhint on localhost and once it ends, kill the server started by http-server:

{
    ...
    "scripts": {
        ...
        "start": "http-server dist -s -g",
        "hint": "hint http://localhost:8080",
        "test": "npm build && npm-run-all -r -p http-server hint",
        ...
    }
    ...
}