【问题标题】:Jest: is it possible to automock an imported function?开玩笑:是否可以自动模拟导入的函数?
【发布时间】:2021-05-12 23:36:43
【问题描述】:

我知道可以自动模拟模块,这样您就不必为它们创建模拟实现。

像这样:

file.test.js

jest.mock('./my-player.js');

import myPlayer from './my-player'

所以现在myPlayer 已经被模拟了,所以我可以使用它。


但是如果我有一个要导入函数的文件呢:

other.test.js

import { getData }  from './helpers';

我必须为此创建一个模拟实现还是有办法自动模拟它?

【问题讨论】:

标签: javascript unit-testing jestjs mocking


【解决方案1】:

您可以通过调用 jest.mock 来自动模拟您的助手,然后导入您的测试所需的函数,即

jest.mock('./helpers');
import { getData }  from './helpers';

jest.mock 将自动模拟导出的函数。

【讨论】:

    猜你喜欢
    • 2022-10-24
    • 2021-10-31
    • 2020-05-27
    • 1970-01-01
    • 2023-04-04
    • 2021-05-22
    • 2018-09-14
    • 2021-02-07
    • 1970-01-01
    相关资源
    最近更新 更多