Skip to content

Instantly share code, notes, and snippets.

@mbjelac
Last active November 10, 2023 10:30
Show Gist options
  • Save mbjelac/95cc7a45529c187b7e9de2346f63099f to your computer and use it in GitHub Desktop.
Save mbjelac/95cc7a45529c187b7e9de2346f63099f to your computer and use it in GitHub Desktop.
API test
// 👇 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
});
@mbjelac
Copy link
Author

mbjelac commented Nov 9, 2023

We can stub the backend method because it was labelled on the backend.

@mbjelac
Copy link
Author

mbjelac commented Nov 9, 2023

The @group test separation powered by jest-runner-groups.

@mbjelac
Copy link
Author

mbjelac commented Nov 10, 2023

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