相关疑难解决方法(0)

如何使用Jest监视默认的导出功能?

假设我有一个导出默认功能的简单文件:

// UniqueIdGenerator.js
const uniqueIdGenerator = () => Math.random().toString(36).substring(2, 8);

export default uniqueIdGenerator;
Run Code Online (Sandbox Code Playgroud)

我会这样使用:

import uniqueIdGenerator from './UniqueIdGenerator';
// ...
uniqueIdGenerator();
Run Code Online (Sandbox Code Playgroud)

我想在测试中断言在保持原始功能的同时调用了此方法。jest.spyOn但是,我需要一个对象以及一个函数名称作为参数。您如何以一种干净的方式做到这一点?还有一个类似的GitHub的问题jasmine兴趣的人。

unit-testing mocking spy reactjs jestjs

12
推荐指数
6
解决办法
4421
查看次数

标签 统计

jestjs ×1

mocking ×1

reactjs ×1

spy ×1

unit-testing ×1