6

Im trying to test getting all users from my REST API.

describe('GET', () => {
    let userId;

    // Setup create the mock user
    beforeAll(async () => {
      //Create the user
      return await request
          .post(routes.users.create)
          .set('Accept', 'application/json')
          .send(TEST_USER_DATA)
          .then(res => userId = res.body.id)
    })

    // Clean up, deleting all the fake data that we created for this test suite
    afterAll(async () => {
      // Clean up, delete the user we created
     return await request.delete(routes.users.delete(userId));
    })

    it('should get all users', async () => {
      const usersResponse = await request
        .get(routes.users.getAll)
        .set('Accept', 'application/json')
        .expect(200)
        .expect('Content-Type', /json/);
      // Logs an empty array
      console.log(usersResponse.body);

      expect(usersResponse.status).to.equal(200);
      expect(Array.isArray(usersResponse.body)).to.be.true();
    });
});

But it look as though my it() block doesn't wait for beforeAll() to finish, because userResponse.body() is just an empty array. But when I do the same think in Postman(e.g. Create a mock user, then get all users, it displays an array with the user that we created) so the problem is definitely not in the server-side.

I've already tried writing my beforeAll block like that:

beforeAll(async () => {
      //Create the user
      return await new Promise((resolve) => {
        request
          .post(routes.users.create)
          .set('Accept', 'application/json')
          .send(TEST_USER_DATA)
          .then(res => userId = res.body.id)
          .then(() => resolve)
        })
    })

And like that:

beforeAll(async (done) => {
      //Create the user
      request
        .post(routes.users.create)
        .set('Accept', 'application/json')
        .send(TEST_USER_DATA)
        .then(res => userId = res.body.id)
        .then(() => done());
})

But neither of them worked.

EDIT

As @jonrsharpe suggested I changed my beforeAll a bit to check the response status, and that we actually created a user

beforeAll(async () => {
      //Create the user
      return await request
          .post(routes.users.create)
          .set('Accept', 'application/json')
          .send(TEST_USER_DATA)
          .expect(200)
          .then(res => {
              userId = res.body.id;
              // Log the correct user
              console.log(res.body);
          })
})

And the beforeAll block doesn't fail, so the creation of a user by itself is working fine.

Olga Kedel
  • 101
  • 1
  • 6
  • Does the request to create the user definitely succeed? If you add e.g. `.expect(201)` or log out the response does that tell you anything? Does it work if you move the creation inside the test? – jonrsharpe Apr 06 '20 at 10:56
  • *Is* that working? A POST to create a resource should succeed `201 Created`. What if you e.g. `.expect(404)`, does it fail? – jonrsharpe Apr 06 '20 at 11:06
  • @jonrsharpe Yea, it 100% works, I tested it in postman. About the return code, for some reason the requirements on this project are for POST requests to return 200 instead of 201. – Olga Kedel Apr 06 '20 at 11:20
  • 3
    Did you ever manage to solve this? Would it be possible that the server was too slow, so that the POST request in beforeAll hit the timeout (default: 5s) and the tests started too early? – Matthias Sep 14 '20 at 07:59
  • @OlgaKedel I am having a similar problem, where an async `beforeAll` fails to finish before `beforeEach` (in the same file) is called. It appears to indeed be a problem with Jest's `beforeAll`, as the same code works fine outside of Jest. – Asker Feb 14 '21 at 08:31
  • I have a similar problem: "Cannot log after tests are done. Did you forget to wait for something async in your test?" when using console.log() inside beforeAll. Is there any progress with this? – Nikko Mar 02 '21 at 10:42

0 Answers0