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

@types/fetch-mock

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@types/fetch-mock

TypeScript definitions for fetch-mock

  • 7.3.8
  • ts4.5
  • ts4.6
  • ts4.7
  • ts4.8
  • ts4.9
  • ts5.0
  • ts5.1
  • ts5.2
  • ts5.3
  • ts5.4
  • ts5.5
  • ts5.6
  • ts5.7
  • ts5.8
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
202K
increased by5.18%
Maintainers
1
Weekly downloads
 
Created

What is @types/fetch-mock?

@types/fetch-mock provides TypeScript type definitions for the fetch-mock library, which is used to mock HTTP requests in tests. This allows developers to simulate different responses from the fetch API, making it easier to test how their code handles various scenarios.

What are @types/fetch-mock's main functionalities?

Mocking a simple GET request

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

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

fetch('/api/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 shows how to mock a POST request to '/api/submit' and return different responses based on the request body.

fetchMock.post('/api/submit', (url, options) => {
  if (options.body === JSON.stringify({ name: 'test' })) {
    return { status: 'success' };
  } else {
    return { status: 'error' };
  }
});

fetch('/api/submit', { method: 'POST', body: JSON.stringify({ name: 'test' }) })
  .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 shows how to mock a GET request to '/api/delayed' and return a response after a 1-second delay.

fetchMock.get('/api/delayed', new Promise(resolve => setTimeout(() => resolve({ data: 'delayed data' }), 1000)));

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

Restoring fetch to its original state

This feature allows you to restore the fetch function to its original state after mocking. The code sample shows how to mock a GET request and then restore the fetch function.

fetchMock.get('/api/data', { data: 'sample data' });
fetchMock.restore();

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

Other packages similar to @types/fetch-mock

FAQs

Package last updated on 07 Nov 2023

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