I am aware of how to mock/spy an ES6 import with jest, but this one gets around my head:
my-module.ts
import minimatch from 'minimatch';
export function foo(pattern: string, str: string): boolean {
return minimatch(pattern, str);
}
test.ts
:
describe('minimatch', () => {
it('should call minimatch', () => {
const mock = jest.fn().mockReturnValue(true);
jest.mock('minimatch', mock);
foo('*', 'hello');
expect(mock).toHaveBeenCalled();
});
});
I've also tried mocking in different way:
import * as minimatch from 'minimatch';
// ...
const mock = jest.fn().mockReturnValue(true);
(minimatch as any).default = mock;
Or even
import {mockModule} from '../../../../../../test/ts/utils/jest-utils';
// ...
const mock = jest.fn().mockReturnValue(true);
const originalModule = jest.requireActual('minimatch');
jest.mock('minimatch', () => Object.assign({}, originalModule, mockModule));
My test fails with all of the above ways to mock.