Git Product home page Git Product logo

fetch-mock-fixtures's Introduction

Build status Coverage Status Documentation Known Vulnerabilities PRs Welcome

Liqueur de Toile

Fetch-mock-fixtures (FMF)

While most of mockers for fetch are only meant to intercept and define the next response content, FMF goes far beyond and offers a wide range of tools for testing js apps.

What is featured :

  • BDD style syntax to configure server and fixtures in a more readable-friendly way. It's heavily inspired by Chai should assertion library,
  • Easy way to configure response on-the-fly, mutating responses from call to call...
  • Enhanced Request native object to automatically parse url details and query content with url-parse,
  • Powerful response presets and fixtures system to avoid writing the same things again and again and ease functional testing,
  • Easy access to the full history or requests/responses handled by the server since its first start or last reset,
  • Parametrized request thanks to path-to-regexp to enable dynamic fixtures routing in a few lines of code
  • and many more !

For instance, with FMF, you can do such things to quickly configure two fixtures in a mocha test (that will obviously succeed) :

import Server from 'fetch-mock-fixtures';

const server = new Server();

describe('Headers test', function() {
  before(() => server.start()) // Start intercepting fetch calls
  after(() => server.stop()) // Restore to normal behavior

  it('should use json headers', async function() {
    server
      .when // or .on
        .header('content-type').equal(/json/) // use a regexp here to avoid writing full header
        .respond.with.preset('200')
      .fallback.to.preset('400')

    let response = await fetch('/', {
      headers: {'content-type': 'application/json'}
    });

    response.status.should.equal(200);
  })
})

How FMF can ease API outgoing requests unit tests ?

FMF enables really quick response configuration that allows testing the outgoing request to set up different responses (see above example). You only have to check a response property (like status) instead of manually parsing request built by your app to validate it.

Furthermore, you can use the before and after hooks or body as a callback to alter response on very precise expectations.

How FMF can ease functional tests ?

In real life, scripts are often sending multiple requests to do their job. FMF removes the pain of handling multiple responses by easing their management. Let's see this example with a two steps authentication login. A bit verbose for what is actually doing but it aims to illustrate things :

import Server from 'fetch-mock-fixtures';

const server = new Server();

// Define on-the-fly fixtures to handle login tests
server
  .verbose(true) // Enable console login for each request/response/error
  .when
      .pathname.equal('/login')
      .method.equal('POST')
      .body('json').equal(body => body.username === 'foo')
    .respond.with
      .preset(401)
  .when
      .pathname.equal('/session')
      .method.equal('POST')
      .body('json').equal(token => body.authToken === '123')
    .respond.with
      .preset('json')
      .body({success: true, sessionToken: '456'})
  .fallback.to
    .preset(403)

describe('Login test suite', function() {
  before(() => server.start())
  after(() => server.stop())

  it('should login', async function() {
    await triggerTheLoginLogic('foo');
    await sendTheTokenLogic('123');
    logged.should.be.true;
  })

  it('should fail login on username', async function() {
    await triggerTheLoginLogic('bar');
    logged.should.be.false;
  })

  it('should fail login on token', async function() {
    await triggerTheLoginLogic('foo');
    await sendTheTokenLogic('hacked!');
    logged.should.be.false;
  })
})

We're not only sending back data to the app but also checking outgoing requests at the same time because the answer will only be sent if calling the right url with the right method and the right data. with and to are only optional sugars to improve human readability.

Last not least, you can easily deploy url-based routing to use your "real" data inside each tests instead of providing fake data and get rid of on-the-fly fixtures (see dynamic fixtures examples).

When to use FMF ?

At any time πŸ˜„

Nevertheless, FMF will truly give its best with any testing framework (Mocha, Jasmine, Junit...) that allows to automate operations between each tests like start, stop or reset the server.

Installation

Installation can easily be done through NPM or Yarn. Sinon is required by FMF to stub fetch but is not included in the bundle. It must be installed as well if not already present.

npm install sinon fetch-mock-fixtures --save-dev

yarn add sinon fetch-mock-fixtures --dev

FMF should be installed as a dev dependency. It is not meant to be used as an in-app offline mode feature.

Note : FMF is built upon Promise, Proxy and fetch API (Request, Headers, Response) that are available in all modern browsers. If you intend to run tests on older browsers (IE) or versions, you may need to polyfill them. Here's some available tools you can use :

Full documentation and API reference

Please pay visit to the docs pages.

Bugs and improvements

Any bugs and issues can be filed on the github repository.

You are free and very welcome to fork the project and submit any PR to fix or improve FMF.

I'm especially interested for good will who wish to improve query matcher processors to provide more tools to evaluate query and choose the right response.

Changelog

  • 2.2.0 : Add global preset configuration within server instance and throw behavior for fixture
  • 2.1.0 : Add history logging and verbose mode
  • 2.0.0 : BREAKING CHANGE - A brand new FMF highly not compatible with previous version
  • 1.0.1 : Add requests history and possibility to set up different responses based on requests order. Add delay as a response parameter into fixture.

fetch-mock-fixtures's People

Contributors

dependabot[bot] avatar greenkeeper[bot] avatar liqueurdetoile avatar snyk-bot avatar

Watchers

 avatar

fetch-mock-fixtures's Issues

An in-range update of webpack is breaking the build 🚨

The devDependency webpack was updated from 4.35.3 to 4.36.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

webpack is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Release Notes for v4.36.0

