摩卡在 npm 测试中没有失败

Mocha not failing on npm test

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

当我调试以下 Mocha 代码时,我可以看到两个 ownerid 值在 expect 子句中不匹配,跨过我的 expect 行会触发 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"
 }
}

你应该让 mocha 通过返回 promise 来等待异步任务完成。

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