Karma Tests: FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
Karma is encountering severe memory issues, resulting in crashed processes and longer-than-usual compile times (likely due to increased GC). Node will rapidly hit the 1.76 GB
memory limit and crash when it cannot allocate more memory.
This appears to have coincided with the inclusion of babel-plugin-rewire
in gitlab-org/gitlab-ce!18116
Expected behavior
Karma should be able to run with yarn karma-start
and perform several incremental builds without crashing
Ideal solutions
Rewire is necessary to avoid invalid test practices which will break in webpack v4. So a solution will need to involve something other than reverting to import * as foo from 'foo';
and then spyOn(foo, 'bar');
.
A good compromise may be to get karma to a better state with some temporary hacks with some long-term solutions considered for a future project. If band-aid solutions are not possible, we may need to revert gitlab-org/gitlab-ce!18116 and put webpack v4 on hold until some other alternative is developed.
Current potential remedies (temporary):
- Manipulate node GC settings:
export NODE_OPTIONS=--max-old-space-size=4096
- Disable source maps (
webpackConfig.devtool = undefined
)
Quick solutions to research:
- Imminent webpack v4 upgrade will reduce memory consumption. Perhaps it will be enough to mitigate this issue. (!17218 (merged))
- Improve
--filter-spec
option to not include unneeded files. (!18606 (closed)) - Try enabling babel
cacheDirectory
(https://github.com/babel/babel-loader#options)
More drastic solutions to explore:
- Rewrite all of our modules that depend on
babel-plugin-rewire
in such a way that rewire is not needed (dependency injection, indirect exports, etc)- perhaps use
DefinePlugin
to create karma-specific behavior in some areas
- perhaps use
- Migrate from karma to jest (built-in mocking, parallelization, isolated test environments, snapshots, many other improvements, etc)