npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mock-fetch-api

v1.0.7

Published

Mock http requests and responses using fetch API (or isomorphic-fetch). Straight forward functions makes it simple to create customizable and legible unit tests.

Downloads

193

Readme

mock-fetch-api

Mock http requests and responses using fetch API (or isomorphic-fetch). Straight forward functions makes it simple to create customizable and legible unit tests.

Installation

npm install --save-dev mock-fetch-api

Usage

var MockFetch = require('mock-fetch-api');

Functions

when()

The when() function sets the required method and URL.

when(method, URL)    
when('GET', 'http://mydomain.com')    

withExpectedHeader()

The withExpectedHeader() function sets the required headers.

withExpectedHeader(Header-Field-Name, Header-Field-Type)  
withExpectedHeader('Content-Type', 'application/json')  

otherwiseRespondWith()

The otherwiseRespondWith() function sets the response if the header specified with the withExpectedHeader() function does not correspond with the header passed to the fetch() function.

otherwiseRespondWith(status, statusText)  
otherwiseRespondWith(401, 'not authorised')  

respondWith()

The respondWith() function sets the response if all the requirements specified with the when() and withExpectedHeader() functions correspond with what is passed to the fetch() function.

respondWith(status, data)  
respondWith(401, '{"data":[{"text":"Hello"},{"text":"Goodbye"}]}')  

failNextCall()

The failNextCall() function forces the fetch to reject.

failNextCall()

Examples

Check out the 'tests' directory to view all examples. https://github.com/Larney11/mock-fetch-api/blob/master/tests/mock-fetch-api-test.js

The following examples are unit tests using Jest.


pit("can set a condition which is returned by fetch", () => {
  var MockFetch  = require('../MockFetch.js');

  MockFetch.when('GET', 'http://mydomain.com').respondWith(200, '"Hello World"');

  return fetch('GET', 'http://mydomain.com').then((response) => {
     return response.json();

  }).then((data) => {
     expect(data).toBe('Hello World');
  });
});


pit("only responds when matched correctly", () => {
  var MockFetch  = require('mock-fetch-api');

  MockFetch.when('GET', 'http://mydomain.com').respondWith(200, '"Hello World"');

  return fetch('http://mydomain.com', { method: 'PUT'}).then((response) => {

  expect(response.status).toBe(404);
  expect(response.statusText).toBe('Not Found');
  });
});    


pit("also checks for an expected header value", () => {
   var MockFetch  = require('../MockFetch.js');

   MockFetch.when('GET', 'http://mydomain.com')
      .withExpectedHeader('X-AuthToken','1234')
      .otherwiseRespondWith(401, "Not Authorized")
      .respondWith(200, '"Hello World"');

   return fetch('http://mydomain.com', { method: 'GET', headers: new Headers({
      'X-AuthToken':'1234'
   })}).then((response) => {
      expect(response.status).toBe(200);
   });
});


pit("fails when expected header is not set", () => {
   var MockFetch  = require('../MockFetch.js');

   MockFetch.when('GET', 'http://mydomain.com')
      .withExpectedHeader({'X-AuthToken':'1234'}).otherwiseRespondWith(401, "Not Authorized")
      .respondWith(200, '"Hello World"');

   return fetch('http://mydomain.com', { method: 'GET'}).then((response) => {

      expect(response.status).toBe(401);
      expect(response.statusText).toBe('Not Authorized');
   });
});


pit("can check for multiple expected headers", () => {
   var MockFetch  = require('../MockFetch.js');

   MockFetch.when('GET', 'http://mydomain.com')
      .withExpectedHeader('X-AuthToken','1234').otherwiseRespondWith(401, "Not Authorized")
      .withExpectedHeader('Content-Type', 'application/json').otherwiseRespondWith(404, "Not Found")
      .respondWith(200, '"Hello World"');

   return fetch('http://mydomain.com', { method: 'GET', headers: new Headers({
      'X-AuthToken':'1234',
      'Content-Type': 'application/json'
   })}).then((response) => {

      expect(response.status).toBe(200);
   });
});


pit("rejects the promise when simulating a failed network connection", () => {
   var MockFetch  = require('../MockFetch.js');

   MockFetch.when('GET', 'http://mydomain.com')
      .respondWith(200, '"Hello World"');

   MockFetch.failNextCall();
   return fetch('http://mydomain.com').then((response) => {
      expect(false).toBe(true);
   }, (error) => {
      expect(true).toBe(true);
   });
});