我正在尝试结合使用Jest和ts-jest来为nodeJS服务器编写单元测试。我有一些与下面非常相似的设置:
impl.ts
export const dependency = () => {}index.ts
import { dependency } from './impl.ts';
export { dependency };consumer.ts
import { dependency } from '../impl' <- importing from index.ts
export const consumer = () => {
try {
dependecy();
return true;
} catch (error) {
return false;
}
}consumer.test.ts
import * as dependencies from '../impl'
import { consumer } from './consumer'
const mockDependency = jest.spyOn(dependencies, 'depenedncy');
describe('function consumer', function () {
beforeEach(function () {
mockDependency.mockReturnValueOnce(false);
});
test('should return true', () => {});
})这只是一段简单的代码,但实际的导出/导入/测试文件遵循类似的结构。我得到了以下几行的打字错误:
TS2769: No overload matches this call.具体地说,被监视的方法不是依赖项的导入重载的一部分,所以我不能将其存根出来。我在一个不同的测试文件中做了同样的事情,没有任何问题。有人知道如何解决打字问题吗?
发布于 2021-06-10 03:31:24
问题被证明是依赖函数本身的类型问题。返回值类型不正确,这就是导致打字错误的原因。本质上,我有这样的想法:
export const dependency: Handler = () => {
return () => {} <- is of type Handler
}而不是这样
export const dependency = (): Handler => {
return () => {} <- is of type Handler
}愚蠢的错误,希望它能在未来帮助其他人。我的结论是,如果你有一个没有意义的类型错误,一定要检查所有相关变量的类型。
https://stackoverflow.com/questions/67910312
复制相似问题