Registry / testing / browser-cache-mock

browser-cache-mock

JSON →
library0.1.7jsnpmunverified

A mock implementation of the Cache API (Cache, CacheStorage) for Node.js testing environments. Version 0.1.7 provides an in-memory cache for testing browser caching strategies without a browser. It ships TypeScript types and is designed for use with test frameworks like Jest or Mocha. This package allows mocking the Cache API to verify cached response behavior, such as offline support or cache-first strategies. The library is minimal and focused, with no external runtime dependencies, and is primarily maintained by MechanicalRock.

npm install browser-cache-mock
INSTALL
IMPORT
SIG · BROWSER-CACHE-MOCK
B
browser-cache-mock
testingjavascriptv0.1.7
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.

CacheMock
import CacheMock from 'browser-cache-mock'
const CacheMock = require('browser-cache-mock')
Default export: ESM-only. CommonJS require() will not work as the package is type: module.
CacheMock (CommonJS)
const CacheMock = require('browser-cache-mock').default
const CacheMock = require('browser-cache-mock')
In CommonJS with esModuleInterop disabled, use .default to access the exported class.
types
import type { CacheMock } from 'browser-cache-mock'
import { CacheMock } from 'browser-cache-mock'
TypeScript type imports should use `import type` to avoid runtime overhead.

Shows how to instantiate CacheMock, mock global caches, and use put/match for testing browser cache strategies.

import CacheMock from 'browser-cache-mock'; const cacheMock = new CacheMock(); // Mock the global caches object if (typeof globalThis.caches === 'undefined') { (globalThis as any).caches = { open: async (cacheName: string) => cacheMock, ...cacheMock }; } // Example: put a response in cache async function testCacheBehavior() { const cache = await caches.open('my-cache'); const request = new Request('https://example.com/data'); const response = new Response('hello world'); await cache.put(request, response); const cached = await cache.match(request); const text = await cached!.text(); console.log(text); // 'hello world' } testCacheBehavior();
Debug
Known issues
gotchaCacheMock does not enforce the same storage quota or eviction policies as real browsers. Tests may not reproduce browser cache limits.
fix
Do not rely on CacheMock for testing cache quota behavior; use browser-based tests for that.
affects: >=0.0.0
gotchaCacheMock is a simple in-memory mock and does not support operational details (e.g., CacheStorage match, delete with ignoreSearch). Behavior may differ from real Cache API.
fix
Review the unsupported features: some CacheStorage methods may be missing or not fully implemented.
affects: >=0.0.0
deprecatedThe package has not been updated recently and may lag behind newer Cache API specifications.
fix
Check the latest spec additions (e.g., CacheStorage.keys) and implement custom mocks if needed.
affects: >=0.1.7
Errors
Common errors & fixes
TypeError: cache_1.default is not a constructor
CommonJS require() returns the module's exports as an object with a default property. Using require without .default yields the module object, not the class.
fix
Use import CacheMock from 'browser-cache-mock' (ESM) or const CacheMock = require('browser-cache-mock').default (CJS).
Cannot find module 'browser-cache-mock' or its corresponding type declarations.
TypeScript cannot resolve the module because the project is not configured to handle ESM-only packages (e.g., missing moduleResolution: node16).
fix
Set moduleResolution to 'node16' or 'nodenext' in tsconfig.json and ensure the package is installed.
SyntaxError: The requested module 'browser-cache-mock' does not provide an export named 'CacheMock'
Attempting named import when the package only exposes a default export.
fix
Use default import: import CacheMock from 'browser-cache-mock'.
Upgrade
Version history
0.1.7latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
20 hits · last 30 days
node
18
OpenAI (training)
1
Resources
browser-cache-mock — npm install browser-cache-mock · libregistry