Last active
November 10, 2023 10:30
-
-
Save mbjelac/95cc7a45529c187b7e9de2346f63099f to your computer and use it in GitHub Desktop.
API test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 👇 separate from other types of Jest tests | |
/** | |
* @group integration/api | |
*/ | |
// imports ... | |
it('admin gets users', async () => { | |
await loginAs(Role.ADMIN); // login helper (setup 1 dummy user for each role) | |
const users = [ | |
{ id: 1, email: 'email1', roles: [Role.ADMIN], invitePending: false }, | |
{ id: 2, email: 'email2', roles: [Role.ADMIN], invitePending: true }, | |
]; | |
await setStubbing('get-users', users); // stub the backend method labelled 'get-users' | |
const actualUsers = await getUsers(); // get actual users with the API function under test | |
expect(actualUsers).toEqual<User[]>(users); // verify correctness | |
}); | |
it('getting users is not public', async () => { | |
logout(); | |
await expectUnauthorized(getUsers); // helper calls the referenced function & asserts unauthorized error | |
}); |
The @group
test separation powered by jest-runner-groups.
Originally published as part of this article.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We can stub the backend method because it was labelled on the backend.