Registry / testing / bun-fetch-mock

bun-fetch-mock

JSON →
library1.0.2jsnpmunverified

A fetch mocking library designed specifically for Bun's runtime (≥1.0.0). Version 1.0.2 provides a simple API for mocking HTTP requests in Bun tests with TypeScript support. Key differentiators: zero external dependencies, first-class Bun integration (no polyfills needed), assertion helpers to verify all mocks were used, and support for dynamic responses. Released as MIT, actively maintained on GitHub.

npm install bun-fetch-mock
INSTALL
IMPORT
SIG · BUN-FETCH-MOCK
B
bun-fetch-mock
testingjavascriptv1.0.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

useFetchMock
import { useFetchMock } from 'bun-fetch-mock'
const { useFetchMock } = require('bun-fetch-mock')
Package is ESM-only and uses named exports. Use ES import syntax.
FetchMock
import type { FetchMock } from 'bun-fetch-mock'
Type import for TypeScript users if they need the return type explicitly.
useFetchMock in CommonJS
const { useFetchMock } = await import('bun-fetch-mock')
const { useFetchMock } = require('bun-fetch-mock')
CommonJS require is not supported; use dynamic import with await.

Demonstrates basic usage: initialize mock inside describe, define routes, and verify all mocks are used.

import { describe, test, expect } from 'bun:test'; import { useFetchMock } from 'bun-fetch-mock'; describe('api', () => { const fetchMock = useFetchMock({ baseUrl: 'https://api.example.com' }); test('mocks fetch', async () => { fetchMock .get('/users/1', { data: { id: 1, name: 'Ada' }, once: true }) .get('/users/1', { status: 404, data: { error: 'Not found' } }); const okRes = await fetch('https://api.example.com/users/1'); expect(await okRes.json()).toEqual({ id: 1, name: 'Ada' }); const notFoundRes = await fetch('https://api.example.com/users/1'); expect(notFoundRes.status).toBe(404); fetchMock.assertAllMocksUsed(); }); });
Debug
Known issues
gotchaInitialize useFetchMock() inside describe block, not inside test blocks
fix
Move useFetchMock() call to describe scope.
affects: >=1.0.0
gotchamock.get('path') without trailing slash does not match requests with trailing slash
fix
Ensure path consistency between mocked routes and actual fetch calls.
affects: >=1.0.0
gotchaassertAllMocksUsed() must be called at the end of each test to verify all mocks were consumed
fix
Call fetchMock.assertAllMocksUsed() before the test ends.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: fetch is not defined
The package expects Bun's global fetch to be available. In Node.js or other runtimes, fetch might not be globally defined.
fix
Ensure you are running tests with Bun (bun test) or polyfill fetch globally.
Cannot find module 'bun-fetch-mock'
Package not installed or installed incorrectly (e.g., in wrong directory).
fix
Run 'bun add -d bun-fetch-mock' from the correct project root.
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
25 hits · last 30 days
node
18
OpenAI (training)
1
Resources
bun-fetch-mock — npm install bun-fetch-mock · libregistry