Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jest-fetch-mock

Package Overview
Dependencies
Maintainers
1
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-fetch-mock

fetch mock for jest

  • 2.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
730K
decreased by-43.51%
Maintainers
1
Weekly downloads
 
Created

What is jest-fetch-mock?

The jest-fetch-mock npm package is a Jest plugin that allows you to easily mock fetch calls in your tests. It provides a simple and flexible way to control the behavior of fetch requests, making it easier to test code that relies on network requests.

What are jest-fetch-mock's main functionalities?

Mocking fetch responses

This feature allows you to mock the response of a fetch call. In the example, the fetch call to '/some-endpoint' is mocked to return a JSON object with data '12345'.

const fetchMock = require('jest-fetch-mock');
fetchMock.enableMocks();

// Mock a simple fetch response
fetchMock.mockResponseOnce(JSON.stringify({ data: '12345' }));

// Example test
it('fetches data successfully', async () => {
  const response = await fetch('/some-endpoint');
  const data = await response.json();
  expect(data).toEqual({ data: '12345' });
});

Mocking fetch errors

This feature allows you to mock fetch errors. In the example, the fetch call to '/error-endpoint' is mocked to throw an error with the message 'Failed to fetch'.

const fetchMock = require('jest-fetch-mock');
fetchMock.enableMocks();

// Mock a fetch error
fetchMock.mockRejectOnce(new Error('Failed to fetch'));

// Example test
it('handles fetch error', async () => {
  try {
    await fetch('/error-endpoint');
  } catch (error) {
    expect(error).toEqual(new Error('Failed to fetch'));
  }
});

Mocking multiple fetch calls

This feature allows you to mock multiple fetch calls in sequence. In the example, the first fetch call to '/first-endpoint' returns a JSON object with data 'first', and the second fetch call to '/second-endpoint' returns a JSON object with data 'second'.

const fetchMock = require('jest-fetch-mock');
fetchMock.enableMocks();

// Mock multiple fetch responses
fetchMock.mockResponses(
  [JSON.stringify({ data: 'first' }), { status: 200 }],
  [JSON.stringify({ data: 'second' }), { status: 200 }]
);

// Example test
it('fetches multiple data successfully', async () => {
  const response1 = await fetch('/first-endpoint');
  const data1 = await response1.json();
  expect(data1).toEqual({ data: 'first' });

  const response2 = await fetch('/second-endpoint');
  const data2 = await response2.json();
  expect(data2).toEqual({ data: 'second' });
});

Other packages similar to jest-fetch-mock

Keywords

FAQs

Package last updated on 04 Apr 2019

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc