fix(test-helpers): make @primer/react/test-helpers runtime-agnostic#7872
fix(test-helpers): make @primer/react/test-helpers runtime-agnostic#7872adierkens wants to merge 1 commit into
Conversation
Previously the published helper hard-referenced the `jest` global to create mock functions for its JSDOM polyfills, so importing it from a Vitest test threw 'ReferenceError: jest is not defined'. The polyfills now detect `globalThis.jest?.fn` or `globalThis.vi?.fn` at runtime and fall back to a plain no-op function if neither is present. Mock-style introspection still works for both Jest and Vitest consumers; consumers without a test runtime get a silent no-op which is sufficient for the polyfill use case. Also removes a stale '// jest function' comment from SelectPanel.test.tsx (the test actually uses vi.fn()). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
🦋 Changeset detectedLatest commit: 1057c2d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
joshblack
left a comment
There was a problem hiding this comment.
I'd be down to leave this one jest-specific since for vitest we'd be using browser mode, right? Or is that not the case? Ideally we wouldn't be shipping this file, I think.
Closes #
Makes the published
@primer/react/test-helpersentry runtime-agnostic so it works with both Jest and Vitest consumers.Today the file hard-references the
jestglobal to create mock functions for its JSDOM polyfills (ResizeObserver,HTMLDialogElement.showModal/close,HTMLCanvasElement.getContext,Element.scrollIntoView,matchMedia,CSS.escape/supports). Any downstream consumer that imports@primer/react/test-helpersfrom a Vitest test currently hitsReferenceError: jest is not defined. Primer itself migrated off Jest in #6452 but kept this published helper as Jest-only, so Vitest consumers couldn't use it.Changelog
New
Changed
packages/react/src/utils/test-helpers.tsx— replaces every barejest.fn(...)call with a smallmockFn(...)helper that detectsglobalThis.jest?.fnorglobalThis.vi?.fnat runtime and falls back to a plain no-op function if neither is present. Mock-style introspection (expect(fn).toHaveBeenCalled(), etc.) continues to work for both Jest and Vitest consumers; consumers without a test runtime get a silent no-op which is sufficient for the polyfill use case.Removed
packages/react/src/SelectPanel/SelectPanel.test.tsx— removes a stale// jest functioncomment (the test below actually usesvi.fn()).Rollout strategy
No public API surface changes. Existing Jest consumers continue to get real
jest.fn()instances (detected viaglobalThis.jest.fn). New Vitest consumers get realvi.fn()instances. Non-test environments get no-ops, which is fine for the JSDOM polyfill use case.Testing & Reviewing
npx tsc -p packages/react/tsconfig.json --noEmitclean.npx prettier --check+npx eslinton changed files clean.SelectPanel.test.tsxruns cleanly (152 tests pass) — confirms the stale-comment removal didn't affect anything.@ts-nocheck(it's a globals/polyfill setup file), so types around the runtime-detect pattern aren't enforced, intentionally.Worth a close look:
globalThis.jest?.fn ?? globalThis.vi?.fn(current — Jest wins if both present, which matches the historical behaviour for Jest consumers).() => {}. Consumers without a test runtime importing this helper are an edge case (it's a test polyfill), but the no-op keeps the polyfill side-effects working.Merge checklist
typeof document !== 'undefined'guard remains in place