I'm writing a React library MyLibrary and bundling it with Rollup.js 2.58.3. I use jest for unit testing.
Quick summary of the issue
I am unable to mock a module from my library using jest. This is due to the way rollup "compiles" my code.
Rollup uses its code splitting functionality to create many chunks. In one example, rollup splits Alpha.js into two chunks: Alpha.js and Alpha-xxxxxx.js. Some functionality from the original file is extracted to this "intermediate" chunk (Alpha-xxxxxx.js).
In my unit test, when mocking any method that was moved into the intermediate file Alpha-xxxxxx.js, jest seems to actually load the module from this "intermediate" module rather than the top level module.
This causes the test to fail.
e.g. jest.mock('MyLibrary/dist/Alpha') does not work since the modules are actually being loaded from Alpha-xxxxxx.js instead of Alpha.js
Detailed explanation with example
I have a two modules Alpha.js and Beta.js in MyLibrary.
MyLibrary/Alpha.js
export const Aaa = () => {
...
}
...
MyLibrary/Beta.js
import { Aaa } from './Alpha';
...
export const Bbb = () => {
...
}
...
Compiled output generated by rollup
When bundled, rollup.js splits Alpha.js into 2 chunks Alpha.js and Alpha-xxxxxx.js. As a result of the code splitting, the compiled version of Beta.js now looks something like this:
MyLibrary/dist/Beta.js
import { Aaa } from './Alpha-xxxxxx';
...
export const Bbb = () => {
...
}
...
This compiled module from MyLibrary is imported in MyApp and the app seems to import it correctly and works fine.
MyApp/index.js
import { Bbb } from 'MyLibrary/dist/Beta'
...
The problem
The jest test below fails since it does not correctly mock the Aaa export.
MyApp/index.test.js
import { Bbb } from 'MyLibrary/dist/Beta';
jest.mock('MyLibrary/dist/Alpha', () => ({
Aaa: jest.fn(),
}));
However, if I mock the intermediate chunk generated by rollup, it works.
import { stuff } from 'MyLibrary/dist/Beta';
jest.mock('MyLibrary/dist/Alpha-xxxxxx', () => ({
Aaa: jest.fn(),
}));
How can I properly mock Alpha from my library in my jest test?