Published at

How to speed up Jest startup / compilation time by 50+%

How to speed up Jest startup / compilation time by 50+%

Test engineers hate him for using this one simple trick

Authors
  • Name
    Joachim Bülow
    Twitter
  • Cofounder and CTO at Doubble
Sharing is caring!
Table of Contents

Recently I have been working on a project that has been growing in size and complexity. We use End-To-End testing to test our application, and as the project grows, the tests are taking longer and longer to intialize and run properly.

NestJS and SWC

According to the NestJS documentation, you can use NestJS with SWC for its build process. We do this in our project, and it speeds up the build process by quite a bit.

Jest not working with SWC

Unfortunately, many developers are having problems with Jest and SWC. We made tremendous efforts in fixing circular dependencies, mocks, and other stuff that was not a problem for the regular build process. It was just a pain with Jest and SWC.

Vitest to the rescue

I said screw it - let’s go for Vitest, an ESBuild-based test runner known for its speed.

Once again - I ran into issues - NestJS and ESBuild do not work well together.

The solution

The final solution is crazy simple - we disable type-checking but stick tith ts-jest and ts-node.

We do this by using isolatedModules when running ts-jest.

I added this to my jest.config.js file:

transform: {
    '^.+\\.tsx?$': [
      'ts-jest',
      {
        isolatedModules: true,
        tsconfig: 'tsconfig.json',
      },
    ],
  },

Which gave us an immediate speed up of around 40% when initializing the tests. Pretty big deal, give it a try.

Sharing is caring!