Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest and RANDOMBYTESREQUEST open handles

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

like image 453
Soullivaneuh Avatar asked Jan 10 '21 12:01

Soullivaneuh


3 Answers

RANDOMBYTESREQUEST issue is in JEST

Initial investigation is in : https://github.com/facebook/jest/issues/11275

Which was then resolved in PR: https://github.com/facebook/jest/pull/11278

This fix is available in JEST version - 27.0.0.

So the solution is to upgrade Jest version to 27.0.0(minimum)

like image 103
Dev Avatar answered Jan 04 '23 00:01

Dev


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.

like image 45
Paul Hill Avatar answered Jan 04 '23 02:01

Paul Hill


  • 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.
like image 25
Maneet Avatar answered Jan 04 '23 00:01

Maneet