相关疑难解决方法(0)

使用jest模拟时的Typescript错误

我有一个以前创建的.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 …

javascript unit-testing typescript reactjs jestjs

4
推荐指数
2
解决办法
7242
查看次数

标签 统计

javascript ×1

jestjs ×1

reactjs ×1

typescript ×1

unit-testing ×1