When I compile my a test using the TypeScript compiler and working with a Jest mock, I often receive errors from tsc
like:
error TS2339: Property 'mockImplementationOnce' does not exist on type
'typeof readFile'.
from this minimal test:
jest.mock('fs');
// Run before the imports but does not alter types :(
import { readFile } from 'fs';
import { fnThatReadsFile } from './lib';
it('should read a file', () => {
const err = {};
readFile.mockImplementationOnce((_, callback) => callback(err, null));
// ^^ error TS2339: Property 'mockImplementationOnce' does not exist on type 'typeof readFile'.
fnThatReadsFile();
// expect...
});
What solutions are there other than:
- casting:
readFile as jest.Mock<{}>
- module augmentation
Could a TypeScript plugin perform the module augmentation when modules are required by jest.mock
?