19

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?

3 Answers 3

14

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
Sign up to request clarification or add additional context in comments.

5 Comments

This will run all commands, but it will exit with the exist code of the last command, ignoring any previous exit codes. So in this case any failing tests will be ignored.
I've added an example of that now.
Note that the test script in the first example won't support Windows, the ; syntax is invalid in PowerShell.
sh: 1: Syntax error: ";" unexpected and this sucks ;__;
Under powershell, rather than &&, use &.
11

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).

1 Comment

I actually prefer this one over concurrently nowadays.
7

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'",
  },
  ...
}

1 Comment

"test": "concurrently 'npm run karma' 'npm run protractor' 'npm run eslint' 'npm run htmlhint' 'npm run stylelint'" should be "test": "concurrently \"npm run karma\" \"npm run protractor\" \"npm run eslint\" \"npm run htmlhint\" \"npm run stylelint\"" on windows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.