开玩笑:如何全局模拟node-uuid(或任何其他导入的模块) [英] Jest: How to globally mock node-uuid (or any other imported module)

查看:255
本文介绍了开玩笑:如何全局模拟node-uuid(或任何其他导入的模块)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近从mocha迁移到jest,我遇到了一个问题.我的测试中有很多警告:

Recently migrated from mocha to jest and I'm running into an issue. I have lots of warnings in my tests:

[SECURITY] node-uuid: crypto not usable, falling back to insecure Math.random()

现在,在每个文件中添加以下行即可解决此问题,但适用于该特定测试文件:

Now, adding the following line to each file fixes the issue, but only for that specific test file:

jest.mock('node-uuid', () => ({ v4: jest.fn(() => 1) }));

我希望有一种方法可以全局模拟node-uuid进行所有测试,而不是单个文件?我做了很多搜索,并在安装文件中尝试了不同的技术,但无济于事.

I'm hoping there's a way to mock node-uuid globally for all tests instead of individual files? I've done a bunch of searches and tried different techniques in my setup file, but to no avail.

推荐答案

您可以定义[root]/__mocks__/node-uuid.js中的"noreferrer>手动模拟 其中[root]node_modules目录所在的目录:

You can define a manual-mock in the [root]/__mocks__/node-uuid.js where [root] is the directory where the node_modules directory is located:

module.exports = { v4: jest.fn(() => 1) }

这篇关于开玩笑:如何全局模拟node-uuid(或任何其他导入的模块)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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