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

vi-fetch

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vi-fetch

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7.6K
decreased by-16.18%
Maintainers
1
Weekly downloads
 
Created
Source

vi-fetch

An easy way to mock fetch

Compatible with Jest and Vitest.

Installing

# with npm
npm install -D fetch-mock

# with pnpm
pnpm install -D fetch-mock

# with yarn
yarn install -D fetch-mock

Usage

Setup

Before using mock API, you need to set up fetch mock. To do this, import fetch-mock/setup in your setup file:

import 'fetch-mock/setup';

If fetch is located on another object, you can manually setup it with prepareFetch function, imported from fetch-mock:

import { prepareFetch } from 'fetch-mock';

prepareFetch(globalThis, 'fetchNode');

Mock API

import { mockApi } from 'fetch-mock';

const mock = mockApi('GET', '/apples').willResolve(33);

expect(mock).toCallApi();

Aliases for popular methods are available: mockGet, mockPost, mockPut, mockPatch, mockDelete.

You can define baseUrl globally:

import { mockApi } from 'fetch-mock';

mockApi.setOptions({
  baseUrl: 'https://api.com/v1',
});

You can create isolated mockApi:

import { createMockApi } from 'fetch-mock';

const { mockApi } = createMockApi({ baseUrl: 'http://api.com/v2' });

const mock = mockApi('GET', '/apples').willResolve(33);

expect(mock).toCallApi();

FAQs

Package last updated on 02 Jan 2022

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