gpt4 book ai didi

module - 在单个 Jest 测试中模拟多个模块

转载 作者:行者123 更新时间:2023-12-05 08:07:29 25 4
gpt4 key购买 nike

我在开 Jest 时遇到了问题。我可以为每个测试文件模拟 1 个模块,但我无法为单个 Jest 测试模拟多个模块。

例如

import mock1 from '../mockClass1';
import mock2 from '../mockClass2';

jest.mock('../mockClass1');
jest.mock('../mockClass2');
mock1.mockImplementation(() => {});
mock2.mockImplementation(() => {});

但这会产生错误,因为其中 1 个模拟无法正常工作。有没有一种方法可以模拟这两个模块,因为我在类(class)的快照测试中都需要它们。

最佳答案

使用另一种模拟方法。我建议模拟这些方法:

const mock1: any = {
methodToMock: jest.fn(() => {})
};

关于module - 在单个 Jest 测试中模拟多个模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55123337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com