I want to mock genName
function and test getMessage
function
moduleA.ts
:
const getMessage = (): string => {
return `Her name is ${genName()}`;
};
function genName(): string {
return Math.random() > 0.5 ? 'emilie' : 'novaline';
}
export default {
getMessage,
genName
}
Here is my test file:
import moduleA from '../moduleA';
moduleA.genName = jest.fn(() => 'mrdulin');
describe('mock function', () => {
it('t-0', () => {
expect(jest.isMockFunction(moduleA.genName)).toBeTruthy();
expect(moduleA.genName()).toBe('mrdulin');
});
it('t-0.5', () => {
expect(jest.isMockFunction(moduleA.genName)).toBeTruthy();
});
it('t-1', () => {
expect(moduleA.getMessage()).toBe('Her name is emilie');
expect(moduleA.genName).toHaveBeenCalled()
});
});
but it seems not mock genName
function success:
FAIL jest-examples/__test__/mock-function-0.spec.ts
● mock function › t-1
expect(received).toBe(expected)
Expected value to be (using ===):
"Her name is emilie"
Received:
"Her name is novaline"
at Object.it (jest-examples/__test__/mock-function-0.spec.ts:18:34)
at Promise.resolve.then.el (node_modules/p-map/index.js:42:16)
at process._tickCallback (internal/process/next_tick.js:109:7)
mock function
✓ t-0 (3ms)
✓ t-0.5
✕ t-1 (14ms)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 2 passed, 3 total
Snapshots: 0 total
Time: 0.152s, estimated 1s
Ran all test suites related to changed files.
But I am sure the moduleA.genName
function has been called.
PASS jest-examples/__test__/mock-function-0.spec.ts
mock function
✓ t-0 (2ms)
✓ t-0.5 (1ms)
✓ t-1 (1ms)
Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: 0.148s, estimated 1s
Ran all test suites related to changed files.
I assume you want to mock it cause of the
Math.random
call. Why not just mock it:As the test run in a sandbox there is also no way that this can interfere any other test
If you adjust a little of your code above, it will work. Instead of calling genName() directly, after the change, you will use exportFunctions.genName()