【问题标题】:Mocha not failing on npm test摩卡在 npm 测试中没有失败
【发布时间】:2018-12-03 19:08:14
【问题描述】:

您好,我在让 mocha 测试项目正常工作时遇到了一些问题。我正在使用 Visual Studio Code。

当我调试以下 Mocha 代码时,我可以看到期望子句中的两个 ownerid 值不匹配,并且跨过我的期望行会触发 emitPendingUnhandledRejections()。

不幸的是,如果我单独进行 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"
 }
}

【问题讨论】:

  • 您希望它们匹配还是不匹配?您的问题暗示您希望它们匹配,但是您正在测试它们是否匹配。除此之外,你正在做Promise 反模式。嵌套的then 应该连接到外部的then,而不是返回。
  • 抱歉 - 已调整。关于反模式,如果我正确链接,ownerdata 总是未定义的。

标签: visual-studio-code mocha.js chai


【解决方案1】:

您应该通过返回承诺让 mocha 等待异步任务完成。

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);
      })
    })
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-16
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多