mocha chai passes the test even if result is not as expected

57 Views Asked by At

I use mocha, chai, express to test REST API response the response status is 201 and I expect to fail the test

it('janus post', () => {
  request('https://***')
    .post('/***')
    .attach('file', 'T:/***/Test1.docx')
    .end(function(err, res) {
      if (err) throw err;
      expect(res.status).to.be.equal(200)
      console.log(res.body);           
  });
});

the test is passing and the result is:

     First test

        ✔ janus post


      1 passing (399ms)

    C:\projects\testacademyapi\node_modules\mocha\lib\runner.js:950
        throw err;
        ^
    AssertionError: expected 201 to equal 200
        at Test.<anonymous> (C:\projects\apitests\tests\webApi.spec.js:28:38)
        at Test.assert (C:\projects\apitests\node_modules\supertest\lib\test.js:172:8)
        at localAssert (C:\projects\apitests\node_modules\supertest\lib\test.js:120:14)
        at C:\projects\apitests\node_modules\supertest\lib\test.js:125:7
        at Request.callback (C:\projects\apitests\node_modules\superagent\lib\node\index.js:857:12)
        at C:\projects\apitests\node_modules\superagent\lib\node\index.js:1070:18
        at IncomingMessage.<anonymous>    (C:\projects\apitests\node_modules\superagent\lib\node\parsers\json.js:21:7)
        at IncomingMessage.emit (node:events:526:35)
        at endReadableNT (node:internal/streams/readable:1359:12)
        at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
      showDiff: true,
      actual: 201,
      expected: 200,
      operator: 'strictEqual'
    }

I expect fail of the test.

1

There are 1 best solutions below

1
shuriksun On

I modified code, added done() and it works now.

Now the code looks like:

it('janus post', (done) => {
  request('https://***')
    .post('/***')
    .attach('file', 'T:/***/Test1.docx')
    .end(function(err, res) {
      if (err) throw err;
      expect(res.status).to.be.equal(200)
      done();
      console.log(res.body);           
  });
});