Getting started

Install

npm install stubfetch

Requires Node.js 18+.

Minimal example

import { GhostEnv, github } from "stubfetch";

const env = new GhostEnv({
  seed: 1,
  providers: [
    github({
      issues: [{ repo: "acme/api", title: "First issue", body: "Details" }],
    }),
  ],
});

const res = await env.fetch("https://api.github.com/repos/acme/api/issues");
console.assert(res.status === 200);
const issues = await res.json();
console.log(issues[0].title);

How routing works

  1. You pass an ordered list of providers to GhostEnv.
  2. Each fetch walks providers in order; the first provider that handles the URL wins.
  3. If none match, fetch throws.

Recording calls

After making calls, inspect history:

env.calls();                        // all CallRecord
env.calls("github");                // filter by provider name
env.wasCalled("github", {
  method: "GET",
  pathIncludes: "/issues",
});

Multiple presets

Combine providers for integration-style tests:

new GhostEnv({
  seed: 0,
  providers: [
    github({ issues: [] }),
    stripe({ customers: [{ email: "[email protected]" }] }),
  ],
});

Order matters: the first provider that handles a URL wins.

Next steps