【问题标题】:Vitest mock modules function in only one test and use the actual function in othersVitest 模拟模块仅在一项测试中起作用,而在其他测试中使用实际功能
【发布时间】:2022-11-02 18:20:42
【问题描述】:

以下是我的问题的抽象,因此没有太大意义:

鉴于我有一个简单的实用程序callMethodIf,它返回另一个导入方法(blackbox)的返回值。

~~/utils/call-method-if.js:

import { blackbox } from '~~/utils/blackbox';

export const callMethodIf = (condition) => {
    return blackbox(condition);
};

~~/utils/blackbox.js:

export const blackbox = (condition) => {
    return { called: condition };
};

我将如何运行一个调用blackbox() 的实际实现的测试用例和另一个我模拟blackbox() 的返回值的测试用例?

我试图这样做:

import { describe, expect, it } from 'vitest';

import { callMethodIf } from '~~/utils/call-method-if';

describe('Call method if', () => {
    it('returns "called: true" if condition is true', () => {
        const result = callMethodIf(true);
        expect(result).toEqual({ called: true });
    });

    it('returns mocked blackbox return object', () => {
        vi.mock('~~/utils/blackbox', () => ({
            blackbox: vi.fn().mockReturnValue({ mock: true })
        }));
        const result = callMethodIf(false);
        expect(result).toEqual({ mock: true });
    });
});

如果我只运行其中一个测试,这两个测试都可以工作,但它们在组合时不起作用。

运行 vi.clearAllMocks()vi.resetAllMocks() 无济于事。

在我的第一个测试中定义一个全局模拟并覆盖它也不起作用:

import { describe, expect, it } from 'vitest';

import { callMethodIf } from '~~/utils/call-method-if';

vi.mock('~~/utils/blackbox', () => ({
    blackbox: vi.fn().mockReturnValue({ mock: true })
}));

describe('Call method if', () => {
    it('returns "called: true" if condition is true', () => {
        vi.mock('~~/utils/blackbox', async () => ({
            blackbox: (await vi.importActual('~~/utils/blackbox')).blackbox
        }));
        const result = callMethodIf(true);
        expect(result).toEqual({ called: true });
    });

    it('returns mocked blackbox return object', () => {
        const result = callMethodIf(false);
        expect(result).toEqual({ mock: true });
    });
});

【问题讨论】:

    标签: javascript unit-testing nuxt.js vitest


    【解决方案1】:

    好的,经过大量的试验和错误,我终于让它工作了。我真的不知道为什么我以前的尝试没有奏效。

    工作解决方案:

    import { describe, expect, it } from 'vitest';
    
    import { callMethodIf } from '~~/utils/call-method-if';
    
    vi.mock('~~/utils/blackbox');
    
    describe('Call method if', () => {
        it('returns "called: true" if condition is true', async () => {
            const blackbox = await import('~~/utils/blackbox');
            blackbox.blackbox = (await vi.importActual('~~/utils/blackbox')).blackbox;
            const result = callMethodIf(true);
            expect(result).toEqual({ called: true });
        });
    
        it('returns mocked blackbox return object', async () => {
            const blackbox = await import('~~/utils/blackbox');
            blackbox.blackbox = vi.fn().mockReturnValue({ mock: true });
            const result = callMethodIf(false);
            expect(result).toEqual({ mock: true });
        });
    });
    

    使用 TypeScript 时,请考虑像这样输入 importActual() 返回:

    blackbox.blackbox = (await vi.importActual<typeof import('~~/utils/blackbox')>('~~/utils/blackbox')).blackbox;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 2021-10-01
      • 2020-04-11
      • 2020-12-12
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      相关资源
      最近更新 更多