Within my module, I have a function called shuffle<T>(a: T[]): T[]
that is exported by the random
module. While testing two methods in another class that rely on this function, I need to mock it. Here's how I attempted to do so:
jest.mock('./random', async () => {
const { Foo } = await import('./Foo');
return {
...jest.requireActual('./random'),
shuffle: jest.fn().mockReturnValue([new Foo()]),
};
});
Since mock()
is hoisted, I had to dynamically import Foo
within the mock()
using an asynchronous approach. However, when running the actual test, I encountered the error:
TypeError: (0 , random_1.shuffle) is not a function
. It seems like all the functions from the random
module were undefined.
Could this be happening because of the async import? If so, what would be the correct way to import Foo
to prevent this issue with hoisting?