Coder Social home page Coder Social logo

mockster's Introduction

Mockster

Mockster on NPM Mockster on TravisCI

A library for mocking fetch responses.

Install

npm i --save mockster
import mockster from 'mockster';

Note: window.fetch is automatically mocked when including this library. It's a good idea to only include mocks in a mock build so this usually isn't an issue. If you need them the library also exports fetchHook and fetchUnhook, as well as fetchUnmock(url, method).

Tip: You can include mocks in a webpack build with no extra config. Create a mocks/index.js file at your root and run npm start -- mocks.

Usage

Supported methods: delete, get, patch, post, put.

mockster.get('/film/:title?rating=:rating', (url, params) => {
  // params.title = 'batman'
  // params.rating = '8'
  return { something: 'to return' };
});

fetch('/film/batman?rating=8');

You can also return error responses using status and body, as well as a headers object if your response isn't JSON.

mockster.post('/project/create', (url, params, options) => {
  if (options.body.name === 'Farting investigation') {
    return {
      status: 409,
      body: 'That project already exists',
    };
  }

  // An object without a `body` property is considered to be the body,
  // and the status will be 200 'OK'.
  return {
    ...options.body,
    id: '3250235H6',
  };
});

fetch('/project/create', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    name: 'Hubot',
  }),
});

You can also return a Promise if you want to delay the response or do async work:

mockster.get('/hello', () =>
  new Promise((resolve) => {
    setTimeout(() => resolve({ hello: 'world' }), 300);
  }));

Parameters

Parameters are avaiable in the second arguments (params). URLs support params, splats, and optional segments.

Example Description
:name a parameter to capture from the route up to /, ?, or end of string
*splat a splat to capture from the route up to ? or end of string
() Optional group that doesn't have to be part of the query. Can contain nested optional groups, params, and splats
anything else free form literals

Some examples:

  • /some/(optional/):thing
  • /users/:id/comments/:comment/rating/:rating
  • /*a/foo/*b
  • /books/*section/:title
  • /books?author=:author&subject=:subject

Matching based on options

It is also possible to match based on options as well as the URL. A deep equal will be performed on the options you wish to compare:

mockster.post('project/create', (url, params, options) => {
  return 'your response';
}, {
  body: {
    name: 'Billy Bob',
  }
});

Not all options will pass an equality check (for example FormData), to this effect options can also be a function:

mockster.post('project/create', (url, params, options) => {
  return 'your response';
}, options => options.body.name.startsWith('Billy'));

mockster's People

Contributors

sekoyo avatar

Stargazers

Anne Thorpe avatar  avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.