Skip to content

perf(tests): Slim what the Jest setup file loads for every spec - #127034

Merged
billyvg merged 1 commit into
masterfrom
perf/slim-jest-setup
Oct 9, 2026
Merged

billyvg merged 1 commit into
masterfrom
perf/slim-jest-setup

Conversation

@billyvg

@billyvg billyvg commented Oct 9, 2026

Copy link
Copy Markdown
Member

Jest's setup file loaded modules in its global hooks just to reset state that can't exist until a spec loads those modules. The afterEach toast dismissal required @sentry/scraps/toast, which pulls in core Text → Prose → CodeBlock → the whole icons barrel (about 340 modules), and beforeEach(closeModal) imported sentry/actionCreators/modal (about 100). Both hooks now check require.cache, which Jest backs with the per-file module registry, and only reset toasts or the modal store when the spec has loaded them. Modals are closed with ModalStore.closeModal(), which is all closeModal() did.

jest-dom also resolved its own aria-query (5.3.2) next to the 5.3.0 that @testing-library/dom pins, so every spec evaluated two copies. A pnpm override points jest-dom at 5.3.0, so its role data now matches what getByRole uses. The two versions differ only in the complementary role's element mapping and two dpub roles added in 5.3.2.

Measured locally on top of the RTL helper change below (base and branch interleaved, 4 workers): an empty spec loads 855 modules instead of 1,350, and specs that don't use the RTL helper (about a quarter of spec files) load 25% fewer modules and run 23% faster. RTL specs load about 150 fewer modules each, but the time difference is within noise.

This is independent of the RTL helper change, but the savings on RTL specs depend on it:

Generated with Claude Code

The global hooks in `tests/js/setup.ts` loaded modules just to reset
state that can't exist until a test loads them:

- `afterEach` required `@sentry/scraps/toast` to dismiss toasts. The toast
  pulls in core Text -> Prose -> CodeBlock -> the whole icons barrel, about
  340 modules, in every spec that never shows a toast.
- `beforeEach(closeModal)` imported `sentry/actionCreators/modal` (~100
  modules incl. analytics) to close a modal that can't be open unless the
  modal store was loaded.

Both hooks now check `require.cache` (which Jest backs with the per-file
module registry) and only load the toast or modal store when the spec has
already loaded `sonner` / the modal store. Modals are closed through
`ModalStore.closeModal()`, which is all `closeModal()` does.

jest-dom resolved its own copy of aria-query (5.3.2) while
@testing-library/dom pins 5.3.0, so every spec evaluated two copies (~150
modules). Override jest-dom's to 5.3.0; the role data now matches what
`getByRole` uses. The only data differences are the `complementary` role
mapping and two dpub roles added in 5.3.2.

Measured locally on top of the RTL test helper import slimming (base vs.
branch interleaved, 4 workers): empty spec
1,350 -> 855 modules; specs that don't use the RTL helper -25% modules
and -23% time per spec; RTL specs -151 modules per spec (time within
noise).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@github-actions github-actions Bot added the Scope: Frontend Automatically applied to PRs that change frontend components label Oct 9, 2026
@billyvg
billyvg marked this pull request as ready for review October 9, 2026 15:32
@billyvg
billyvg requested a review from a team as a code owner October 9, 2026 15:32
@billyvg
billyvg requested a review from a team October 9, 2026 15:32
@scttcper

scttcper commented Oct 9, 2026

Copy link
Copy Markdown
Member

could this be fixed with resolutions in pnpm so there's only one version of these packages?

@scttcper

scttcper commented Oct 9, 2026

Copy link
Copy Markdown
Member

nevermind i understand what you're doing now

@billyvg

billyvg commented Oct 9, 2026

Copy link
Copy Markdown
Member Author

yeah there are 2 changes in this

@billyvg
billyvg enabled auto-merge (squash) October 9, 2026 16:54
@billyvg
billyvg disabled auto-merge October 9, 2026 16:56
@billyvg
billyvg merged commit 8bec6e3 into master Oct 9, 2026
78 checks passed
@billyvg
billyvg deleted the perf/slim-jest-setup branch October 9, 2026 17:28

This branch was successfully deployed

1 active deployment
Preview — dd4ee5b9 Deployed Oct 9, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Frontend Automatically applied to PRs that change frontend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants