Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocha with Chai HTTP async testing?

I'm testing a Rest API using Mocha and Chai HTTP. I've been writing all of the tests cases using callbacks and now I want to use async/await because is a lot more readable tan callbacks and promises, so I get this code:

let sport1, sport2;
let user1, user2;
let user1Token;
let event1, event2, event3, event4;

beforeEach(async function () {
  const preUser1 = test.createUser('[email protected]');
  const preUser2 = test.createUser('[email protected]')

  sport1 = await Sport.create(test.createSport('Tenis'));
  sport2 = await Sport.create(test.createSport('Baloncesto'));
  user1 = await User.create(preUser1);
  user2 = await User.create(preUser2);
  event1 = await Event.create(test.createEventDb(user1._id, sport1._id));
  event2 = await Event.create(test.createEventDb(user1._id, sport2._id));
  event3 = await Event.create(test.createEventDb(user2._id, sport1._id));
  event4 = await Event.create(test.createEventDb(user2._id, sport2._id));

  await chai.request(app)
    .post(`${apiPath}/sessions`)
    .set('content-type', 'application/json')
    .send({ email: preUser1.email, password: preUser1.password })
    .end(function (err, res) {
      console.log(res.body)
      user1Token = res.body.data.session.token;
    });
});

The await statements are working fine, but the problem here is with chai. I'm getting this error:

1) Events "before each" hook for "should return 200, id and event info":
 TypeError: First argument must be a string or Buffer
    at ClientRequest.end (_http_outgoing.js:605:11)
    at Test.Request.end (node_modules/superagent/lib/node/index.js:873:9)
    at node_modules/superagent/lib/request-base.js:72:12
    at Test.then (node_modules/superagent/lib/request-base.js:71:31)
    at process._tickCallback (internal/process/next_tick.js:109:7)

But this error is not raising without the async/await call. Any idea on what's going on? Thanks.

like image 702
Elias Garcia Avatar asked Sep 25 '17 19:09

Elias Garcia


1 Answers

As @Rhayene mentioned, to use Chai with promises you can't use the .end() method, that's why I'm getting that error. So instead, you only store the response in a variable and then make the needed assertions. So, my refactored code is this:

let sport1, sport2;
let user1, user2;
let user1Token;
let event1, event2, event3, event4;

beforeEach(async function () {
  const preUser1 = test.createUser('[email protected]');
  const preUser2 = test.createUser('[email protected]')

  sport1 = await Sport.create(test.createSport('Tenis'));
  sport2 = await Sport.create(test.createSport('Baloncesto'));
  user1 = await User.create(preUser1);
  user2 = await User.create(preUser2);
  event1 = await Event.create(test.createEventDb(user1._id, sport1._id));
  event2 = await Event.create(test.createEventDb(user1._id, sport2._id));
  event3 = await Event.create(test.createEventDb(user2._id, sport1._id));
  event4 = await Event.create(test.createEventDb(user2._id, sport2._id));

  const res = await chai.request(app)
    .post(`${apiPath}/sessions`)
    .set('content-type', 'application/json')
    .send({ email: preUser1.email, password: preUser1.password })

  user1Token = res.body.data.session.token;
});
like image 70
Elias Garcia Avatar answered Nov 08 '22 19:11

Elias Garcia