Skip to content

test: improve FetchAdapter binding regression tests#1138

Merged
yyyyaaa merged 1 commit into
mainfrom
yyyyaaa/improve-fetch-binding-tests
May 12, 2026
Merged

test: improve FetchAdapter binding regression tests#1138
yyyyaaa merged 1 commit into
mainfrom
yyyyaaa/improve-fetch-binding-tests

Conversation

@yyyyaaa
Copy link
Copy Markdown
Contributor

@yyyyaaa yyyyaaa commented May 12, 2026

Summary

Problem with the previous tests

PR #1130 added 4 tests for the .bind(globalThis) fix (PR #1128), but 3 of them had gaps:

Test Issue
TestFetchAdapter hand-written replica Tests a copy of FetchAdapter, not the generated code — won't catch template drift
new Function() regex extraction Fragile regex coupling + verbose inline type casts
Wrong-precedence negative test Tests a hand-built broken pattern, not the codebase — passes even if FetchAdapter is deleted

Verified empirically: the precedence bug variant fetchFn ?? createFetch().bind(globalThis) passed all 3 tests green (after jest -u to update snapshots).

New approach

loadGeneratedFetchAdapter() helper:

  1. Calls generateOrmClientFile() to get the real template output
  2. Compiles it with ts.transpileModule() to CommonJS
  3. Runs it in a node:vm sandbox with stubbed imports (createFetch, ./realtime)
  4. Returns the real FetchAdapter constructor

Two behavioral tests cover both ?? branches:

  • Default path: no injected fetch → createFetch() result must be bound to globalThis
  • Injected path: user-supplied fetch → must also be bound (catches the precedence bug)

Verification

Variant Regex Default-path Injected-path
Correct fix PASS PASS PASS
Fix fully reverted FAIL FAIL FAIL
Precedence bug FAIL PASS FAIL

Full suite: 20 suites, 347 tests, 123 snapshots — all green, 0 regressions.

Test plan

  • pnpm test in graphql/codegen — 347 tests pass
  • pnpm build across full monorepo — all packages compile
  • Verified tests fail on reverted fix
  • Verified tests fail on precedence-bug variant
  • Confirmed SDKs already regenerated by PR Updated fetch fix test and sdk #1130

🤖 Generated with Claude Code

…ness

Replace 3 weak tests from PR #1130 (hand-written replica, regex-extracted
new Function() eval, standalone wrong-precedence demo) with 2 behavioral
tests that compile and run the actual generated FetchAdapter in a node:vm
sandbox. The remaining regex test is kept as a static source-text guard.

The new tests exercise both branches of the generated constructor
expression `(fetchFn ?? createFetch()).bind(globalThis)` through the real
generated class, catching regressions that string-matching alone misses
(e.g. the precedence bug `fetchFn ?? createFetch().bind(globalThis)`).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@yyyyaaa yyyyaaa merged commit d81d1d9 into main May 12, 2026
37 checks passed
@yyyyaaa yyyyaaa deleted the yyyyaaa/improve-fetch-binding-tests branch May 12, 2026 13:52
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