Nx: Slow jest tests on Windows

Created on 27 Apr 2019  路  19Comments  路  Source: nrwl/nx

Expected Behavior

Tests are execetued fast on Windows

Current Behavior

Tests are exectued slow on Windows

Failure Information (for bugs)

Problem is with ts-jest which from version to version become slower and slower.
(I've written about it here: https://github.com/kulshekhar/ts-jest/issues/259#issuecomment-485784109)

Steps to Reproduce

I've made sample repo to demonstrate the problem
https://github.com/Karql/angular-nx-slow-tests

Please read the README.md - every thing is in it.

Other

Only one work around I've found is set isolatedModules: true in ts-jest config.
But for now there is now way to pass this option to JestBuilder
https://github.com/nrwl/nx/blob/625b2837a806e02709bdbe7b45ce3b131760448c/packages/jest/src/builders/jest/jest.builder.ts

To achieve it I've made hacky post install script which add this options:

const fs = require('fs');
const path = require('path');

// WORKAROUND for performance issue with ts-jst on windows
// https://github.com/kulshekhar/ts-jest/issues/259

var jestBuilderPath = path.join(__dirname, "..", "node_modules", "@nrwl", "builders", "src", "jest", "jest.builder.js")
let jestBuilder = fs.readFileSync(jestBuilderPath, { encoding: 'utf8' });

if (jestBuilder.indexOf("isolatedModules") < 0) {
    jestBuilder = jestBuilder.replace("var tsJestConfig = {", "var tsJestConfig = {\n\t\t\tisolatedModules: true,");
    fs.writeFileSync(jestBuilderPath, jestBuilder);
}

Do you have any idea how to fix it?

In jest 24 there is native support for type script:
https://jestjs.io/blog/2019/01/25/jest-24-refreshing-polished-typescript-friendly
maybe this is the way? Please read the opinion one of the main ts-jest contributors https://github.com/kulshekhar/ts-jest/issues/961#issuecomment-458294885

Regards!

retry with latest bug

Most helpful comment

Any updates on this?
Btw, if anybody wants to make the workaround that Karql mentions, by hand:
node_modules/@nrwl/jest/src/builders/jest/jest.impl.ts:

var tsJestConfig = {
  isolatedModules: true,
  ...

Worked for my team, thanks!

All 19 comments

We have the same problem. tests run under 20s on my macbook pro, but take 300 seconds on my colleagues windows PC since upgrading nx and Jest 24

+1 tests are super slow on windows, can we get some support on this?

Colleague of @ernie58 here with some more info

It works fine if I use WSL with Debian and node on it.
My node versions are the same on WSL and my Windows machine (10.15.3)
Jest 24 has a specific ticket on performance issues on their github https://github.com/facebook/jest/issues/7811

Since the tests also run fast on our Linux build agents, this seems like something Windows specific?

@TomDobbelaere yes looks like Windows specific, on macOS it's OK too

Any updates on this?
Btw, if anybody wants to make the workaround that Karql mentions, by hand:
node_modules/@nrwl/jest/src/builders/jest/jest.impl.ts:

var tsJestConfig = {
  isolatedModules: true,
  ...

Worked for my team, thanks!

Maybe not directly related but a friendly reminder, to check your antivirus software.

In my case the new IntellIJ version has added some exclusions to Windows Defender and it has speed up my tests by over 30%.
see: Antivirus Impact on Build Speed

Any updates on this? I had the above working for NX 7.x, but upgrading now to 8.5 and coming across this issue again. @FrozenPandaz I believe there is still no way to pass the isolatedModules flag to the builder (without the hack?)

I made a recent change to merge the jest.config.js globals with the ones set in the jest builder. You _should_ be able to add the isolatedModules flag there and have it persist now.

Can someone (@Karql, @EvtK, @mtuzinskiy...) try again? I don't have a Windows machine to hand to try this out :(

@jdpearce I have just tested it, but it doens't look like it is merged. Without the hack the tests are still very very slow. This is my jest.config.js:

module.exports = {
  testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'],
  transform: {
    '^.+\\.(ts|html)$': 'ts-jest'
  },
  resolver: '@nrwl/builders/plugins/jest/resolver',
  testEnvironment: 'jest-environment-jsdom-thirteen',
  moduleFileExtensions: ['ts', 'js', 'html', 'json'],
  collectCoverage: true,
  coverageDirectory: 'reports/coverage/',
  coverageReporters: ['html', 'cobertura'],
  setupTestFrameworkScriptFile: './test-setup.ts',
  globals: {
    'ts-jest': {
      tsConfig: '<rootDir>/tsconfig.spec.json',
      stringifyContentPathRegex: '\\.html$',
      astTransformers: [require.resolve('jest-preset-angular/InlineHtmlStripStylesTransformer')],
      isolatedModules: true
    },
  },
  testPathIgnorePatterns: ['node_modules', 'tools/schematics', '-e2e'],
  transformIgnorePatterns: ['node_modules/(?!@ngrx)'],
  snapshotSerializers: [
    'jest-preset-angular/AngularSnapshotSerializer.js',
    'jest-preset-angular/HTMLCommentSerializer.js'
  ]
};

I did add it correct right?

@EvtK - apologies, the change I made hasn't actually been released yet! That jest.config.js looks good to me, but the globals are probably still being overridden :(

Should be fixed in 8.5.1 though.

no problems, it's great you took the effort to fix this. Will wait for the 8.5.1 though!

@EvtK - we've just released 8.5.1, can you try again? 馃檹

wel this is interesting.. no matter what if I do or don't add the isolatedModules: true flag in the globals, jest seems to be somewhat quicker anyways!

maybe is isolatedModules: true now a default? Though testing this on our CI server (windows devops) doesn't seem to do the trick. Too slow.

edit: nah.. I tested some more and the global config from jest.config.js seems still overriden.

@EvtK 馃槥

I'll have to investigate this again, thanks for giving it a try!

My team is also experiencing this problem on Windows machines. I temporarily fixed it using the isolatedModules workaround. Upgrading to Nrwl 8.5.1 from 8.5.0 didn't solve it for us either.

@erik-slack check-in on this issue to get updates on merging the jest global config.

With this PR merged into Jest, it should now be possible to set globals and have them persist.

As a result, I'm going to close this issue for now. I'd suggest raising another issue if there is something else that can be done about this in Nx.

Thanks to all for contributing on this thread. 馃憤

Just a heads up for everyone in this thread: this change is not yet published in a release

Look for "[jest-config] Merge preset globals with project globals (#9027)" in the changelog to see, if it is released and of which version the change ist be a part of

FYI: https://github.com/kulshekhar/ts-jest/pull/1549 will be in alpha version of ts-jest (possibly today). You can test the alpha version and give some feedbacks for https://github.com/kulshekhar/ts-jest/issues/1115

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jasedwards picture jasedwards  路  3Comments

joelmuskwe picture joelmuskwe  路  3Comments

zachnewburgh picture zachnewburgh  路  3Comments

markphip picture markphip  路  3Comments

IonFoXx picture IonFoXx  路  3Comments