Skip to content

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

License

Notifications You must be signed in to change notification settings

Larney11/mock-fetch-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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);
   });
});

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published