Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@types/fetch-mock
Advanced tools
TypeScript definitions for 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.
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));
Nock is a HTTP mocking and expectations library for Node.js. It allows you to intercept HTTP requests and mock responses. Compared to fetch-mock, Nock is more focused on Node.js and provides a more extensive set of features for mocking HTTP requests.
Mock Service Worker (MSW) is a library for mocking network requests in both browser and Node.js environments. It uses Service Worker API to intercept requests and provides a more modern approach to mocking compared to fetch-mock. MSW is particularly useful for testing in browser environments.
Axios Mock Adapter is a library that allows you to easily mock requests made with axios. It provides a simple API for defining mock responses for different HTTP methods. Compared to fetch-mock, axios-mock-adapter is specifically designed for use with the axios library.
npm install --save @types/fetch-mock
This package contains type definitions for fetch-mock (https://github.com/wheresrhys/fetch-mock).
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/fetch-mock.
These definitions were written by Alexey Svetliakov, Tamir Duberstein, Risto Keravuori, Chris Sinclair, Matt Tennison, Quentin Bouygues, Fumiaki Matsushima, Colin Doig, Felix Chen, and Katsuya Hino.
FAQs
TypeScript definitions for fetch-mock
We found that @types/fetch-mock demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.