How to replace a global function while executing unit test with a joke

I am working on writing a unit test for a module. And I really need help to handle global vars and functions. I just have a question: Let's say the module I want to test by the name 'needTest.js'. It is structured as follows:

enter image description here

needTest.js, import main from '../needTest.js'. : main p1, p2 p3, , isA, isB, isC. ? , , , .

+4
1

- Jest jest.mock.

jest.mock('module-isA-came-from');
jest.mock('module-isB-came-from');
jest.mock('module-isC-came-from');

factory:

jest.mock('module-isA-came-from', () => jest.fn(() => 42));

:

isA.mockReturnValue(42);
0

Source: https://habr.com/ru/post/1675111/


All Articles