10

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.

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Justin Schrader
  • 201
  • 1
  • 3
  • 7

1 Answers1

24

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) }
Fathy
  • 4,939
  • 1
  • 23
  • 25
  • 4
    Thanks! This worked - 1 important thing to note is that if you have `rootDir` set in config, then the `__mocks__` folder has to go wherever that location is. I would upvote this answer, but I don't have enough rep yet... – Justin Schrader Nov 26 '17 at 13:07