嗨,我在使我的摩卡测试项目正常工作时遇到一些问题。我正在使用Visual Studio代码。
当我调试下面的Mocha代码时,我可以看到Expect子句中的两个ownerid值不匹配,并且超出我的期望行会触发sparkPendingUnhandledRejections()。
不幸的是,如果我单独进行npm测试,则所有测试都将通过,而我预期会失败。为什么会这样?
it('Get Owner should be all match', () => {
let ownerdata: any;
helper.createbasicowner()
.then((ownerdata: any) => {
return chai.request(app).post('/GetOwnerByID').send({
ownerid: ownerdata.ownerid
}).then((odata: any) => {
expect(odata.body.ownerid).to.not.eql(ownerdata.ownerid);
})
})
});
这是我的package.json:
{
"name": "d",
"version": "1.0.0",
"description": "webservices for ",
"main": "index.js",
"scripts": {
"test": "mocha --reporter spec --compilers ts:ts-node/register test/**/*.test.ts",
"start": "node dist/index.js"
},
"author": "Wilbur",
"license": "ISC",
"dependencies": {
"@types/chai-http": "^3.0.5",
"@types/express": "^4.16.0",
"@types/mocha": "^5.2.5",
"@types/node": "^10.9.4",
"@types/pg-promise": "^5.4.3",
"body-parser": "^1.18.3",
"chai": "^4.1.2",
"chai-http": "^4.2.0",
"express": "^4.16.3",
"mocha": "^5.2.0",
"morgan": "^1.9.0",
"ts-node": "^7.0.1",
"typescript": "^3.0.3"
}
}
答案 0 :(得分:1)
您应该让Mocha通过返回Prom来等待异步任务完成。
it('Get Owner should be all match', () => {
let ownerdata: any;
return helper.createbasicowner()
.then((ownerdata: any) => {
return chai.request(app).post('/GetOwnerByID').send({
ownerid: ownerdata.ownerid
}).then((odata: any) => {
expect(odata.body.ownerid).to.not.eql(ownerdata.ownerid);
})
})
});