Skip to content

Add PerfKit integration contract tests#3774

Open
dangayle wants to merge 1 commit into
mainfrom
add-analytics-perfkit-diagnostics
Open

Add PerfKit integration contract tests#3774
dangayle wants to merge 1 commit into
mainfrom
add-analytics-perfkit-diagnostics

Conversation

@dangayle
Copy link
Copy Markdown

@dangayle dangayle commented Jun 1, 2026

What this PR does

PerfKit.tsx had zero tests, so if someone accidentally changed a label, broke the load order, or stopped pinging PerfKit on navigation, nothing would catch it — it would just silently ship broken.

This PR adds a safety net. Three small things:

  1. Writes the rules down as tests. A new PerfKit.test.tsx checks the adapter still does exactly what it should — loads the right script URL, sets the right data-* labels, waits until the script is loaded before wiring anything up, and calls PerfKit on the five view events. If anyone breaks the contract later, the test fails.
  2. Names the PerfKit URL (PERF_KIT_URL) so the test can point at it, and so changing that URL becomes a deliberate, reviewed edit instead of a quiet one.
  3. Tidies one line — groups the data-* labels into a single object so the test can read them. No behavior change.

That's the whole PR. No new features, nothing users see, no version bump. It's purely a guard so Hydrogen can't accidentally break its own PerfKit wiring.

What the tests lock down

PerfKit.test.tsx (PerfKit is mocked at the network level — the real script is never downloaded):

  • Requests the pinned PerfKit SPA script URL.
  • Passes the required data-* labels exactly: data-application=hydrogen, data-spa-mode=true, parsed data-shop-id, data-storefront-id (from hydrogenSubchannelId), data-monorail-region=global, data-resource-timing-sampling-rate=100.
  • Parses the shop id out of the shop GID.
  • Registers Internal_Shopify_Perf_Kit.
  • Does not wire anything while the script is still loading, and not on error — only after it's done.
  • Wires all five view subscriptions (page_viewed, product_viewed, collection_viewed, search_viewed, cart_viewed).
  • Calls ready() exactly once, after wiring.
  • Doesn't wire twice across a loading → done transition (the re-render guard).
  • page_viewed → window.PerfKit.navigate(); product/collection/search/cart → setPageType(...).
  • Doesn't throw if window.PerfKit isn't there yet (script-load race).

About versions

The PerfKit script URL is pinned in PERF_KIT_URL and asserted exactly in the test:
https://cdn.shopify.com/shopifycloud/perf-kit/shopify-perf-kit-spa.min.js

Heads-up for reviewers: this URL is not stable across Hydrogen versions (older builds used a versioned URL like shopify-perf-kit-1.0.1.min.js; the current build uses the unversioned -spa bundle), and window.PerfKit exposes no runtime version. This PR just pins the current -spa build. Giving PerfKit an explicit version/capability signal is a separate, cross-team follow-up.

Scope / impact

  • Non-user-facing. PERF_KIT_URL is module-local — nothing new is exported from the package entrypoint. No runtime behavior change. No semver bump (empty changeset).
  • Touches 3 files: PerfKit.tsx, PerfKit.test.tsx (new), and an empty changeset.

Checks run

  • pnpm --dir packages/hydrogen typecheck
  • pnpm --dir packages/hydrogen test ✅ — 58 files, 548 tests
  • pnpm --dir packages/hydrogen exec vitest run src/analytics-manager/PerfKit.test.tsx ✅ — 16 tests
  • eslint + prettier on changed files ✅

Follow-ups (separate, not in this PR)

  • Skeleton e2e coverage proving the default Hydrogen app emits the contract end to end.
  • An explicit PerfKit version/capability signal so version checks can be deterministic.

Requested by Dan Gayle dan.gayle@shopify.com

@shopify
Copy link
Copy Markdown
Contributor

shopify Bot commented Jun 1, 2026

Oxygen deployed a preview of your add-analytics-perfkit-diagnostics branch. Details:

Storefront Status Preview link Deployment details Last update (UTC)
Skeleton (skeleton.hydrogen.shop) ✅ Successful (Logs) Preview deployment Inspect deployment June 2, 2026 1:21 AM

Learn more about Hydrogen's GitHub integration.

Lock down the deterministic Hydrogen <-> PerfKit contract with dedicated
tests, without changing runtime behavior:

- Export the pinned PERF_KIT_URL constant (shopify-perf-kit-spa.min.js) so
  the exact SPA script URL is asserted in tests; bumping it is now a
  deliberate, reviewed change.
- Memoize the data-* attributes object (stable identity; lets tests assert
  the exact attributes). No behavior change.
- PerfKit.test.tsx covers: SPA script URL; exact data-* attributes; shop GID
  parsing; storefront id from hydrogenSubchannelId; Internal_Shopify_Perf_Kit
  registration; no wiring while loading/error; wiring only after done; all
  five view subscriptions; ready() once after wiring; no double-wire across a
  loading->done transition; page_viewed -> navigate(); product/collection/
  search/cart -> setPageType(...); no throw when window.PerfKit is absent.

Non-user-facing, nothing exported from the package entrypoint, no semver
change (empty changeset).

Requested by Dan Gayle <dan.gayle@shopify.com>
@dangayle dangayle force-pushed the add-analytics-perfkit-diagnostics branch from d6e6b62 to da534b6 Compare June 2, 2026 01:17
@dangayle dangayle changed the title Add Hydrogen analytics and PerfKit diagnostic foundation Add PerfKit integration contract tests Jun 2, 2026
@dangayle dangayle marked this pull request as ready for review June 3, 2026 23:00
@dangayle dangayle requested a review from a team as a code owner June 3, 2026 23:00
@@ -0,0 +1,6 @@
---
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This change set can be removed as there is not package bumps!

Copy link
Copy Markdown
Collaborator

@andguy95 andguy95 left a comment

Choose a reason for hiding this comment

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

Remove change set

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants