fetchception

2.0.1 • Public • Published

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.

Readme

Keywords

none

Package Sidebar

Install

npm i fetchception

Weekly Downloads

167

Version

2.0.1

License

ISC

Unpacked Size

42.2 kB

Total Files

20

Last publish

Collaborators

  • papandreou
  • gustavnikolaj