fix(test-helpers): make @primer/react/test-helpers runtime-agnostic#7872
Draft
adierkens wants to merge 1 commit into
Draft
fix(test-helpers): make @primer/react/test-helpers runtime-agnostic#7872adierkens wants to merge 1 commit into
adierkens 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 |
Contributor
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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