{"id":20807540,"url":"https://github.com/clo4/deno_mock_fetch","last_synced_at":"2026-06-11T03:01:10.937Z","repository":{"id":48487342,"uuid":"386912013","full_name":"clo4/deno_mock_fetch","owner":"clo4","description":"Use URLPattern syntax to mock fetch responses","archived":false,"fork":false,"pushed_at":"2021-11-07T09:17:19.000Z","size":11,"stargazers_count":12,"open_issues_count":4,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-03-30T02:45:15.485Z","etag":null,"topics":["deno","fetch-mock","mock-fetch","typescript"],"latest_commit_sha":null,"homepage":"https://doc.deno.land/https/deno.land/x/mock_fetch/mod.ts","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/clo4.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-07-17T10:51:31.000Z","updated_at":"2025-10-07T03:38:30.000Z","dependencies_parsed_at":"2022-09-01T03:50:37.058Z","dependency_job_id":null,"html_url":"https://github.com/clo4/deno_mock_fetch","commit_stats":null,"previous_names":["separaterecords/deno_mock_fetch"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/clo4/deno_mock_fetch","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clo4%2Fdeno_mock_fetch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clo4%2Fdeno_mock_fetch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clo4%2Fdeno_mock_fetch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clo4%2Fdeno_mock_fetch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/clo4","download_url":"https://codeload.github.com/clo4/deno_mock_fetch/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clo4%2Fdeno_mock_fetch/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34180147,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-11T02:00:06.485Z","response_time":57,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deno","fetch-mock","mock-fetch","typescript"],"created_at":"2024-11-17T19:38:35.253Z","updated_at":"2026-06-11T03:01:10.880Z","avatar_url":"https://github.com/clo4.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# deno_mock_fetch\n\nAn _extremely_ simple way to mock `globalThis.fetch`.\n\n[Read the documentation][docs], or see \"Usage\" below.\n\n[docs]: https://doc.deno.land/https/deno.land/x/mock_fetch/mod.ts\n\n## Usage\n\n### 1. Setup\n\nImport the library and install the mock. Any fetches after calling `install()`\nwill throw an error if you haven't explicitly added a mock for that route.\n\n```typescript\nimport * as mf from \"https://deno.land/x/mock_fetch@0.3.0/mod.ts\";\n\n// Replaces globalThis.fetch with the mocked copy\nmf.install();\n```\n\n\u003cbr\u003e\n\n### 2. Mocking routes\n\nCall `mock` with a route (optionally starting with a method specifier, eg.\n`DELETE@`) and a function (can be async). Whenever that route is fetched, the\nfunction will be executed and the response will be returned.\n\nThe route uses [URLPattern], which allows you to match patterns and wildcards.\n\n**Only the path name will be used to match a handler**, so you can use literally\nanything for the host when fetching.\n\n[URLPattern]: https://github.com/WICG/urlpattern/blob/main/explainer.md#web-apis\n\n```typescript\nmf.mock(\"GET@/api/hello/:name\", (_req, params) =\u003e {\n  return new Response(`Hello, ${params[\"name\"]}!`, {\n    status: 200,\n  });\n});\n\nconst res = await fetch(\"https://localhost:1234/api/hello/SeparateRecords\");\nconst text = await res.text(); //=\u003e \"Hello, SeparateRecords!\"\n```\n\n\u003cbr\u003e\n\n### 3. Teardown\n\nYou can remove a single route's handler with `remove`, or reset all handlers\nwith `reset`. Once the handler has been removed, that route will go back to\nthrowing.\n\n```typescript\nmf.remove(\"GET@/api/hello/:name\");\n// OR: mf.reset()\n\nawait fetch(\"https://example.com/api/hello/world\");\n// UnhandledRouteError: GET /api/hello/world (0 routes have handlers)\n```\n\nTo restore the original `fetch`, call `uninstall`.\n\n```typescript\nmf.uninstall();\n```\n\n\u003cbr\u003e\n\n## Advanced usage\n\nYou don't have to replace the global fetch, or even have global state, by using\nthe `sandbox` function. The returned object provides the same methods as the\nmodule (minus install \u0026 uninstall). Calling these methods will not alter global\nstate.\n\n```typescript\n// Ky is an excellent and easy-to-use fetch wrapper.\nimport ky from \"https://cdn.skypack.dev/ky?dts\";\n\n// This object can also be destructured.\nconst mockFetch = mf.sandbox();\n\n// Make a ky instance that uses mocked fetch - never touching the global fetch.\n// Using a prefix URL means you won't need to write the URL every time.\nconst myKy = ky.extend({\n  fetch: mockFetch.fetch,\n  prefixUrl: \"https://anyurlyouwant.com\",\n});\n\n// Now you can mock the routes like normal\nmockFetch.mock(\"PUT@/blog/posts\", async (req) =\u003e {\n  return new Response(/* ... */);\n});\n\nmyKy.put(\"blog/posts\", {\n  /* ... */\n});\n```\n\nYou can destructure it, too.\n\n```typescript\nconst { fetch, mock, remove, reset} = mf.sandbox();\n```\n\n\u003cbr\u003e\n\n## Credits\n\n**[@eliassjogreen]**'s tiny router ([source][router]) does the bulk of the work.\nIt's general-purpose, but works great for Deno Deploy.\n\n[@eliassjogreen]: https://github.com/eliassjogreen\n[router]: https://crux.land/router@0.0.5\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclo4%2Fdeno_mock_fetch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fclo4%2Fdeno_mock_fetch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclo4%2Fdeno_mock_fetch/lists"}