rewrite async test mocking example to use stub() with auto-restore#3119
Merged
Conversation
The previous example assigned to `globalThis.fetch` directly, which fails type-checking on stable (signature mismatch) and leaks the mock into every later test in the file. Switch to `stub` from `@std/testing/mock` with a `using` declaration so the original `fetch` is restored automatically when each test exits.
Contributor
Author
|
@bartlomieju ready for review when you have a moment. |
3 tasks
bartlomieju
approved these changes
May 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Testing async functions section in
examples/tutorials/testing.mddemonstrates how to mock
fetchwhen testing async functions. The previousexample had two problems that bite anyone who copies it:
globalThis.fetch = async (url: string) => …fails type-checking onstable Deno — the real
fetchacceptsURL | RequestInfo, not juststring. Runningdeno testagainst the existing snippet on Deno2.7.14 produced
TS2322: Type '(url: string) => Promise<Response>' is not assignable to type '…(input: URL | RequestInfo, init?: …)…'.fetch, so themock leaked into every later test in the file. The page didn't mention
this, so a reader following the pattern would silently break test
isolation.
This PR rewrites that single section to use
stubfrom@std/testing/mockbound witha
usingdeclaration. The stub matchesfetch's real signature (thefile type-checks) and the disposer restores the original when each test
scope exits. A
:::tipcallout explains why the old shape was wrong soreaders learn the rule, not just the snippet. Filenames also moved from
dashes to underscores per
runtime/contributing/style_guide.md.Verified by running both tests plus a third "fetch is restored" probe
against
denoland/deno:latest(2.7.14 — canary tag isn't published toDocker Hub right now):
Check async_test.tspassed and all 3 testspassed in 5ms. The output block in the page is the real
deno testoutput from that run.
Closes bartlomieju/orchid-inbox#46