Skip to content

Instantly share code, notes, and snippets.

@thomashagstrom
Last active October 23, 2023 14:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thomashagstrom/e5bffe6c3e3acec592201b6892226af2 to your computer and use it in GitHub Desktop.
Save thomashagstrom/e5bffe6c3e3acec592201b6892226af2 to your computer and use it in GitHub Desktop.
Mock for single test
import { Auth } from 'aws-amplify';
import GetJwtToken from './GetJwtToken';
describe('jwt', () => {
describe('GetJwtToken', () => {
it('Original mock for Auth class', async () => {
const result: Error = (await GetJwtToken()) as Error;
expect(result.message).toBe('Could not get session');
});
it('Single function mock of `Auth.getIdToken` using spyOn', async () => {
const spy = jest.spyOn(Auth, 'currentSession').mockImplementation(() => ({
getIdToken: () => ({
getJwtToken: () => '123',
}),
}));
const result = await GetJwtToken();
expect(result).toBe('123');
spy.mockRestore();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment