Files
supabase/packages/common/helpers.test.ts
Drew McLellan 773b4f767c fix(common): skip the reduced-motion query when matchMedia is missing (#53613)
## Problem

`Marketing site tests / build` fails on PRs that touch www, with
`window.matchMedia is not a function` in `lib/sentry-capture.test.tsx`.

`packages/common/helpers.ts` calls `window.matchMedia` when it is
imported. jsdom does not implement it. #53294 made
`common/consent-state` import `posthog-client`, which reaches
`helpers.ts` through `telemetry-utils`. The www `sentry-browser` test
project runs in jsdom without a `matchMedia` stub, so it now crashes on
import.

The break went unnoticed on `master` because the www workflow only runs
on pull requests, and its path filter listed just two files in
`packages/common`.

This is the same problem several other places already work around:
`consent-state.ts` duplicates `ensurePlatformSuffix` to avoid the
import, and the `packages/common` and `kb` tests stub
`window.matchMedia` before importing.

## Solution

- Guard the call in `helpers.ts` with `typeof window.matchMedia ===
'function'`, so `useReducedMotion` returns `false` where the API is
missing. Browsers are unaffected.
- Add `packages/common/helpers.test.ts`. It imports `helpers` and
`consent-state` in jsdom without `matchMedia`, and checks that the query
is still read where it exists. Both import tests fail on `master` with
this error.
- Run the Marketing site tests on changes to `packages/common/**/*.ts*`
and its `package.json`, since www imports it widely. This part is
independent of the fix and can be split out if you prefer.

The `helpers.ts` change is identical to the one in #53589, so the two
merge cleanly whichever lands first. This earlier attempt, #53596, was
closed.

Not changed: the per-test `matchMedia` stubs. They're harmless, and can
be removed as a follow-up.

## Review instructions

1. From `packages/common`, run `pnpm exec vitest run helpers.test.ts`
and check that all 3 tests pass.
2. Run `git checkout origin/master -- packages/common/helpers.ts` and
run it again. Two tests should fail with `window.matchMedia is not a
function`. Then run `git checkout HEAD -- packages/common/helpers.ts`.
3. Check that `Marketing site tests / build` passes on this PR and ran
because of the `packages/common` path.

## Checklist

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill (not applicable)
2026-11-18 14:30:07 +00:00

42 lines
1.3 KiB
TypeScript

// @vitest-environment jsdom
import { afterEach, describe, expect, it, vi } from 'vitest'
// `helpers.ts` reads `window.matchMedia` when it is imported, so each test sets the environment up
// first and then imports a fresh copy of the module.
async function importHelpers() {
vi.resetModules()
return import('./helpers')
}
afterEach(() => {
vi.unstubAllGlobals()
})
describe('importing helpers', () => {
it('works where matchMedia is missing, as in jsdom', async () => {
expect(window.matchMedia).toBeUndefined()
await expect(importHelpers()).resolves.toMatchObject({ isBrowser: true })
})
// The chain that broke the www `sentry-browser` tests: consent-state -> posthog-client ->
// telemetry-utils -> helpers.
it('lets consent-state load where matchMedia is missing', async () => {
expect(window.matchMedia).toBeUndefined()
vi.resetModules()
await expect(import('./consent-state')).resolves.toHaveProperty('hasConsented')
})
it('reads the reduced-motion preference where matchMedia exists', async () => {
const matchMedia = vi.fn().mockReturnValue({
matches: true,
addEventListener: () => {},
removeEventListener: () => {},
})
vi.stubGlobal('matchMedia', matchMedia)
await importHelpers()
expect(matchMedia).toHaveBeenCalledWith('(prefers-reduced-motion: reduce)')
})
})