Trying to use supertest to check the body of response - getting an error
Asked Answered
T

6

36

I am trying to use supertest for some testing. Here is the code snippet that I am trying to test:

it("should create a new org with valid privileges and input with status 201", function(done) {
  request(app)
    .post("/orgs")
    .send({ name: "new_org", owner: "[email protected]", timezone: "America/New_York", currency: "USD"})
    .expect(201)
    .end(function(err, res) {
      res.body.should.include("new_org");
      done();
    });
});

I am getting an error when trying to test the res body:

 TypeError: Object #<Object> has no method 'indexOf'
  at Object.Assertion.include (../api/node_modules/should/lib/should.js:508:21)
  at request.post.send.name (../api/test/orgs/routes.js:24:27)
  at Test.assert (../api/node_modules/supertest/lib/test.js:195:3)
  at Test.end (../api/node_modules/supertest/lib/test.js:124:10)
  at Test.Request.callback (../api/node_modules/supertest/node_modules/superagent/lib/node/index.js:575:3)
  at Test.<anonymous> (../api/node_modules/supertest/node_modules/superagent/lib/node/index.js:133:10)
  at Test.EventEmitter.emit (events.js:96:17)
  at IncomingMessage.Request.end (../api/node_modules/supertest/node_modules/superagent/lib/node/index.js:703:12)
  at IncomingMessage.EventEmitter.emit (events.js:126:20)
  at IncomingMessage._emitEnd (http.js:366:10)
  at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23)
  at Socket.socketOnData [as ondata] (http.js:1367:20)
  at TCP.onread (net.js:403:27)

Is this a bug in supertest, or am I formatting my test incorrectly? Thanks

Twain answered 15/1, 2013 at 13:57 Comment(1)
Side note: Remember to handle err within your .end function or it will ignore any previously raised exceptions.Ingalls
M
20

Alternatively, this should work too:

res.body.should.have.property("name", "new_org");

Also, just a note but logically I think it makes sense to put this in another call to expects instead of in the final callback. This function can also be re-used, so I tend to put it somewhere reusable when possible:

var isValidOrg = function(res) {
  res.body.should.have.property("name", "new_org");
};

it("should create a new org with valid privileges and input with status 201", function(done) {
  request(app)
    .post("/orgs")
    .send({ name: "new_org", owner: "[email protected]", timezone: "America/New_York", currency: "USD"})
    .expect(201)
    .expect(isValidOrg)
    .end(done);
});

Now you could imagine you're testing a GET for /orgs/:orgId and you could just re-use the same validation.

Metaphrast answered 7/2, 2014 at 22:41 Comment(0)
D
19

Using Jest this works for me like this

  it('should do something', async () => {

    const test = await supertest(app)
      .post('/some/endpoint')
      .send({
        someResource,
      })
      .expect(200);

    expect(test.body.someKey).toBeDefined();
    expect(test.body.someKey).toBe('someValue');
  });
Distillery answered 18/5, 2021 at 16:36 Comment(0)
T
6

To test a response body, you can just include the expected response in expect,

const { describe, it } = require('mocha');
const supertest = require('supertest');
describe('Validate API calls', () => {
  it('create session post request should fail for invalid credentials', (done) => {
    const data = { user_name: 'incorrect_username', password: 'INVALID' };
    supertest(app).post('/api/session')
      .send(data)
      .expect('Content-Type', /json/)
      .expect({ name: 'AuthenticationError', message: 'Unauthorized' })
      .expect(401, done);
  });
});

source: https://willi.am/blog/2014/07/28/test-your-api-with-supertest/

Tourbillion answered 22/3, 2019 at 11:33 Comment(0)
T
0

This can be rewritten as follows:

res.body.name.should.equal("new_org");

Which will fix the error.

Twain answered 15/1, 2013 at 14:57 Comment(0)
S
0

if your res.body is an array you need to provide the index of the object so res.body[res.body.length -1].name.should.equal("new_org") - if your property is the last in the array and not ordered

Spatula answered 25/5, 2017 at 9:54 Comment(0)
A
0

You can also just use a standard expect in the response callback

it("should create a new org with valid privileges and input with status 201", async() {
  return request(app)
    .post("/orgs")
    .send({
      name: "new_org",
      owner: "[email protected]",
      timezone: "America/New_York",
      currency: "USD"
    })
    .expect(201)
    .expect((res) => {
      expect(res.body.name).toContain('new_org')
    })
});
Annmarieannnora answered 6/3 at 12:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.