Features

  • SourceMapDevToolPlugin append option now supports the default placeholders in addition to [url]
  • Arrays in resolve and parser options (Rule and Loader API) support backreferences with "..." when overriding options.
Commits

The new version differs by 42 commits.

  • 95d21bb 4.36.0
  • aa1216c Merge pull request #9422 from webpack/feature/dot-dot-dot-merge
  • b3ec775 improve merging of resolve and parsing options
  • 53a5ae2 Merge pull request #9419 from vankop/remove-valid-jsdoc-rule
  • ab75240 Merge pull request #9413 from webpack/dependabot/npm_and_yarn/ajv-6.10.2
  • 0bdabf4 Merge pull request #9418 from webpack/dependabot/npm_and_yarn/eslint-plugin-jsdoc-15.5.2
  • f207cdc remove valid jsdoc rule in favour of eslint-plugin-jsdoc
  • 31333a6 chore(deps-dev): bump eslint-plugin-jsdoc from 15.3.9 to 15.5.2
  • 036adf0 Merge pull request #9417 from webpack/dependabot/npm_and_yarn/eslint-plugin-jest-22.8.0
  • 37d4480 Merge pull request #9411 from webpack/dependabot/npm_and_yarn/simple-git-1.121.0
  • ce2a183 chore(deps-dev): bump eslint-plugin-jest from 22.7.2 to 22.8.0
  • 0beeb7e Merge pull request #9391 from vankop/create-hash-typescript
  • bf1a24a #9391 resolve super call discussion
  • bd7d95b #9391 resolve discussions, AbstractMethodError
  • 4190638 chore(deps): bump ajv from 6.10.1 to 6.10.2

There are 42 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of semantic-release is breaking the build 🚨

The devDependency semantic-release was updated from 15.13.20 to 15.13.21.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

semantic-release is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Release Notes for v15.13.21

15.13.21 (2019-08-12)

Bug Fixes

  • package: update hosted-git-info to version 3.0.0 (391af98)
Commits

The new version differs by 3 commits.

  • 391af98 fix(package): update hosted-git-info to version 3.0.0
  • d45d8b6 docs: fix typo
  • 519df0d chore: remove commitizen from our dependencies

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of url-parse is breaking the build 🚨

The dependency url-parse was updated from 1.4.6 to 1.4.7.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

url-parse is a direct dependency of this project, and it is very likely causing it to break. If other packages depend on yours, this update is probably also breaking those in turn.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Commits

The new version differs by 4 commits.

  • 0cde3dc [dist] 1.4.7
  • 96662a3 [pkg] Update querystringify and other deps (#176)
  • 1809266 chore(package): update nyc to version 14.0.0 (#174)
  • c3b3485 [test] Fix test

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of karma-chrome-launcher is breaking the build 🚨

The devDependency karma-chrome-launcher was updated from 3.0.0 to 3.1.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

karma-chrome-launcher is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Release Notes for v3.1.0

Features

  • add --disable-dev-shm-usage flag to headless (137005d)
FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of karma-webpack is breaking the build 🚨

The devDependency karma-webpack was updated from 4.0.0-rc.6 to 4.0.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

karma-webpack is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Release Notes for v4.0.0

4.0.0 (2019-06-07)

Bug Fixes

chore

BREAKING CHANGES

  • default: minimum required nodejs version is 8.9
Commits

The new version differs by 8 commits ahead by 8, behind by 2.

  • 9138b13 chore(release): 4.0.0
  • 74e526f chore(default): update (#412)
  • a2b044d refactor(package): add more keywords for npm (#411)
  • ca7c5e2 chore(package): drop node v6 support (#409)
  • bef8bb6 refactor(karma-webpack): fix linting error (#410)
  • 63cfd78 fix(karma-webpack): Regression in multi-compiler mode (#390) (#391)
  • 11cb4fb chore(release): 4.0.0-rc.6
  • ed66799 chore(package-lock): fix low severity vulnerabilities

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of sinon is breaking the build 🚨

The devDependency sinon was updated from 8.0.4 to 8.1.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

sinon is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Commits

The new version differs by 8 commits.

  • 384732e Update docs/changelog.md and set new release id in docs/_config.yml
  • dbfe691 Add release documentation for v8.1.0
  • cb8f3b7 8.1.0
  • 3c6fd39 Update CHANGELOG.md and AUTHORS for new release
  • cd27e25 Update @sinonjs/samsam to latest
  • 7c79e80 Merge pull request #2199 from RealZogger/getCall-negative
  • d851a5e Add clarifying comment for negative indices
  • 6d59744 Support negative indices in getCall

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of babel7 is breaking the build 🚨

There have been updates to the babel7 monorepo:

    • The devDependency @babel/core was updated from 7.4.3 to 7.4.4.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

This monorepo update includes releases of one or more dependencies which all belong to the babel7 group definition.

babel7 is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of babel7 is breaking the build 🚨

There have been updates to the babel7 monorepo:

    • The devDependency @babel/core was updated from 7.7.7 to 7.8.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

This monorepo update includes releases of one or more dependencies which all belong to the babel7 group definition.

babel7 is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of eslint-loader is breaking the build 🚨

The devDependency eslint-loader was updated from 3.0.1 to 3.0.2.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

eslint-loader is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Release Notes for v3.0.2

3.0.2 (2019-09-27)

Bug Fixes

Commits

The new version differs by 3 commits.

  • fce6f88 chore(release): 3.0.2
  • 7d1d1fe fix: check if contain results (#300)
  • 16e9ccf fix: ensure output file path (#299)

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

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.