我有一个之前创建的.js
文件模拟了我们的一些功能jest
测试目的。我正在将其迁移到.ts
file:
服务器.ts
const Server = jest.genMockFromModule('../Server');
Server.getAsync = Server.default.getAsync;
// other REST-ful functions here
export default Server;
我收到以下错误:
类型“{}”上不存在属性“getAsync”
类型“{}”上不存在属性“default”
然后,在相应的测试文件中:
MyComponent.test.ts
import Server from 'path/to/Server';
jest.mock('path/to/Server');
const dispatchMock = jest.fn();
const getStateMock = jest.fn();
describe('MyComponent.someFunction', () => {
beforeEach(() => {
jest.resetAllMocks();
});
it('Does the right stuff', () => {
Server.getAsync.mockReturnValueOnce(Promise.resolve([{ key: 'value' }]));
dispatchMock.mockImplementationOnce((promise) => promise);
dispatchMock.mockImplementationOnce();
return someFunction()(dispatchMock)
.then(() => {
expect(Server.getAsync).toHaveBeenCalledTimes(1);
expect(Server.getAsync.mock.calls[0][0]).toBe('something');
});
});
});
我收到错误dispatchMock.mockImplementationOnce()
预期有 1 个参数,但得到 0 个。(方法)
jest.MockInstance.mockImplementationOnce(fn: (...args: any[]) =>
任何):jest.Mock
...on Server.getAsync.mockReturnValueOnce
属性“mockReturnValueOnce”在类型“(url: string,
基本路线?: 字符串 | null,loadingGenerator?:(isLoading:布尔值)=>
{ 类型:字符串...'。
...以及继续Server.getAsync.mock
属性“mock”在类型“(url: string, baseRoute?:
字符串| null,loadingGenerator?: (isLoading: boolean) => { 类型:
斯特林...'。
我已经为此苦苦思索了一段时间,所以任何帮助将不胜感激。
UPDATE
好的,我添加了as any
到我的第一行的末尾Server.ts
文件所以现在看起来像:
const Server = jest.genMockFromModule('../Server') as any;
这样就消除了第一组错误。仍然面临我的错误.test.ts
文件虽然。
UPDATE 2
我注意到,当我运行实际的笑话测试时,即使存在类型错误,它们也都通过了。这些问题似乎与实际测试无关。