Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Improve Jest performance #640

Merged
merged 17 commits into from Apr 24, 2021
Merged

Improve Jest performance #640

merged 17 commits into from Apr 24, 2021

Conversation

jtbandes
Copy link
Member

@jtbandes jtbandes commented Apr 24, 2021

On my M1 MacBook Air I can now run all the tests in about 16 seconds. Running a single small test file is down from 15sec to <2sec.

There were a few components to this:

  • Jest would take several seconds to load jest.config.ts files (Jest spends several seconds in loadTSConfigFile before running tests jestjs/jest#11337). Fixed by replacing them with jest.config.json files.
  • Jest tries to use the native find if available over node fs, but it doesn't pass any exclude options to find, which means that it has to wait a few seconds for find, then filter a huge list of all .js files in app/node_modules. Upgrading jest lets us pass a config option to force it to use node fs instead (feat(config): allow passing option to force node crawl jestjs/jest#11264 (comment)).
  • jsdom is no longer the default environment in jest, and there is some overhead to jsdom. We are embracing this change by only enabling jsdom in certain test files that need it. Unfortunately Jest only recognizes the /** @jest-environment jsdom */ comment if it is at the very beginning of the file, so I had to replace our eslint-plugin-header rule with a custom rule to enforce the presence of license headers.
  • Converted the custom ?raw transformer to .js instead of .ts, since we are not using ts-node anymore.
  • Actually executing the test files with ts-jest was slow, because it has to run the TS compiler. Instead we can use babel-jest which doesn't do type checking. What does the new major release of Jest (24) means for this library kulshekhar/ts-jest#961 (comment) (This also requires enabling isolatedModules in tsconfig.) For better or worse, our regular webpack build already typechecks our test files.

Before:
image

After:
image

@jtbandes jtbandes merged commit 504305c into main Apr 24, 2021
@jtbandes jtbandes deleted the jacob/jest-perf branch April 24, 2021 02:26
@@ -0,0 +1,37 @@
// This Source Code Form is subject to the terms of the Mozilla Public
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe throw this in packages?

@@ -25,8 +25,8 @@
},
"devDependencies": {
"@jest/types": "26.6.2",
"jest": "26.6.3",
"jest-circus": "26.6.3",
"jest": "27.0.0-next.8",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop this in favor of just having it listed in repo root?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amacneil thoughts on this after your excursions in packaging setup?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't done my package.json cleanup yet, but jest and typescript should be listed in each package.json so that we can individually run yarn test (or yarn jest) from inside these packages.

@amacneil
Copy link
Contributor

amacneil commented Apr 25, 2021

Are test files still being type checked in CI now?

@jtbandes
Copy link
Member Author

Some are, like the ones in app, but maybe not all. That is an important follow up item I can look at next week

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants