My question is about how to mock jest function importing from another file as default.
What I want to test is that component using function to enabled Feature (Features.js
)
I mocked this function using jest.fn() and tried to change value using mockReturnValueOnce
It looks like below.
mocks/features.js
export default function isFeatureEnabled (featureName) {
return true // want to test both true/false cases
}
test.spec.js
jest.mock('__mocks__/features.js', () => ({
isFeatureEnabled: jest.fn()
}))
describe('isFeatureEnabled', () => {
it('isFeatureEnabled=[true]', () => {
isFeatureEnabled.mockReturnValueOnce(true)
// some tests
})
it('isFeatureEnabled=[false]', () => {
isFeatureEnabled.mockReturnValueOnce(false)
// some tests
})
})
When I run the test, I got an error says mockReturnValueOnce is not a function
. This stackoverflow question inspired me to implement in this way, however still I cannot figure it out how to make it working.