简体   繁体   English

为npm测试运行多个命令

[英]Running multiple commands for npm test

I'm currently using a gulp task to test a project. 我目前正在使用gulp任务来测试项目。 This runs tasks using the following tools: 这使用以下工具运行任务:

  • Karma (async) 业力(异步)
  • Protractor (spawned process) 量角器(衍生过程)
  • ESlint (using gulp-eslint ) ESlint(使用gulp-eslint
  • HTMLHint (using gulp-htmlhint ) HTMLHint(使用gulp-htmlhint
  • Stylelint (using gulp-postcss ) Stylelint(使用gulp-postcss

The task fails if any of these tasks failed. 如果任何这些任务失败,任务将失败。

All of these tools have perfectly fine cli interfaces. 所有这些工具都具有完美的cli接口。 So I decided I'd like to run these tools using an npm test script instead. 所以我决定使用npm测试脚本来运行这些工具。

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. 但是,这意味着如果karma失败,其他任何工具都不会运行。

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? 是否可以创建一个所有这些工具都可以运行的设置,但是如果任何命令失败, npm test将会失败?

The scripts tags in package.json are run by your shell, so you can run the command that you want the shell to run: package.json中的scripts标记由shell运行,因此您可以运行希望shell运行的命令:

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

Will run all commands if you have a unix/OSX shell. 如果你有一个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. 为了能够像你指定的那样保留exit_code,你需要有一个单独的脚本来运行命令。 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 npm-run-all也可以很好地处理这个问题

You can run multiple npm commands concurrently, continuing on error as follows: 您可以同时运行多个npm命令,继续出现错误,如下所示:

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安装。

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: 当每个部分作为单独的脚本排序时, package.json的脚本部分看起来有点像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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