如果npm测试失败,npm posttest不会触发

时间:2014-08-13 17:18:39

标签: node.js testing npm

当测试失败时,有没有办法触发npm的后测?如果package.json包含

"scripts": {
  "pretest": "echo pretest",
  "test": "some_failed_test_or_error",
  "posttest": "echo posttest"
}

$ npm test将回应" pretest"但不是" posttest"。

如果我使用mocha并且测试失败,我会得到相同的行为,即使mocha没有触发任何异常(只是一些简单的失败assert(true==false))。

我在预测试中启动资源,并且我想在测试后杀死资源,无论测试本身是通过还是失败。

MacOS OS X 10.9.4,npm版本1.4.21,节点v0.10.30。

2 个答案:

答案 0 :(得分:11)

解决了这个问题。不确定以下内容是否适用于Windows。 bash ||运算符后跟空注释:会更改退出代码。

例如,使用mocha:

"scripts": {
  "pretest": "echo pretest",
  "test": "mocha || :",
  "posttest": "echo posttest"
}

答案 1 :(得分:2)

该问题的另一个答案是简洁明了,但有点不合常规。我知道,如果我使用它,每次回来并看到“ ||:”时,都会在脑海中形成一个大问号。非常简洁,您甚至可能会错过它。它需要在某个地方提供文档。

我花了一些时间寻找更正统的解决方案,并在这里找到它:https://github.com/npm/npm/issues/5493

您需要npm-run-all

"test": "npm-run-all the-actual-test run-after-test-even-if-failed --continue-on-error"

但是,您不能对前置脚本和后置脚本使用“ pretest”或“ posttest”名称,因为它们会运行两次。