我有一个以前创建的.js文件,它模拟了我们的一些函数用于jest测试目的.我正在将其迁移到.ts文件:
Server.ts
const Server = jest.genMockFromModule('../Server');
Server.getAsync = Server.default.getAsync;
// other REST-ful functions here
export default Server;
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
类型"{}"上不存在属性"getAsync"
类型"{}"上不存在属性"默认"
然后,在相应的测试文件中:
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');
});
});
});
Run Code Online (Sandbox Code Playgroud)
我收到了错误 dispatchMock.mockImplementationOnce()
期望1个参数,但得到0.(方法)jest.MockInstance <{}>.mockImplementationOnce(fn:(... args:any …