Home > OS >  Mocha tests are passing but assertion or expect is failing
Mocha tests are passing but assertion or expect is failing

Time:11-23

I am using expect and assert both show up as errors which is what I want and I want the test to fail. But for some reason the test passes when I use either one. I tried expect and then tried assert. Not sure why this is happening. The data I am getting back from the request is correct but it is just that assert and/or expect is not working.

const assert = require('assert');
const expect = require('chai').expect;
const request = require('supertest');
const server = require('../server');

describe('Unit testing the /api/auth/signup route', function () {
  it('Should return OK status', async () => {
    try {
      let res = await request(server).post('/api/auth/signup').send({
        email: '[email protected]',
        password: 'tompassword',
      });

      // assert.equal(res.body.data.user.email, '[email protected]');
      expect(res.body.data.user.email).to.equal('[email protected]');
    } catch (err) {
      console.log(err);
    }
  });
});

CodePudding user response:

Both assert and expect throw an error (an AssertionError in Node) to report an unsatisfied expectation. Mocha will catch all errors thrown by the test function and report the test as failed.

You should not wrap those statements inside a try/catch block. Doing so will suppress the exception, and Mocha will consider the test as passed.

  • Related