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

fetch-mock

Package Overview
Dependencies
Maintainers
1
Versions
231
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fetch-mock

Mock http requests made using fetch

  • 12.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
782K
increased by8.11%
Maintainers
1
Weekly downloads
 
Created

What is fetch-mock?

fetch-mock is a library for mocking HTTP requests made using the Fetch API. It allows developers to simulate different responses and behaviors for fetch calls, which is particularly useful for testing and development purposes.

What are fetch-mock's main functionalities?

Mocking a simple GET request

This feature allows you to mock a simple GET request. The code sample demonstrates how to mock a GET request to 'https://api.example.com/data' and return a JSON object with sample data.

const fetchMock = require('fetch-mock');
fetchMock.get('https://api.example.com/data', { data: 'sample data' });

fetch('https://api.example.com/data')
  .then(response => response.json())
  .then(data => console.log(data));

Mocking a POST request with specific body

This feature allows you to mock a POST request with a specific request body. The code sample demonstrates how to mock a POST request to 'https://api.example.com/submit' and return different responses based on the request body.

const fetchMock = require('fetch-mock');
fetchMock.post('https://api.example.com/submit', (url, options) => {
  if (options.body === JSON.stringify({ key: 'value' })) {
    return { status: 'success' };
  } else {
    return { status: 'error' };
  }
});

fetch('https://api.example.com/submit', {
  method: 'POST',
  body: JSON.stringify({ key: 'value' })
})
  .then(response => response.json())
  .then(data => console.log(data));

Mocking with delay

This feature allows you to mock a request with a delay. The code sample demonstrates how to mock a GET request to 'https://api.example.com/delayed' and return a response after a 1-second delay.

const fetchMock = require('fetch-mock');
fetchMock.get('https://api.example.com/delayed', new Promise(resolve => setTimeout(() => resolve({ data: 'delayed data' }), 1000)));

fetch('https://api.example.com/delayed')
  .then(response => response.json())
  .then(data => console.log(data));

Mocking with different response statuses

This feature allows you to mock requests with different HTTP response statuses. The code sample demonstrates how to mock a GET request to 'https://api.example.com/not-found' and return a 404 status.

const fetchMock = require('fetch-mock');
fetchMock.get('https://api.example.com/not-found', 404);

fetch('https://api.example.com/not-found')
  .then(response => {
    if (!response.ok) {
      throw new Error('Network response was not ok');
    }
    return response.json();
  })
  .catch(error => console.error('Fetch error:', error));

Other packages similar to fetch-mock

Keywords

FAQs

Package last updated on 15 Nov 2024

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