Socket
Socket
Sign inDemoInstall

fetch-mock-jest

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

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
Weekly downloads
205K
decreased by-0.06%
Maintainers
1
Weekly downloads
 
Created

What is fetch-mock-jest?

fetch-mock-jest is a library that allows you to mock fetch calls in your Jest tests. It provides a simple and powerful way to simulate different responses from the fetch API, making it easier to test your code's behavior under various conditions.

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

Mocking a simple fetch request

This feature allows you to mock a simple fetch request and provide a predefined response. In this example, the fetch call to '/api/data' will return a JSON object with data '12345'.

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

fetchMock.mockResponseOnce(JSON.stringify({ data: '12345' }));

fetch('/api/data')
  .then(res => res.json())
  .then(data => {
    console.log(data); // { data: '12345' }
  });

Mocking multiple fetch requests

This feature allows you to mock multiple fetch requests in sequence. Each call to fetch will return the next mocked response in the order they were defined.

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

fetchMock
  .mockResponseOnce(JSON.stringify({ data: 'first' }))
  .mockResponseOnce(JSON.stringify({ data: 'second' }));

fetch('/api/first')
  .then(res => res.json())
  .then(data => {
    console.log(data); // { data: 'first' }
  });

fetch('/api/second')
  .then(res => res.json())
  .then(data => {
    console.log(data); // { data: 'second' }
  });

Mocking fetch with different HTTP methods

This feature allows you to mock fetch requests based on different HTTP methods. In this example, a POST request to '/api/data' will return a specific response, while other methods will return a different response.

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

fetchMock.mockResponseOnce((req) => {
  if (req.method === 'POST') {
    return JSON.stringify({ message: 'Post request received' });
  }
  return JSON.stringify({ message: 'Other request' });
});

fetch('/api/data', { method: 'POST' })
  .then(res => res.json())
  .then(data => {
    console.log(data); // { message: 'Post request received' }
  });

Mocking fetch with different status codes

This feature allows you to mock fetch requests with different HTTP status codes. In this example, a fetch call to '/api/not-found' will return a 404 status, simulating a 'Not Found' error.

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

fetchMock.mockResponseOnce('', { status: 404 });

fetch('/api/not-found')
  .then(res => {
    if (!res.ok) {
      throw new Error('Not Found');
    }
    return res.json();
  })
  .catch(error => {
    console.error(error); // Error: Not Found
  });

Other packages similar to fetch-mock-jest

Keywords

FAQs

Package last updated on 24 Jan 2020

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