【发布时间】:2018-10-16 04:49:09
【问题描述】:
当我使用 TypeScript 编译器编译测试并使用 Jest 模拟时,我经常收到来自 tsc 的错误,例如:
error TS2339: Property 'mockImplementationOnce' does not exist on type
'typeof readFile'.
来自这个最小的测试:
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...
});
除了:
- 铸造:
readFile as jest.Mock<{}> - module augmentation
当jest.mock 需要模块时,TypeScript 插件可以执行模块扩充吗?
【问题讨论】:
-
也许有人在使用 this method 有帮助
标签: unit-testing typescript jestjs typescript2.0