Skip to content

Instantly share code, notes, and snippets.

@devotdev
Created December 5, 2024 14:57
Show Gist options
  • Save devotdev/c2632ff31bdd8431853d57396f49381c to your computer and use it in GitHub Desktop.
Save devotdev/c2632ff31bdd8431853d57396f49381c to your computer and use it in GitHub Desktop.
Using beforeEach with asynchronous code
describe('Async API Tests', () => {
let mockUser;
// Example using beforeEach to set up async state before each test
beforeEach(async () => {
mockUser = { id: 1, name: 'Jane Doe' };
axios.get.mockResolvedValue({ data: mockUser });
});
it('fetches user asynchronously', async () => {
const result = await fetchData('https://api.example.com/user/1');
expect(result).toEqual(mockUser);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment