Socket
Socket
Sign inDemoInstall

fetch-mock-jest

Package Overview
Dependencies
331
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    fetch-mock-jest

Jest wrapper for fetch-mock, a comprehensive stub for fetch


Version published
Maintainers
1
Install size
6.25 MB
Created

Readme

Source

fetch-mock-jest

Wrapper around fetch-mock - a comprehensive, isomorphic mock for the fetch api - which provides an interface that is more idiomatic when working in jest

Installation

npm install -D fetch-mock-jest

global fetch

const fetchMock = require('fetch-mock-jest')

node-fetch

jest.mock('node-fetch', () => require('fetch-mock-jest').sandbox())
const fetchMock = require('node-fetch')

API

All the built in jest function inspection assertions can be used, e.g. expect(fetchMock).toHaveBeenCalledWith('http://example.com').

fetch.mockClear() can be used to reset the call history

fetch.mockReset() can be used to remove all configured mocks

All other jest methods for configuring mock functions are disabled as fetch-mock's own methods should always be used

fetchMock.mock.calls and fetchMock.mock.results are also exposed, giving access to manually inspect the calls.

The following custom jest expectation methods, proxying through to fetch-mock's inspection methods are also available. They can all be prefixed with the .not helper for negative assertions.

  • expect(fetchMock).toHaveFetched(filter, options)
  • expect(fetchMock).toHaveLastFetched(filter, options)
  • expect(fetchMock).toHaveNthFetched(n, filter, options)
  • expect(fetchMock).toHaveFetchedTimes(n, filter, options)
  • expect(fetchMock).toBeDone(filter)

filter and options are the same as those used by fetch-mock's inspection methods

TODO

These would also be useful jest extensions:

  • toHaveFetched
  • toHaveRespondedWith(object | status | string ) (using fetch-mock internals to convert to a response config, then use jest objectMatching)

Keywords

FAQs

Last updated on 19 Jan 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc