Skip to content

Latest commit

 

History

History
38 lines (33 loc) · 1.18 KB

README.md

File metadata and controls

38 lines (33 loc) · 1.18 KB

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.