Milambardo
Milambardo

Reputation: 330

Mocha not failing on npm test

Hi I am having some issues with getting my mocha test project to work properly. I am using Visual Studio Code.

When I debug the following Mocha code, I can see that the two ownerid values do not match in the expect clause, and that stepping over my expect line fires emitPendingUnhandledRejections().

Unfortunately, if I npm test separately, all tests pass, where I am expecting on fail. Why is this?

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

Here is my 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"
 }
}

Upvotes: 0

Views: 77

Answers (1)

abskmj
abskmj

Reputation: 758

You should let mocha to wait for the async task to finish by returning the 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);
      })
    })
});

Upvotes: 1

Related Questions