Registry / testing / bun-bagel

bun-bagel

JSON →
library1.2.0jsnpmunverified

bun-bagel is a lightweight mocking library for Bun's native fetch API, enabling developers to intercept HTTP requests and return custom responses during testing. Current stable version is 1.2.0. It is experimental and actively developed. Key differentiators: built specifically for Bun runtime (not Node.js), supports URL pattern matching with wildcards, mock by method/headers, and allows responses using Bun.file. No external dependencies except TypeScript peer dependency for type definitions. Ideal for unit testing Bun applications without network calls.

npm install bun-bagel
INSTALL
IMPORT
SIG · BUN-BAGEL
B
bun-bagel
testingjavascriptv1.2.0
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.

mock
import { mock } from 'bun-bagel'
const mock = require('bun-bagel')
ESM-only; CJS require will fail.
clearMocks
import { clearMocks } from 'bun-bagel'
import clearMocks from 'bun-bagel'
Named export, not default.
MockOptions
import type { MockOptions } from 'bun-bagel'
import { MockOptions } from 'bun-bagel'
TypeScript type import; use 'import type' for types.

Basic example of mocking a fetch request with glob pattern matching and response data.

import { mock, clearMocks } from 'bun-bagel'; // Mock a URL pattern mock('https://api.example.com/users/*', { data: { id: 1, name: 'Alice' } }); // Fetch the mocked URL const response = await fetch('https://api.example.com/users/123'); console.log(await response.json()); // { id: 1, name: 'Alice' } clearMocks(); // Clean up after test
Debug
Known issues
deprecatedLibrary is experimental and may change without major version bump before 2.0.0.
fix
Lock versions in package.json and update accordingly.
affects: 0.0.0 - 1.2.0
gotchaMock patterns use globs (e.g., '/*'), not regex. Unexpected results if regex used.
fix
Use wildcard patterns: 'https://example.com/api/users/*' matches any user ID.
affects: >=0.0.0
gotchaRequires Bun runtime. Does not work in Node.js or browsers.
fix
Use only in Bun environment.
affects: >=0.0.0
breakingVersion 1.0.0 changed from default export to named exports (mock, clearMocks).
fix
Replace 'import bagel from "bun-bagel"' with 'import { mock } from "bun-bagel"'.
affects: <1.0.0
deprecatedThe 'resetMocks' function was renamed to 'clearMocks' in version 1.0.0.
fix
Use 'clearMocks' instead.
affects: <1.0.0
Errors
Common errors & fixes
TypeError: require() of ES Module not supported
bun-bagel is ESM-only; require() fails.
fix
Use dynamic import or switch to ESM: import { mock } from 'bun-bagel'.
TypeError: Failed to parse URL from undefined
Mock URL not provided or invalid.
fix
Ensure mock() receives a string URL: mock('https://example.com/api', data)
TypeError: mock is not a function
Wrong import: default import used instead of named.
fix
Use named import: import { mock } from 'bun-bagel'
Error: Mock not found for URL
No matching mock registered for fetch call.
fix
Check URL pattern and method/headers. Use mock() with correct options.
TypeError: Cannot read properties of undefined (reading 'json')
Fetch returned undefined or mock returned non-response data.
fix
Ensure mock returns valid data or use Bun.file for file responses.
Upgrade
Version history
1.2.0latest on npm
Audit
Dependencies
typescriptoptionalPeer dependency for type definitions
Agent activity
28 hits · last 30 days
node
20
Amazon
1
OpenAI (training)
1
Resources
bun-bagel — npm install bun-bagel · libregistry