Registry / testing / chai-fetch-mock

chai-fetch-mock

JSON →
library3.0.0jsnpmunverified

Chai plugin for fetch-mock library version 3.0.0. Provides Chai assertions for fetch-mock, a mocking library for fetch() and isomorphic-fetch. Active maintenance, weekly release cadence. Differentiator: adds fluent BDD-style assertions like `.called`, `.with.args()`, `.with.url()`, `.with.options()` for fetch-mock routes, requiring careful ordering with other Chai plugins. Supports Node.js 7+ and Chai 3.x/4.x, fetch-mock 5.1.x/6.x. Note: fetch-mock 6.0 requires Node >=7.

npm install chai-fetch-mock
INSTALL
IMPORT
SIG · CHAI-FETCH-MOCK
C
chai-fetch-mock
testingjavascriptv3.0.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.

default (chaiFetchMock)
import chaiFetchMock from 'chai-fetch-mock'
const chaiFetchMock = require('chai-fetch-mock')
ESM-only export; use import statement or dynamic import()
chai.use
chai.use(chaiFetchMock)
chai.use('chai-fetch-mock')
Pass the plugin function, not a string
expect(fetchMock).route(...)
expect(fetchMock).route('/cats').to.have.been.called
expect(fetchMock).to.have.been.called.route('/cats')
route() must be called before other assertions, not after

Sets up a fetch mock, applies the chai-fetch-mock plugin, and uses various route assertions.

import chai from 'chai'; import chaiFetchMock from 'chai-fetch-mock'; import fetchMock from 'fetch-mock'; chai.use(chaiFetchMock); const { expect } = chai; before(() => fetchMock.get('/cats', { cats: 5 })); expect(fetchMock).route('/cats').to.have.been.called; expect(fetchMock).route('/cats').to.have.been.called.with.args(['/cats', { cats: 5 }]); expect(fetchMock).route('/cats').to.have.been.called.with.url('/cats'); expect(fetchMock).route('/cats').to.have.been.called.with.options({ method: 'GET' }); after(() => fetchMock.restore());
Debug
Known issues
gotchaRoute must be defined via route() before using .called, .with.args, .with.url, or .with.options
fix
Always start the assertion chain with .route(urlOrName), e.g., expect(fetchMock).route('/path').to.have.been.called
affects: >=1.0.0
gotchaApply chai-fetch-mock after other plugins like sinon-chai to avoid conflicts with similar vocabulary (e.g., called)
fix
Order plugin registration: sin·on-chai first, then chai-fetch-mock
affects: >=1.0.0
deprecatedfetch-mock 6.x is required for Node >=7; support for fetch-mock 5.x may be dropped
fix
Update to fetch-mock >=6.0.0 and Node >=7
affects: >=3.0.0
gotcha`.with.args()` asserts deep equality of the entire arguments array, not just the URL
fix
Pass array of arguments: [url, options]
affects: >=1.0.0
gotchaAssertions are asynchronous; ensure the fetch call has completed before making assertions (e.g., inside .then() or async/await)
fix
Place assertions inside the .then() callback after the fetch promise resolves
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: chai.use is not a function
chai.use is invoked on a string instead of the chai object
fix
const chai = require('chai') or import chai from 'chai'
AssertionError: expected [Function] to have been called
Forgetting to call .route() before .called
fix
Add .route('/path') before .to.have.been.called
TypeError: Cannot read property 'route' of undefined
fetchMock is not passed to expect() or is undefined
fix
Make sure fetchMock is imported and passed: expect(fetchMock).route(...)
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies
chairequiredPeer dependency; plugin extends chai assertions
fetch-mockrequiredPeer dependency; provides the mock fetch object
Agent activity
16 hits · last 30 days
node
14
OpenAI (training)
1
Resources
chai-fetch-mock — npm install chai-fetch-mock · libregistry