Visual studio code 摩卡咖啡未通过npm测试

Visual studio code 摩卡咖啡未通过npm测试,visual-studio-code,mocha.js,chai,Visual Studio Code,Mocha.js,Chai,嗨,我有一些问题,让我的摩卡测试项目正常工作。我正在使用VisualStudio代码 当我调试下面的Mocha代码时,我可以看到expect子句中的两个ownerid值不匹配,跨过expect行激发emitPendingUnhandledRejections() 不幸的是,如果我单独进行npm测试,那么所有的测试都会通过,而我预期会失败。为什么会这样 it('Get Owner should be all match', () => { let ownerdata: any; h

嗨,我有一些问题,让我的摩卡测试项目正常工作。我正在使用VisualStudio代码

当我调试下面的Mocha代码时,我可以看到expect子句中的两个ownerid值不匹配,跨过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等待异步任务完成

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

你希望他们匹配还是不匹配?您的问题意味着您希望它们匹配,但是如果它们不匹配,您将进行测试。除此之外,您正在执行
承诺
反模式。嵌套的
then
应连接到外部
then
,而不是返回。抱歉-已调整。关于反模式,如果我正确链接,ownerdata总是未定义的。