运行测试时出现 npm 错误 ELIFECYCLE

26

我正在使用mocha-phantomjs进行单元测试设置。我有以下的package.json脚本来运行测试。

"scripts": {
"test": "npm run testFlickr",
"testFlickr": "mocha-phantomjs ./test/FlickrTest.html" 
}

这在浏览器中运行正常。当我在命令行中运行npm test命令时,测试运行良好,但也会出现以下错误。

3 passing (5s)
6 failing


npm ERR! flickr-test@ testFlickr: `mocha-phantomjs ./test/FlickrTest.html`
npm ERR! Exit status 6
npm ERR!
npm ERR! Failed at the flickr-test@ testFlickr script.
npm ERR! This is most likely a problem with the flickr-test package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     mocha-phantomjs ./test/FlickrTest.html
npm ERR! You can get their info via:
npm ERR!     npm owner ls flickr-test
npm ERR! There is likely additional logging output above.
npm ERR! System Windows_NT 6.1.7600
npm ERR! command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nod
ejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "testFlickr"
npm ERR! cwd C:\Users\user\Desktop\test
npm ERR! node -v v0.10.26
npm ERR! npm -v 1.4.3
npm ERR! code ELIFECYCLE
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     C:\Users\user\Desktop\test\npm-debug.log
npm ERR! not ok code 0
npm ERR! Test failed.  See above for more details.
npm ERR! not ok code 0
请问有人可以告诉我如何解决这个错误。
4个回答

30

我遇到了同样的问题,解决方法是在运行单元测试时不要使用

npm run test

改用:

npm test

22

当运行npm test时,它抑制了ELIFECYCLE错误,因此您永远不会遇到此类问题。

代码参考

一旦将测试脚本移动到不同的脚本名称,您将开始看到ELIFECYCLE错误。

我的解决方法是在命令的结尾添加exit 0。对于您的代码,它看起来像这样:

"scripts": {
  "test": "npm run testFlickr",
  "testFlickr": "mocha-phantomjs ./test/FlickrTest.html; exit 0" 
}

我遇到了这个问题,是因为我想在我的 package.json 文件中有多个测试脚本。 - Sgnl
5
在任意脚本的结尾添加;exit 0将会抑制错误。 - joemaller
@joemaller在Windows上不行。 - Cool Blue
您提供的参考资料似乎非常相关,但似乎并不适用。我将我的测试放在“test”脚本中(即不调用其他npm脚本),但仍然在唯一的问题是测试失败时出现ELIFECYCLE错误。使用npm v5.6.0 - Tom
@Tom,你在 ELIFECYCLE 错误的同时得到了什么输出?一个 gist 可能会有用。如果你可以分享你的日志,请不要忘记删除任何敏感信息。 - Sgnl

6
当我在cmd中运行npm test命令时,测试都通过了。
不是的,你有6个测试失败了。mocha-phantomjs的退出码等于失败测试的数量。直接运行mocha-phantomjs ./test/FlickrTest.html并查看哪些测试失败了。请注意,PhantomJS与您的浏览器略有不同 - 您可能需要进行调试
您的脚本设置很奇怪。您应该只有:
"scripts": {
   "test": "mocha-phantomjs ./test/FlickrTest.html"
}

然后奇数节点错误应该消失。

6

在使用npm run时遇到的问题,这与Mocha在测试失败时退出代码不等于0有关。如果您使用的是Windows,请尝试以下操作:

"scripts": {
  "test": "npm run testFlickr || ECHO.",
  "testFlickr": "mocha-phantomjs ./test/FlickrTest.html || ECHO." 
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接