Jest takes 10s to run two trivial TypeScript tests, how do I determine why it's slow?

Viewed 3298

I'm trying to use Jest to run some TypeScript tests, but it's slow, taking 10s to run the following tests:

import "jest"

test("good", () => {
  expect(2).toBe(2)
})

test("bad", () => {
  expect(2).toBe(3)
})

I've run it in --watch mode as well and it has the same 10s run when the file changes.

I am using TypeScript in a large project. While there are no modules being imported in the sample file, the tsconfig.json has paths to the whole project.

How do I determine why Jest is going so slowly, so that I can figure out how to speed it up?


If I create a .js file with the same tests, and remove the import line, it is fast. This indicates probably something with the TypeScript aspect.

1 Answers

I got a major speedup by using isolatedModules: true in the config.

My configuration now looks like this:

/** @type {import('@ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
  preset: "ts-jest",
  testEnvironment: "node",
  globals: {
    "ts-jest": {
      isolatedModules: true,
    },
  },
  transform: {
    "^.+\\.jsx?$": "babel-jest",
  },
  moduleNameMapper: {
    // ...removed...
  },
  moduleDirectories: ["js", ".", "node_modules"],
}

Related