-1

I have test code as below:

const mocha = require('mocha');
const describe = mocha.describe;
const it = mocha.it;
const chai = require('chai');
const request = require('supertest');

const app = require('../app.test');

chai.should();

describe('Get /histories', () => {
  it('should return 200 status code', done => {
    request(app)
      .get('/client/profile')
      .expect('Content-Type', /json/)
      .expect(200, done);
  });

  it('should return code: 400', done => {
    request(app)
      .get('/client/profile')
      .expect('Content-Type', /json/)
      .expect(200)
      .expect(res => {});
  });
});

And I respond with a custom status code using Express:

return res.json({
    code: 400
})

Therefore I want to test that test code has code as key of object and the value is 400 as number.

How can I write this test?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Thomas Jason
  • 538
  • 2
  • 7
  • 18

2 Answers2

1

You could use this:

it('should return code: 400', (done) => {
  request(app)
    .get('/client/profile')
    .expect('Content-Type', /json/)
    .expect(200)
    .end((err, res) => {
      if (err) {
        return done(err);
      }
      expect(res.code).to.be.equal(400);
      return done();
    });
});
0

I think this is what you are looking for. Let me know if you need more information.

it('should return code: 400', async (done) => {
    var resp = await request(app)
      .get('/client/profile')
      expect(resp.statusCode).to.equal(400)
      expect(resp.body.code).to.equal(400)
  });

resp.statusCode is the http response code. resp.body is the response body. As that is a json, the response json of will have code.

Sovik
  • 59
  • 2