Jest and RANDOMBYTESREQUEST open handles

Viewed 3166

I have a FeathersJS api project running Jest for testing.

My tests suite is working fine. However, it always ends with the following warning message:

Jest has detected the following 2 open handles potentially keeping Jest from exiting:

  ●  RANDOMBYTESREQUEST

      at random (node_modules/bcryptjs/dist/bcrypt.js:70:56)
      at node_modules/bcryptjs/dist/bcrypt.js:84:9
      at node_modules/bcryptjs/dist/bcrypt.js:39:29
      at Object.<anonymous> (node_modules/bcryptjs/dist/bcrypt.js:43:2)


  ●  RANDOMBYTESREQUEST

      at Object.<anonymous>.module.exports (node_modules/nexmo/node_modules/uuid/rng.js:3:10)
      at Object.<anonymous> (node_modules/nexmo/node_modules/uuid/uuid.js:57:18)
      at Object.<anonymous> (node_modules/nexmo/src/JwtGenerator.js:1:1)

What this error means and how can I fix it?

Note: I would be happy to add more details such as code sample, but I don't really know where to start. Don't hesitate to ask for more with comments, I will update the post accordingly.

Thanks

3 Answers

It's happening because the RANDOMBYTESREQUEST (request for random bytes from the OS) is taking a long time. Those random bytes are used to provide cryptographic randomness and rely on an entropy buffer that the OS builds up over time using all sorts of inputs. You can think of it as a buffer of randomness. This error is caused by the pool running out of random bytes and seems to happen when a process or machine is very new. In my case it's the uuid library that is running the entropy buffer dry when testing. https://www.npmjs.com/package/uuid#uuidv4options-buffer-offset

The fix it turns out is pretty odd. If it is your local development machine having the issue you (and this is going to seem crazy) move the mouse pointer around during the test. This adds entropy input so RANDOMBYTESREQUEST doesn't run out of random bytes.

  • Ran into a similar issue while testing a function that was using nodemailer.
x:   ●  RANDOMBYTESREQUEST
x:       23 |  * [Node-mailer docs](https://nodemailer.com/smtp/oauth2/).
x:       24 |  */
x:     > 25 | const transporter = nodemailer.createTransport(
x:          |                                ^
x:       26 |   {
x:       27 |     host: "smtp.gmail.com",
x:       28 |     port: 465,
x:       at new SMTPConnection (node_modules/nodemailer/lib/smtp-connection/index.js:48:26)
x:       at new SMTPTransport (node_modules/nodemailer/lib/smtp-transport/index.js:51:26)
x:       at Object.<anonymous>.module.exports.createTransport (node_modules/nodemailer/lib/nodemailer.js:49:27)
  • It was a server-side script meant to be run on NodeJS only as opposed to in-browser.
  • So the testEnvironment needed to be set to "node". By default, it is "jsdom". Jest config reference.
  • With "jsdom", the test ran successfully but the process continues indefinitely. By switching to "node", the warning stayed but the process terminated successfully after a few seconds.

Note:

  • Upon following the stack track, it was leading to the function crypto.randomBytes. The cause could be as pointed out by Paul Hill above.
Related