mercredi 13 juin 2018

Jest finishing async test before done()

I'm writing an async test in Jest and having trouble. I'm pretty sure the test is finishing and passing before the async call returns anything in spite of everything I've tried. I know the function works because it logs out the correct response after the test suite has finished. Here is the test code:

describe('updateUser', () => {
  test('should update a user', (done) => {

    updateUser(user).then(({err, res}) => {
      console.log('updated user:', err, res); // this show up after the test is done
      expect(err).toBeFalsy();
      expect(res).toBeTruthy();
      done();
    });
  });
});

console output:

 updateUser
   ✓ should update a user (68ms)

Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        6.058s
Ran all test suites.
 console.log server/db/crud_Users/crud_user.test.js:38
   updated user: null { homeLocations: [],
     meetingPlaces: [],
     hostedEvents: [],
     attendingEvents: [],
     flags: [],
     tokens: [],
     _id: 5b2147495995cb45f9c4f079,
     name: 'test',
     email: '83277963493533480000@test.com',
     password: 'testtest',
     ageRange: '1',
     gender: 'Female',
     accountCreatedAt: null,
     __v: 0 }

Expected behavior: the test suite waits for the console.log statement and the assertions to run before finishing.

Actual behavior: it doesn't.

I also tried making the test callback an async function and awaiting the updateUser call, but nothing changed; I tried tacking the done() callback on in a second .then block as well, with no results.

Aucun commentaire:

Enregistrer un commentaire