I am trying to mock an imported function, since the module is an ES module and is a Sealed and readonly Object so the usual methods doesn’t seem to work due to ESM limitations.
So far I have tried:
// import { baz } from '@foo/bar'; <-- want to mock baz
import * as fooBar from '@foo/bar';
SANDBOX.stub(fooBar, 'baz').resolves({} as any); <-- can't stub ESM
SANDBOX.mock(fooBar).expects('baz').resolves({}); <-- TypeError: Cannot redefine property
SANDBOX.replace(fooBar, 'baz', SANDBOX.fake.returns({})); <-- TypeError: Cannot assign to read only property
SANDBOX.stub(fooBar); <-- ES Modules cannot be stubbed
I think what would be nice is the ability to mock/replace entire module.
Something similar to:
jest.mock('@foo/bar', () => {...blah});
Is this possible to achieve in Sinon?
New contributor
Rakshit Verma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.