An extremely simple window.fetch mock for Deno.


Import the library and install the mock. Any fetches after calling install() will throw an error if you haven't mocked the route.

import * as mf from "";

// Replaces window.fetch with the mocked copy

Call mock with a route (optionally starting with a method specifier, eg. DELETE@) and a function (can be async). Whenever that route is fetched, the function will be executed and the response will be returned.

mf.mock("GET@/api/hello/:name", (_req, match) => {
  return new Response(`Hello, ${match.params["name"]}!`, {
    status: 200,

const res = await fetch("https://localhost:1234/api/hello/SeparateRecords");
const text = await res.text(); //=> "Hello, SeparateRecords!"

You can remove all routes by calling reset().

// now, /api/hello/:name will throw again

And when you want to restore the un-mocked fetch, call uninstall().


Of course, you don't have to replace the global fetch function, you can access the mocked fetch directly via mockedFetch.

import ky from "";

const mockedKy = ky.extend({
  fetch: mf.mockedFetch,