How to solve the Memory Leak problem on Node.js while running Jest
It looked like another typical day, with different deployments. However, we were surprised by a CircleCI error while running some of our E2E test cases. After re-running the deployment process multiple times, we realized it began to fail randomly without any explanation. The test cases worked perfectly in our local environments, but this is what appeared on CircleCI:
This tutorial is going to be divided into the following points:
- Problem explanation.
- Solution 1: Change the NodeJS version.
- Solution 2: Using the Garbage Collector.
- CircleCI integration.
The heap size indicates the amount of memory allocated by the Node.js process during the test. We weren’t concerned about this problem until CircleCI complained about it, so we started doing some tests. Before looking at the problem, this is the configuration we had:
- NodeJS (v16.17.1)
- Typescript (ts-node v10.9.1)
- Jest (ts-jest v27.1.5)
This is the command we used to run all the test cases of our Node application:
jest __tests__/e2e/ --logHeapUsage --runInBand --detectOpenHandles --forceExit
Just a few comments on this command:
- The --logHeapUsage logs the heap…