找出断言失败时我的测试崩溃的原因。
我有一个非常简单的NodeJs应用程序,我使用Mocha用于BDD而没有断言框架(只是来自NodeJs的基本断言)。
我使用npm test
运行我的Mocha测试,我有以下package.json文件:
{
"name": "server",
"version": "1.0.0",
"description": "Mah Project!",
"main": "index.js",
"scripts": {
"test": "mocha test.js",
"test-kitten": "mocha -R nyan test.js",
"watch": "gulp watch",
"start": "node server.js"
},
"repository": {
"type": "git",
"url": ""
},
"keywords": [
"awesome"
],
"author": "Rick and Morty",
"license": "ISC",
"homepage": "",
"dependencies": {
"express": "^4.14.0",
"mongodb": "^2.2.6",
"underscore": "^1.8.3"
},
"devDependencies": {
"gulp": "^3.9.1",
"gulp-mocha": "^3.0.1",
"mocha": "^3.0.2",
"superagent": "^2.2.0"
}
}
为了尝试Mocha,我创建了一个非常简单的测试:
"use strict";
let assert = require("assert");
describe("server", function(){
it("prints out 'Hello, world'", function(){
assert.equal('A', 'B');
});
});
当我有assert.equal('A', 'A');
时,测试通过,一切都很好。
然而,当我有assert.equal('A', 'B');
时,测试失败(按预期),但应用程序也崩溃了!
根据我关注的教程,这不应该崩溃:
它还会创建一个调试文件:
0 info it worked if it ends with ok
1 verbose cli [ '/home/ubuntu/.nvm/versions/node/v4.4.5/bin/node',
1 verbose cli '/home/ubuntu/.nvm/versions/node/v4.4.5/bin/npm',
1 verbose cli 'run',
1 verbose cli 'test' ]
2 info using npm@2.15.5
3 info using node@v4.4.5
4 verbose run-script [ 'pretest', 'test', 'posttest' ]
5 info pretest server@1.0.0
6 info test server@1.0.0
7 verbose unsafe-perm in lifecycle true
8 info server@1.0.0 Failed to exec test script
9 verbose stack Error: server@1.0.0 test: `mocha test.js`
9 verbose stack Exit status 1
9 verbose stack at EventEmitter.<anonymous> (/home/ubuntu/.nvm/versions/node/v4.4.5/lib/node_modules/npm/lib/utils/lifecycle.js:217:16)
9 verbose stack at emitTwo (events.js:87:13)
9 verbose stack at EventEmitter.emit (events.js:172:7)
9 verbose stack at ChildProcess.<anonymous> (/home/ubuntu/.nvm/versions/node/v4.4.5/lib/node_modules/npm/lib/utils/spawn.js:24:14)
9 verbose stack at emitTwo (events.js:87:13)
9 verbose stack at ChildProcess.emit (events.js:172:7)
9 verbose stack at maybeClose (internal/child_process.js:827:16)
9 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)
10 verbose pkgid server@1.0.0
11 verbose cwd /home/ubuntu/workspace/server
12 error Linux 4.2.0-c9
13 error argv "/home/ubuntu/.nvm/versions/node/v4.4.5/bin/node" "/home/ubuntu/.nvm/versions/node/v4.4.5/bin/npm" "run" "test"
14 error node v4.4.5
15 error npm v2.15.5
16 error code ELIFECYCLE
17 error server@1.0.0 test: `mocha test.js`
17 error Exit status 1
18 error Failed at the server@1.0.0 test script 'mocha test.js'.
18 error This is most likely a problem with the server package,
18 error not with npm itself.
18 error Tell the author that this fails on your system:
18 error mocha test.js
18 error You can get information on how to open an issue for this project with:
18 error npm bugs server
18 error Or if that isn't available, you can get their info via:
18 error
18 error npm owner ls server
18 error There is likely additional logging output above.
19 verbose exit [ 1, true ]
我尝试通过
来纠正这个问题"dependencies": {
"express": "^4.14.0",
"mongodb": "^2.2.6",
"underscore": "^1.8.3",
"gulp": "^3.9.1",
"gulp-mocha": "^3.0.1",
"mocha": "^3.0.2",
"superagent": "^2.2.0"
}
并从我的package.json中完全删除 devDependencies
对象,但这不是一个真正的解决方案,只是针对该问题的一个微薄的解决方法。如果我有一个仅用于开发环境的软件包和工具,我应该可以单独使用和安装它们!
即使在上一个试验性问题之后,问题仍然存在。
答案 0 :(得分:6)
这是npm run
的预期行为。
经过长时间的搜索,我发现了一种以正确方式引导我的威胁:
这里发生的是,mocha返回失败测试的数量。因此,如果所有测试都通过,则mocha返回0并且npm继续而不会抛出异常。
但是,如果X测试失败,则mocha将返回X.因为运行脚本的返回结果与0不同,所以npm将使用代码X引发异常并且将无法执行。
就个人而言,这种行为是废话。我不知道在这里应该责怪谁,如果mocha在任意数量的测试失败时返回任意数字,npm拒绝除0之外的任何东西,或者我,因为显然不知道如何运行测试,或者至少跟不上这些技术....
现在我需要继续寻找一种使用mocha和npm运行测试的好方法。希望这有助于某人。