运行testing时npm错误ELIFECYCLE

我正在使用mocha-phantomjs设置进行unit testing。 我有以下package.json脚本运行testing。

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

这在浏览器中运行正常。 当我运行cmd命令npm test时,testing运行正常,但它也给出了以下错误

 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 

请谁能告诉我如何解决这个错误。

当我运行cmd命令npm test时,testing运行正常

不,他们不是。 你有6个失败的testing。 mocha-phantomjs的退出代码等于失败的testing次数。 直接运行mocha-phantomjs ./test/FlickrTest.html ,看看有什么失败。 实现PhantomJS与浏览器有点不同 – 你可能需要debugging它 。

你的脚本设置很奇怪。 你应该只有:

 "scripts": { "test": "mocha-phantomjs ./test/FlickrTest.html" } 

那么奇怪的节点错误应该消失。

当运行npm test它会压制ELIFECYCLE错误,所以你永远不会遇到这种经验。

参考代码

将testing脚本移动到其他脚本名称后,您将看到ELIFECYCLE错误。

我的修复是添加一个exit 0到命令的末尾。 对于你的代码,它看起来像这样:

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

当使用npm run ,这是一个问题,只要testing失败,它必须与Mocha用代码!== 0退出。 如果你在Windows上试试这个:

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