How can mock a destructured import in the form of
const {mymodule} = require('@org/package')
I have a custom library in a private repository that exports a function from the index.js file like this
// index.js
exports.mymodule = require('./mymodule')
Using jest I tried to mock the module like this
jest.mock('@org/package', () => ({
func: jest.fn()
}))
However, when I check the value of {module}
it remains null.
Then, when remove the curly brackets from module
, it becomes populated with the mock module.
How can I mock the destructured module?
Module object is supposed to have mymodule
property, while mocked module doesn't have it. To ensure the interoperation with ES modules, CommonJS export should have __esModule
as well:
jest.mock('@org/package', () => ({
__esModule: true,
mymodule: {
func: jest.fn()
}
}))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With