-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathindex.test.ts
More file actions
52 lines (42 loc) · 1.37 KB
/
index.test.ts
File metadata and controls
52 lines (42 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Uncomment the code below and write your tests
import type * as ModuleTypes from './index';
jest.mock('./index', () => {
const actual = jest.requireActual<typeof ModuleTypes>('./index');
return {
__esModule: true,
...actual,
mockOne: jest.fn(),
mockTwo: jest.fn(),
mockThree: jest.fn(),
};
});
import { mockOne, mockTwo, mockThree, unmockedFunction } from './index';
describe('partial mocking', () => {
let consoleSpy: jest.SpiedFunction<typeof console.log>;
beforeEach(() => {
consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => undefined);
});
afterEach(() => {
consoleSpy.mockRestore();
jest.clearAllMocks();
});
afterAll(() => {
jest.unmock('./index');
});
/* ---------------------------------------------------------------- */
test('mockOne, mockTwo, mockThree should NOT log to console', () => {
mockOne();
mockTwo();
mockThree();
expect(consoleSpy).not.toHaveBeenCalled();
expect(mockOne).toHaveBeenCalledTimes(1);
expect(mockTwo).toHaveBeenCalledTimes(1);
expect(mockThree).toHaveBeenCalledTimes(1);
});
/* ---------------------------------------------------------------- */
test('unmockedFunction SHOULD log to console', () => {
unmockedFunction();
expect(consoleSpy).toHaveBeenCalledTimes(1);
expect(consoleSpy).toHaveBeenCalledWith('I am not mocked');
});
});