【发布时间】:2021-08-26 19:43:19
【问题描述】:
我正在尝试将 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.
具体来说,被监视的方法不是依赖项导入重载的一部分,所以我不能把它存根。我在不同的测试文件中做同样的事情,它没有问题。有人知道如何解决打字问题吗?
【问题讨论】:
-
你拼错了
depenedncy和dependecy
标签: node.js typescript jestjs es6-modules ts-jest