Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test assertion may be unpredictable #31

Closed
FrazCake opened this issue Sep 30, 2020 · 1 comment
Closed

Test assertion may be unpredictable #31

FrazCake opened this issue Sep 30, 2020 · 1 comment

Comments

@FrazCake
Copy link

HI, i was looking at this test:

test('should return 200 and apply the default query options', async () => {
      await insertUsers([userOne, userTwo, admin]);

      const res = await request(app)
        .get('/v1/users')
        .set('Authorization', `Bearer ${adminAccessToken}`)
        .send()
        .expect(httpStatus.OK);

      expect(res.body).toEqual({
        results: expect.any(Array),
        page: 1,
        limit: 10,
        totalPages: 1,
        totalResults: 3,
      });
      expect(res.body.results).toHaveLength(3);
      expect(res.body.results[0]).toEqual({
        id: userOne._id.toHexString(),
        name: userOne.name,
        email: userOne.email,
        role: userOne.role,
      });
    });

More in detail at this part:

expect(res.body.results[0]).toEqual({
        id: userOne._id.toHexString(),
        name: userOne.name,
        email: userOne.email,
        role: userOne.role,
      });
    });

It seems to me that you can't be sure that the first element is userOne, because there is no default sorting provided and mongo don't ensures a default sort. Am I wrong?
Maybe you can do:

expect(res.body.results).toContainEqual({
        id: userOne._id.toHexString(),
        name: userOne.name,
        email: userOne.email,
        role: userOne.role,
      });
    });
@hagopj13
Copy link
Owner

hagopj13 commented Oct 3, 2020

You're right. Thanks a lot. I fixed it by adding a default sort order (based on createdAt) in the paginate plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants