I have a lets say file.js having code something like this
const myFunc = () => {
return {
func1: () => {},
func2: () => {}
}
}
export const myObject = {
key: ''
};
export default myFunc();
I am trying to mock this exports in my test using jest. Lets say file.test.js is test file.
jest.mock('./path/file', () => {
return {
default: {
func1: jest.fn(),
func2: jest.fn()
},
myObject: {}
};
});
But when my tests is running then it is throwing me error saying _File.default.func1 is not a function
.
How can I correctly mock my js file having both default and named exports?
solution:
index.ts
:
const myFunc = () => {
return {
func1: () => {},
func2: () => {},
};
};
export const myObject = {
key: '',
};
export default myFunc();
index.test.ts
:
import fns, { myObject } from './';
jest.mock('./', () => {
return {
myObject: { key: 'teresa teng' },
func1: jest.fn(),
func2: jest.fn(),
};
});
describe('64003254', () => {
it('should pass', () => {
expect(jest.isMockFunction(fns.func1)).toBeTruthy();
expect(jest.isMockFunction(fns.func2)).toBeTruthy();
expect(myObject.key).toBe('teresa teng');
});
});
unit test result:
PASS src/stackoverflow/64003254/index.test.ts (11.809s)
64003254
✓ should pass (6ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 13.572s
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.