/jest-runner-iansu

A patched version of the default jest-runner that prevents out of memory issues

Primary LanguageJavaScript

jest-runner-iansu

This is a patched version of jest-runner that shuts down the worker when it has used more than 80% of the available heap memory. Jest will automatically spawn a new worker and continue running the tests. This prevents any out of memory errors.

Usage

Install the runner with npm or Yarn. Make sure you install the version that corresponds to your Jest version. For example, if you are using Jest 28:

npm i -D jest-runner-iansu@28
yarn add --dev jest-runner-iansu@28

Add this line to your Jest config:

runner: 'jest-runner-iansu'

How it works

This patch only requires adding a few lines of code to the testWorker.js file:

const heap = v8.getHeapStatistics();

if (heap.total_heap_size / heap.heap_size_limit > 0.8) {
  (0, _exit().default)(1);
}

When a test is dispatched to the worker we first check if the heap usage is above 80%. If it is, the worker just exits with a non-zero exit code. Jest will automatically detect this and spawn a new worker. If the heap usage is below 80% the worker runs the test as usual.

Note I've opened an issue in Jest to see if it might be possible to build this into the default test runner and expose the threshold via a config setting/CLI argument: jestjs/jest#12893

License

The original jest-runner is released under the MIT license as are all changes in the patched versions.