Skip to content

Instantly share code, notes, and snippets.

@michaelpaul
Created March 17, 2020 21:21
Show Gist options
  • Select an option

  • Save michaelpaul/43c6f91f2a3e5abe95304e3385546ad3 to your computer and use it in GitHub Desktop.

Select an option

Save michaelpaul/43c6f91f2a3e5abe95304e3385546ad3 to your computer and use it in GitHub Desktop.
Jest Mock
import { login, authenticate } from './auth';
jest.mock('./auth');
test('can add', () => {
expect(2 + 2).toBe(4)
});
test('function mock', () => {
const mockFn = jest.fn(() => 42);
expect(mockFn()).toBe(42);
expect(mockFn.mock.calls.length).toBe(1);
});
test('function mock with multiple returns', () => {
const mockFn = jest.fn();
mockFn.mockReturnValueOnce(32).mockReturnValueOnce(64);
expect(mockFn()).toBe(32);
expect(mockFn()).toBe(64);
expect(mockFn.mock.calls.length).toBe(2);
});
test('auth.login mock', () => {
login();
expect(login.mock.calls.length).toBe(1);
});
test('auth.authenticate mock', () => {
authenticate.mockReturnValueOnce(true);
expect(authenticate()).toBe(true);
});
test('auth.authenticate mock false', () => {
authenticate.mockReturnValueOnce(false);
expect(authenticate()).toBe(false);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment