fetchception

fetch mocking

Usage no npm install needed!

<script type="module">
  import fetchception from 'https://cdn.skypack.dev/fetchception';
</script>

README

fetchception

npm version Build Status

Mock out network traffic from fetch in tests. Experiment based on idea from fileception and httpception. Utilizing the great modelling, inspection and diffing of HTTP conversations from messy and unexpected-messy.

const fetchception = require("fetchception");
const assert = require("assert");

it("should cleanly mock out fetch in the test", () =>
  fetchception(
    [
      {
        request: "/api/foo",
        response: {
          statusCode: 200,
          body: { foo: "bar" },
        },
      },
    ],
    () => {
      return fetch("/api/foo")
        .then((res) => res.json())
        .then((res) => {
          assert.strictEqual(res.foo, "bar");
        });
    }
  ));

When the test is done, the fetch global will automatically be restored.