使用玩笑模拟时的打字稿错误 [英] Typescript errors when using jest mocks

查看:25
本文介绍了使用玩笑模拟时的打字稿错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个先前创建的 .js 文件,它模拟了我们用于 jest 测试目的的一些函数.我正在将其迁移到 .ts 文件:

I have a previously-created .js file that mocks away some of our functions for jest test purposes. I'm migrating that to a .ts file:

Server.ts

const Server = jest.genMockFromModule('../Server');

Server.getAsync = Server.default.getAsync;
// other REST-ful functions here

export default Server;

我收到以下错误:

类型{}"上不存在属性getAsync"

Property 'getAsync' does not exist on type '{}'

类型{}"上不存在属性default"

Property 'default' does not exist on type '{}'

然后,在相应的测试文件中:

Then, in a corresponding test file:

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<{}>

Expected 1 arguments, but got 0. (method) jest.MockInstance<{}>.mockImplementationOnce(fn: (...args: any[]) => any): jest.Mock<{}>

...on Server.getAsync.mockReturnValueOnce

属性 'mockReturnValueOnce' 在类型 '(url: string,baseRoute?: 字符串 |null, loadingGenerator?: (isLoading: boolean) =>{ 类型:字符串...'.

Property 'mockReturnValueOnce' does not exist on type '(url: string, baseRoute?: string | null, loadingGenerator?: (isLoading: boolean) => { type: strin...'.

...以及Server.getAsync.mock

属性 'mock' 不存在于类型 '(url: string, baseRoute?:字符串 |null, loadingGenerator?: (isLoading: boolean) => { type:字符串...'.

Property 'mock' does not exist on type '(url: string, baseRoute?: string | null, loadingGenerator?: (isLoading: boolean) => { type: strin...'.

我一直在思考这个问题,所以任何帮助都将不胜感激.

I've been pounding my head on this for a while so any help would be greatly appreciated.

更新

好的,我将 as any 添加到我的 Server.ts 文件第一行的末尾,所以现在看起来像:

Okay, I added as any to the end of the first line of my Server.ts file so now it looks like:

const Server = jest.genMockFromModule('../Server') as any;

这消除了第一组错误.仍然面临我的 .test.ts 文件中的错误.

That got rid of the first set of errors. Still facing the errors in my .test.ts file though.

更新 2

我注意到,当我运行实际的 jest 测试时,即使存在类型错误,它们也全部通过.这些问题似乎与实际测试无关.

I've noticed that when I run the actual jest tests, that they all pass even though there are TypeErrors. These issues don't appear to be related to actual tests.

推荐答案

我自己解决了这个问题.我让它工作的方法是将任何对 Server.getAsync 的调用转换为特定的 jest 模拟类型.

I fixed this myself. The way that I got it to work was to cast any calls to Server.getAsync to the specific jest mock type.

let getAsyncMock = Server.getAsync as jest.Mock

let getAsyncMock = <jest.Mock>(Server.getAsync)

这消除了我的错误.

这篇关于使用玩笑模拟时的打字稿错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