Resolve memory leaks caused by Jest with Node.js 16.x and 18.x (JavaScript heap out of memory)

retyui

Davyd NRB

Posted on January 8, 2024

Resolve memory leaks caused by Jest with Node.js 16.x and 18.x (JavaScript heap out of memory)

We saw memory leaks when running tests using a Jest framework after migrating to an up-to-date version of Node.js in our project. (jest issues#11956

  • v14.21.3 ✅ (no leaks)
  • v16.20.2 ⚠️ (leaks)
  • v18.19.0 ⚠️ (leaks)
  • v20.10.0 ✅ (no leaks)

Undoubtedly a Node.js problem, it was resolved in the release v20.10.0

Therefore, adding a --no-compilation-cache option will fix memory leaks if you plan to use versions 16 or 18.

see package.json example:

{
  "scripts": {
-    "test": "jest"
+    "test": "node --expose-gc --no-compilation-cache node_modules/.bin/jest"
  }
}
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
retyui
Davyd NRB

Posted on January 8, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related