Skip to content

test(cookbook): add unit tests for 6 recipes and extract shared test utility#3632

Draft
itsjustriley wants to merge 5 commits intomainfrom
test/recipe-unit-tests
Draft

test(cookbook): add unit tests for 6 recipes and extract shared test utility#3632
itsjustriley wants to merge 5 commits intomainfrom
test/recipe-unit-tests

Conversation

@itsjustriley
Copy link
Copy Markdown
Contributor

@itsjustriley itsjustriley commented Mar 26, 2026

WHY are these changes introduced?

Fixes https://github.com/Shopify/developer-tools-team/issues/1130
Fixes https://github.com/Shopify/developer-tools-team/issues/1131
Fixes https://github.com/Shopify/developer-tools-team/issues/1132
Fixes https://github.com/Shopify/developer-tools-team/issues/1133

Six cookbook recipes had no unit tests, meaning structural regressions (broken exports, missing config, invalid CSP domains) and behavioral regressions (broken query minification, incorrect locale parsing, wrong type guards) would only be caught by slower E2E tests or in production.

WHAT is this pull request doing?

Recipe tests (6 recipes, 68 tests across 7 files):

  • Fix setup command #1130 — Express: validate server.mjs structure, Express setup, renderToPipeableStream, vite config, and AppSession class
  • fix skeleton inter-route imports #1131 — GTM: validate recipe structure, CSP domain configuration, GoogleTagManager dataLayer/useAnalytics integration
  • Fix useMoney with long language code #1132 — Partytown: test maybeProxyRequest pure function (script proxying, non-script bypass, already-proxied bypass) and partytownAtomicHeaders (COOP/COEP headers)
  • Check cache api #1133 — Third-party-api: test minifyQuery, display name extraction via OPERATION_NAME_PATTERN, and createRickAndMortyClient query method error handling (success, data.error, HTTP error)
  • Combined-listings: test isCombinedListing type guard with all input types (valid product, missing tag, null, undefined, non-object), settings snapshot, and filter query generation
  • Markets: test all 6 pure i18n utilities — getLocaleFromRequest, getPathWithoutLocale, localeMatchesPrefix, normalizePrefix, findLocaleByPrefix, cleanPath — plus exported constants

Shared infrastructure:

  • Extract loadRecipePatch utility into cookbook/recipes/__test-utils__/index.ts to replace duplicated findPatchFile helpers
  • Export OPERATION_NAME_PATTERN constant from createRickAndMortyClient.server.ts so the test imports the real regex instead of duplicating it

Recipes not tested (and why):

  • b2b, bundles, subscriptions — React components + GraphQL fragments only
  • legacy-customer-account-flow — route files only
  • custom-cart-method, infinite-scroll — patches only, no ingredient source files
  • metaobjects — parseSection depends on @shopify/hydrogen's parseMetafield
  • multipass — multipassify.server.ts needs crypto-js; multipass.ts needs fetch + browser env

HOW to test your changes?

cd cookbook && npx vitest run recipes --pool forks
# 68 passed, 7 files

Full cookbook suite: npx vitest run --pool forks — 155 passed, 13 files

Checklist

  • I've read the Contributing Guidelines
  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've added a changeset if this PR contains user-facing or noteworthy changes
  • I've added tests to cover my changes
  • I've added or updated the documentation

itsjustriley and others added 2 commits March 26, 2026 11:59
… recipes

#1130: Express recipe tests — validate server.mjs structure (Express
setup, express.static middleware, EADDRINUSE handling), entry.server
streaming change, vite config oxygen removal, and AppSession class.

#1131: GTM recipe tests — validate recipe structure, CSP domain
configuration in entry.server patch, GoogleTagManager component
dataLayer integration and useAnalytics usage.

#1132: Partytown recipe tests — test maybeProxyRequest pure function
(script proxying, non-script bypass, already-proxied bypass) and
partytownAtomicHeaders (COOP/COEP headers).

#1133: Third-party-api recipe tests — test minifyQuery pure function
(comment removal, whitespace collapsing, structure preservation) and
display name extraction regex pattern.

Partytown and third-party-api have real pure functions to unit test.
GTM and express are primarily template/config, so tests validate
structural integrity and key configuration properties.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Export minifyQuery from createRickAndMortyClient.server.ts and import
  in test (eliminates duplicated implementation)
- Mock @Shopify/hydrogen to resolve import in test context
- Replace ! non-null assertions with proper narrowing guards
- Use beforeAll + shared variables to avoid repeated file reads
- Import readdir directly instead of dynamic reimport
- Use method signature patterns for more precise string matching

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@shopify
Copy link
Copy Markdown
Contributor

shopify bot commented Mar 26, 2026

Oxygen deployed a preview of your test/recipe-unit-tests branch. Details:

Storefront Status Preview link Deployment details Last update (UTC)
Skeleton (skeleton.hydrogen.shop) ✅ Successful (Logs) Preview deployment Inspect deployment March 27, 2026 3:01 PM

Learn more about Hydrogen's GitHub integration.

itsjustriley and others added 3 commits March 26, 2026 12:15
Three changes to improve test quality:

1. Express AppSession test: scope method assertions to extracted class body
   rather than the entire server.mjs file. Previously, get( could match
   app.get(/favicon.ico...) unrelated to AppSession, meaning the test
   could pass even if AppSession lost its get() method.

2. GTM test: extract findPatchFile helper (matching express test pattern)
   instead of inlining the patch-file lookup. Moves readdir to beforeAll
   for consistency and reusability.

3. Partytown test: rename misleading test from returns the original URL
   for non-proxy domains to proxies all domains when nonProxyDomains is
   empty - the test asserts the URL IS proxied, not that it is returned
   unchanged.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Replace `classMatch![0]` non-null assertion with proper narrowing guard
  (`if (!classMatch) throw`) in express.test.ts, removing the last remaining
  `!` assertion that the earlier commit claimed to have eliminated.

- Add comments to `minifyQuery` negative assertions (`.not.toContain('#')`,
  `.not.toMatch(/\s{2,}/)`) explaining they are supplementary to the exact
  `.toBe()` match assertions that serve as the primary regression guards.

- Expand `extractDisplayName` regex comment to explain the intentional
  duplication from `createRickAndMortyClient.server.ts` and why exporting
  the regex is not worth the added public API surface.

- Improve `findPatchFile` error messages in express and GTM tests to include
  the actual files found in the patches directory, enabling faster debugging
  when a patch file is renamed.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Three review fixes from consensus review:
- Export OPERATION_NAME_PATTERN constant from createRickAndMortyClient.server.ts
  to eliminate regex duplication between source and test
- Extract shared loadRecipePatch utility into __test-utils__/index.ts to
  replace duplicated findPatchFile helpers in express and GTM tests
- Fix misleading test name "removes oxygen plugin" to accurately describe
  the structural assertion

New coverage additions:
- createRickAndMortyClient query method: 3 tests covering success, data.error,
  and HTTP error response branches
- combined-listings recipe: 9 tests covering isCombinedListing type guard with
  all input types, settings snapshot, and filter query generation
- markets recipe i18n utilities: 25 tests covering all 6 pure functions
  (getLocaleFromRequest, getPathWithoutLocale, localeMatchesPrefix,
  normalizePrefix, findLocaleByPrefix, cleanPath) plus exported constants

68 recipe tests across 7 files, 155 total cookbook tests, all passing.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
@itsjustriley itsjustriley changed the title test: add unit tests for express, GTM, partytown, and third-party-api recipes test(cookbook): add unit tests for 6 recipes and extract shared test utility Mar 27, 2026
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.

1 participant