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

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

问题描述:

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

Is there a way to trigger npm's posttest when test fails? If package.json contains

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

$ npm test 会回显pretest"而不是posttest".

$ npm test will echo "pretest" but not "posttest".

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

I get the same behavior if I use mocha and a test fails, even if mocha doesn't trigger any exceptions (just some simple failure assert(true==false)).

我在预测试中启动一个资源,我想在后测试中终止该资源,无论测试本身通过还是失败.

I'm launching a resource on pretest, and I'd like to kill the resource on posttest, whether the test itself passes or fails.

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

MacOS OS X 10.9.4, npm version 1.4.21, node v0.10.30.

解决了.不确定以下是否适用于 Windows.bash || 运算符后跟空注释 : 更改退出代码.

Worked it out. Not sure if the following will work in Windows. The bash || operator followed by an empty comment : changes the exit code.

例如,使用摩卡咖啡:

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