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
- You pass an ordered list of
providerstoGhostEnv. - Each
fetchwalks providers in order; the first provider that handles the URL wins. - If none match,
fetchthrows.
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
- Presets for URL patterns and config objects
- API reference for
db(),reset,snapshot, exports - Testing & chaos for
runEval/run_evaland failure injection