Created
March 17, 2020 21:21
-
-
Save michaelpaul/43c6f91f2a3e5abe95304e3385546ad3 to your computer and use it in GitHub Desktop.
Jest Mock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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