Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Huge performance drop when "allowJs": true is enabled in tsconfig #4294

Open
LoganTann opened this issue May 14, 2024 · 3 comments
Open

Comments

@LoganTann
Copy link

LoganTann commented May 14, 2024

Version

at least since 0.3.3, tried in 0.5.3 and looks even worse (no proofs though)

Steps to reproduce

Minimal reproduction repository : https://github.com/LoganTann/tsjest-performance-issue-reproduction

  • Clone the repo
  • npm i
  • npm run test

Expected behavior

  • Jest should take 8 seconds to load the dependency, not more than 10 minutes.

Actual behavior

  • Simple test is taking an extremely long time (> 10min) to load the dependency

  • Disabling the {allowJs} property in the tsconfig file will make the test run under 8 seconds. However, jest will print over 1400 warnings (executed grep " file to compile while" output_nojs.log | wc -l).

  • After a successful run, later tests are expected to be much faster thanks to the caching.

Debug log

Interrupted execution, took too much time.

Additional context

  • Running in WSL 2, inside the ~/reproduction/ folder
  • No performance issues in the back-end, both in dev and prod. The code compiles and runs fine.
  • SWC + vitest is not affected by this issue, but I cannot migrate the whole app in another tech stack.
Performance measures I did five months ago (under 0.3.3)

cross-env NODE_ENV=test jest -runInBand --maxWorkers=1 SalesforceConnections.controller.spec.ts --no-cache

Imports : 
    HttpService: 62226 ms (@nestjs/axios)
    BadRequestException: 1 ms
    ConfigModule: 4736 ms (@nestjs/config)
    Test: 22756 ms (@nestjs/testing)
    TypeOrmModule: 133894 ms (@nestjs/typeorm)
    Repository: 0 ms
    configuration: 9 ms
    RequestUserModel: 8 ms
    CryptoService: 33 ms
    entities: 153 ms
    FixtureService: 109 ms
    TenantEntity: 0 ms
    TenantModel: 5 ms
    TenantService: 34 ms
    UserEntity: 0 ms
    SalesforceConnectionsController: 183104 ms (imports the same)
    SalesforceInstanceDetailsResponseDto: 0 ms
    SalesforceConnectionEntity: 0 ms
    SalesforceInstanceEntity: 0 ms
    SalesforceConnectionService: 0 ms
    SalesforceInstanceService: 0 ms
    SalesforceOauthService: 0 ms
    ConnectivityStatus: 1 ms
Test module compilation :
    Compile: 11315 ms

Environment

System:
    OS: Linux 5.15 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
  Binaries:
    Node: 18.19.0 - ~/.nvm/versions/node/v18.19.0/bin/node
    npm: 10.2.3 - ~/.nvm/versions/node/v18.19.0/bin/npm
  npmPackages:
    jest: ^29.7.0 => 29.7.0
@LoganTann
Copy link
Author

Related: #1115

@LoganTann
Copy link
Author

Found a workaround, suggested by the warning message when you disable "allowJs" :

  • Remove "compilerOptions.allowJs": true in your tsconfig file
  • Edit the transform regex to select only typescript files with ts-jest :
'transform': {
    '^.+\\.ts$': 'ts-jest', // << selects only ts. It was previously '^.+\\.(j|t)s$'
},

This is still an issue. This needs to be documented, as I had this issue since at least january and I had to investigate since I cleared my jest cache.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jun 28, 2024

A bit background for this issue: ts-jest internally uses TypeScript compiler API to transform ts/js file into js file.

In general, setting allowJs to true is necessary if one wants the TypeScript compiler API to load and process JavaScript files in addition to TypeScript files when transforming files. This leads to the result that more files are loaded into memory which can blow up the machine.

Currently there are 2 workarounds:

With the release of typescript 5.5, now we can actually use other transpilers like esbuild or swc to process js files + opt in type checking from new v5.5 API

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants