kulshekhar / ts-jest

A Jest transformer with source map support that lets you use Jest to test projects written in TypeScript.
https://kulshekhar.github.io/ts-jest
MIT License
6.95k stars 451 forks source link

High memory usage with jest projects #2015

Open richardcornelissen opened 4 years ago

richardcornelissen commented 4 years ago

šŸ› Bug Report

Memory usage is high when running ts-jest with jest projects.

To Reproduce

Steps to reproduce the behavior:

git clone git@github.com:richardcornelissen/ts-jest-projects-oom.git
yarn install
yarn test

You will see tests running with a high heap size. image

Expected behavior

Much lower memory usage.

Link to repo (highly encouraged)

https://github.com/richardcornelissen/ts-jest-projects-oom

Debug log:

Too large to inline, available at https://raw.githubusercontent.com/richardcornelissen/ts-jest-projects-oom/main/ts-jest.log

envinfo

System:
    OS: macOS 10.15.7
    CPU: 3,1 GHz Dual-Core Intel Core i5
Npm packages:
    jest: 26.5.2
    ts-jest: 26.4.1
    typescript: 4.0.3

Potential cause

From snapshotting the heap and reading the logs, it looks like initializeLanguageServiceInstance (see language-service.ts) gets called for each jest project, which keeps a cache of transpiled and instrumented code in memory (memoryCache) which never gets cleared

Workaround

If you encounter this issue, using ts-jest's isolatedModules might help. In the example repo, run yarn test:isolated-modules as an example image

ahnpnl commented 4 years ago

This has been reported in #1967 , this issue is a duplicated one to that. You can check more discussion there.