Skip to content

Instantly share code, notes, and snippets.

@mbjelac
Last active November 10, 2023 10:30
Show Gist options
  • Save mbjelac/b111b38303ec17d40102dd1bb12bf984 to your computer and use it in GitHub Desktop.
Save mbjelac/b111b38303ec17d40102dd1bb12bf984 to your computer and use it in GitHub Desktop.
API test (spying)
// 👇 separate from other types of Jest tests
/**
* @group integration/api
*/
// imports ...
it('admin creates user', async () => {
await loginAs(Role.ADMIN); // login helper (setup 1 dummy user for each role)
const user = { email: 'aNewEmail', roles: [Role.ADMIN] };
await createUser(user); // call the API function
expect(await getLastMethodCall()) // gets the last method call from the backend's API test spy endpoint
.toEqual<MethodCall>(
{
method: 'create-user', // the call was made to the method labelled 'create-user'
arguments: [user] // just one argument here, but can be more depending on the method signature
}
);
});
@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.

@mbjelac
Copy link
Author

mbjelac commented Nov 10, 2023

We spy the backend method because it was labelled on the backend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment