Freestyle step fails while testing a Node app with Jest
Overview
Step fails when running tests with a similar error:
Message: Failed to read environment variable exporting file:/codefresh/volume/asdfg/env_vars_to_export
Caused by: Failed to read file /codefresh/volume/asdfg/env_vars_to_export
Details
This error can mean multiple things, but the most common reason is that the build was killed due to reaching the memory limit.
There is a known memory consumption issue with Jest affecting Node v16.11.0 or later. You can avoid this issue by following the steps below.
- Check the size (Small, Medium, Large, etc.) of the build to see the memory limit for the build.
- Choose one of the options:
- Upgrade Jest to v29.0 or later and use workerIdleMemoryLimit option which was created as a workaround for aforementioned issue. Adjust this option according to the amount of memory available for your build.
-
Use –max-old-space-size V8 flag to set the max memory size of V8’s old memory section:
Example: change your test command from
jest
tonode --max-old-space-size=<SIZE> node_modules/.bin/jest <JEST_ARGS>
, where--max-old-space-size
value is smaller than the memory limit for the build size in MB.
- Re-run the build, and it should not be killed due to hitting the memory limit.