Jest: How to globally mock node-uuid (or any other imported module)

旧街凉风 提交于 2019-12-10 19:24:54

问题


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) }));

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.


回答1:


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) }


来源:https://stackoverflow.com/questions/47487766/jest-how-to-globally-mock-node-uuid-or-any-other-imported-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!