简体   繁体   中英

Running multiple commands for npm test

I'm currently using a gulp task to test a project. This runs tasks using the following tools:

  • Karma (async)
  • Protractor (spawned process)
  • ESlint (using gulp-eslint )
  • HTMLHint (using gulp-htmlhint )
  • Stylelint (using gulp-postcss )

The task fails if any of these tasks failed.

All of these tools have perfectly fine cli interfaces. So I decided I'd like to run these tools using an npm test script instead.

For simplicitly let's say all tools run by simply invoking them without any flags. Then this can be done using:

{
  ...
  "scripts": {
    "test": "karma && protractor && eslint && htmlhint && stylelint"
  },
  ...
}

However, this means that if karma fails, none of the other tools will run.

Is it possible to create a setup where all of these tools will run, but npm test will fail if any of the commands failed?

The scripts tags in package.json are run by your shell, so you can run the command that you want the shell to run:

"scripts": {
  "test": "karma ; protractor ; eslint ; htmlhint ; stylelint"
},

Will run all commands if you have a unix/OSX shell.

To be able to retain the exit_code like you specify you need to have a separate script to run the commands. Maybe something like this:

#!/bin/bash

EXIT_STATUS=0

function check_command {
    "$@"
    local STATUS=$?
    if [ $STATUS -ne 0 ]; then
        echo "error with $1 ($STATUS)" >&2
        EXIT_STATUS=$STATUS
    fi
}

check_command karma
check_command protractor
check_command eslint
check_command htmlhint
check_command stylelint
exit $EXIT_STATUS

npm-run-all can also handle this well

You can run multiple npm commands concurrently, continuing on error as follows:

npm-run-all --parallel --continue-on-error karma protractor eslint htmlhint stylelint

Options as written in the documentation:

-p, --parallel <tasks>   - Run a group of tasks in parallel.
                           e.g. 'npm-run-all -p foo bar' is similar to
                                'npm run foo & npm run bar'.
-c, --continue-on-error  - Set the flag to continue executing other tasks
                           even if a task threw an error. 'run-p' itself
                           will exit with non-zero code if one or more tasks
                           threw error(s).

concurrently is a nice library that can handle this. It can be installed from npm.

npm install --save-dev concurrently

When each section is sorted as a separate script, the scripts section of package.json looks a bit like this:

{
  ...
  "scripts": {
    "test": "concurrently 'npm run karma' 'npm run protractor' 'npm run eslint' 'npm run htmlhint' 'npm run stylelint'",
    "karma": "karma start --single-run",
    "protractor": "protractor",
    "eslint": "eslint .",
    "htmlhint": "htmlhint 'app/**/*.html'",
    "stylelint": "stylelint 'app/**/*.css'",
  },
  ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM