Skip to content

fix(test-helpers): make @primer/react/test-helpers runtime-agnostic#7872

Draft
adierkens wants to merge 1 commit into
mainfrom
adierkens/remove-jest-fn
Draft

fix(test-helpers): make @primer/react/test-helpers runtime-agnostic#7872
adierkens wants to merge 1 commit into
mainfrom
adierkens/remove-jest-fn

Conversation

@adierkens
Copy link
Copy Markdown
Contributor

Closes #

Makes the published @primer/react/test-helpers entry runtime-agnostic so it works with both Jest and Vitest consumers.

Today the file hard-references the jest global 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-helpers from a Vitest test currently hits ReferenceError: 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

  • Nothing added.

Changed

  • packages/react/src/utils/test-helpers.tsx — replaces every bare jest.fn(...) call with a small mockFn(...) helper that detects globalThis.jest?.fn or globalThis.vi?.fn at 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 function comment (the test below actually uses vi.fn()).

Rollout strategy

  • Patch release
  • Minor release
  • Major release
  • None

No public API surface changes. Existing Jest consumers continue to get real jest.fn() instances (detected via globalThis.jest.fn). New Vitest consumers get real vi.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 --noEmit clean.
  • npx prettier --check + npx eslint on changed files clean.
  • SelectPanel.test.tsx runs cleanly (152 tests pass) — confirms the stale-comment removal didn't affect anything.
  • The helper is @ts-nocheck (it's a globals/polyfill setup file), so types around the runtime-detect pattern aren't enforced, intentionally.

Worth a close look:

  • The runtime-detect shape itself — there are a couple of reasonable variations:
    • globalThis.jest?.fn ?? globalThis.vi?.fn (current — Jest wins if both present, which matches the historical behaviour for Jest consumers).
    • Reverse order if we'd prefer Vitest to win when both are accidentally loaded (e.g. some pnpm hoisting edge cases).
  • The fallback no-op for "no runtime" — it returns () => {}. 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

  • Added/updated tests
  • Added/updated documentation (changeset)
  • Added/updated previews (Storybook)
  • Changes are SSR compatible — the typeof document !== 'undefined' guard remains in place
  • Tested in Chrome
  • Tested in Firefox
  • Tested in Safari
  • Tested in Edge
  • (GitHub staff only) Integration tests pass at github/github-ui (Learn more about how to run integration tests)

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-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 22, 2026

🦋 Changeset detected

Latest commit: 1057c2d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/react Patch

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

@github-actions github-actions Bot added the integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm label May 22, 2026
@github-actions
Copy link
Copy Markdown
Contributor

⚠️ Action required

👋 Hi, this pull request contains changes to the source code that github/github-ui depends on. If you are GitHub staff, test these changes with github/github-ui using the integration workflow. Check the integration testing docs for step-by-step instructions. Or, apply the integration-tests: skipped manually label to skip these checks.

To publish a canary release for integration testing, apply the Canary Release label to this PR.

Copy link
Copy Markdown
Member

@joshblack joshblack left a comment

Choose a reason for hiding this comment

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants