-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlib.fail.jest-test.js
38 lines (31 loc) · 1.07 KB
/
lib.fail.jest-test.js
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
jest.mock('./db', () => ({
get: jest.fn()
}));
const lib = require('./lib.fail');
const mockDb = require('./db');
const {getTodo} = lib;
// Using const means we can't re-assign
let {makeKey} = lib;
beforeEach(() => jest.clearAllMocks());
test('CommonJS > Mocking destructured makeKey doesn’t work', async () => {
const mockMakeKey = jest.fn(() => 'mock-key');
makeKey = mockMakeKey;
await getTodo(1);
expect(makeKey).not.toHaveBeenCalled();
expect(mockDb.get).not.toHaveBeenCalledWith('mock-key');
});
test('CommonJS > Mocking lib.makeKey doesn’t work', async () => {
const mockMakeKey = jest.fn(() => 'mock-key');
lib.makeKey = mockMakeKey;
await getTodo(1);
expect(mockMakeKey).not.toHaveBeenCalled();
expect(mockDb.get).not.toHaveBeenCalledWith('mock-key');
});
test('CommonJS > Spying lib.makeKey doesn’t work', async () => {
const makeKeySpy = jest
.spyOn(lib, 'makeKey')
.mockImplementationOnce(() => 'mock-key');
await getTodo(1);
expect(makeKeySpy).not.toHaveBeenCalled();
expect(mockDb.get).not.toHaveBeenCalledWith('mock-key');
});