Registry / testing / mentoss

mentoss

JSON →
library0.13.0jsnpmunverified

Mentoss is a lightweight, TypeScript-first utility for mocking fetch() requests and responses in Node.js (>=18.18) and browser environments. Version 0.13.0 (July 2024) provides three core classes — MockServer, FetchMocker, and MockAgent — to define server routes, replace global fetch, and intercept undici requests. Unlike similar libraries (e.g., nock, msw), Mentoss focuses exclusively on fetch() and undici, supporting both ESM and CommonJS through conditional exports, shipping type definitions, and offering a minimal API with built-in route tracking and assertion helpers. The package is authored by Nicholas C. Zakas (ESLint creator) and has a release cadence of approximately monthly updates.

npm install mentoss
INSTALL
IMPORT
SIG · MENTOSS
M
mentoss
testingjavascriptv0.13.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.

MockServer
import { MockServer } from 'mentoss'
const { MockServer } = require('mentoss')
Mentoss is ESM-only since v0.9.0; require() fails with ERR_REQUIRE_ESM.
FetchMocker
import { FetchMocker } from 'mentoss'
import { FetchMocker } from 'mentoss/src/index.js'
Do not import from internal paths; they are not part of public API.
MockAgent
import { MockAgent } from 'mentoss'
import { MockAgent } from 'mentoss/types'
Types are re-exported; no separate type-only import path.

Sets up a MockServer with a GET route, creates a FetchMocker, makes a fetch call, and verifies the route was called.

import { MockServer, FetchMocker } from 'mentoss'; const server = new MockServer('https://api.example.com'); server.get('/foo/bar', { status: 200, body: { message: 'OK' } }); const mocker = new FetchMocker({ servers: [server] }); const { fetch } = mocker; const response = await fetch('https://api.example.com/foo/bar'); const data = await response.json(); console.log(data); console.assert(mocker.called('https://api.example.com/foo/bar')); console.assert(mocker.allRoutesCalled()); mocker.clearAll();
Debug
Known issues
breakingESM-only since v0.9.0: require('mentoss') throws ERR_REQUIRE_ESM. Use import or dynamic import().
fix
Switch to ESM imports (import { MockServer } from 'mentoss') or use dynamic import() in CJS context.
affects: >=0.9.0
gotchaUsing Mentoss inside Jest requires --experimental-vm-modules flag and Jest 28+ with transformIgnorePatterns adjusted.
fix
Run Jest with node --experimental-vm-modules ./node_modules/.bin/jest and add 'mentoss' to transformIgnorePatterns exception.
affects: >=0.0.0
gotchaMockAgent does not support baseUrl or credentials options; they are silently ignored (only relevant for browser fetch()).
fix
Do not pass baseUrl or credentials to MockAgent; configure those via MockServer routes if needed.
affects: >=0.0.0
deprecatedMethod mocker.routesCalled() was deprecated in v0.10.0 and removed in v0.11.0. Use mocker.allRoutesCalled() instead.
fix
Replace mocker.routesCalled() with mocker.allRoutesCalled().
affects: >=0.10.0 <0.11.0
Errors
Common errors & fixes
ERR_REQUIRE_ESM: require() of ES Module /path/to/node_modules/mentoss/src/index.js from /path/to/project/test.js not supported.
Mentoss is ESM-only; using require() fails since v0.9.0.
fix
Use import { MockServer } from 'mentoss' instead of require().
TypeError: MockServer is not a constructor
Importing MockServer incorrectly (e.g., as default import or from wrong path).
fix
Use named import: import { MockServer } from 'mentoss'.
Jest encountered an unexpected token - SyntaxError: Unexpected token 'export'
Jest's default transform doesn't transpile ESM from node_modules.
fix
Enable ESM support in Jest: use node --experimental-vm-modules jest and add 'mentoss' to transformIgnorePatterns in jest.config.
Error: Cannot find module 'undici'
MockAgent requires undici to be installed; Mentoss does not include it as a dependency.
fix
Install undici: npm install undici.
Upgrade
Version history
0.13.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources