【发布时间】:2019-07-23 06:45:49
【问题描述】:
我正在尝试使用 Jest 模拟 TypeScript 类,我显然正在做某事,因为收到以下错误:
error TS2743: No overload expects 1 type arguments, but overloads do exist that expect either 0 or 2 type arguments.
这是我的代码:
Foo.ts
export default class Foo {
bar(): number {
return Math.random()
}
}
Foo.test.ts
import Foo from './Foo'
describe('Foo', () => {
it("should pass", () => {
const MockFoo = jest.fn<Foo>(() => ({
bar: jest.fn(() => {
return 123
}),
}))
})
})
完整的错误:
TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
src/Foo.test.ts:6:29 - error TS2743: No overload expects 1 type arguments, but overloads do exist that expect either 0 or 2 type arguments.
6 const MockFoo = jest.fn<Foo>(() => ({
~~~
更新
如果有任何相关性,这是我的 Jest 配置:
module.exports = {
moduleFileExtensions: ['ts', 'js'],
transform: {
'^.+\\.ts$': 'ts-jest',
},
testMatch: ['**/src/**/*.test.(ts)'],
testEnvironment: 'node',
};
【问题讨论】:
标签: typescript mocking jestjs