I'm writing a React library MyLibrary
and bundling it with Rollup 2.58.3. I use jest for unit testing.
I have a module ModuleA.js
in MyLibrary
.
// ModuleA.js
export const Aaa = () => {
...
}
...
Another module ModuleB.js
in MyLibrary
imports ModuleA.js
// ModuleB.js
import { Aaa } from './ModuleA';
...
export const Bbb = () => {
...
}
...
When bundled, rollup 2 splits ModuleA.js
into 2 chunks ModuleA.js
and ModuleA-xxxxxx.js
. As a result of the code splitting, the generated ModuleB.js
now looks something like this:
// dist/ModuleB.js
import { Aaa } from './ModuleA-xxxxxx';
...
export const Bbb = () => {
...
}
...
I import and use this library in a different project MyApp
.
// MyApp/index.js
import { Bbb } from 'MyLibrary/dist/ModuleB'
In my jest test for MyApp/index.test.js
, I need to mock the Aaa
export. This is what SHOULD work, but it doesn't:
// DOESN'T WORK
// MyApp/index.test.js
import { Bbb } from 'MyLibrary/dist/ModuleB';
jest.mock('MyLibrary/dist/ModuleA', () => ({
Aaa: jest.fn(),
}));
However, if I mock the chunk generated by rollup, it works.
// WORKS
// MyApp/index.test.js
import { stuff } from 'MyLibrary/dist/ModuleB';
jest.mock('MyLibrary/dist/ModuleA-xxxxxx', () => ({
Aaa: jest.fn(),
}));
How can I properly mock ModuleA
from my library in my jest test?
from Recent Questions - Stack Overflow https://ift.tt/3Dd9gYl
https://ift.tt/eA8V8J
No comments:
Post a Comment