Registry / testing / chai-fetch

chai-fetch

JSON →
library0.3.1jsnpmunverified

Chai plugin providing asynchronous matchers for Fetch API responses, including .responseText() and .status(). Current stable version is 0.3.1 (last updated 2019). Release cadence is low/single-version. Key differentiator: simplifies testing fetch responses with clean chai syntax, supports promises and async/await, and includes TypeScript typings. Alternative to raw promise chaining or custom assertions.

npm install chai-fetch
INSTALL
IMPORT
SIG · CHAI-FETCH
C
chai-fetch
testingjavascriptv0.3.1
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.

chai-fetch
const chaiFetch = require('chai-fetch')
import chaiFetch from 'chai-fetch'
CommonJS module; ESM import requires default interop, use `import chaiFetch from 'chai-fetch'` only if bundler supports CJS interop.
chai-fetch (TypeScript)
import chaiFetch = require('chai-fetch')
import * as chaiFetch from 'chai-fetch'
TypeScript with --esModuleInterop: use `import chaiFetch from 'chai-fetch'` for default import.
chai.use(chaiFetch)
chai.use(chaiFetch)
chai.use(chaiFetch())
Pass the plugin function, not the result of calling it.

Demonstrates basic setup and usage of chai-fetch matchers .responseText() and .status() with async/await.

const chai = require('chai'); const chaiFetch = require('chai-fetch'); chai.use(chaiFetch); const { expect } = chai; // Async test (Mocha) it('should match response text', async () => { const response = new Response('hello world', { status: 200 }); await expect(Promise.resolve(response)).to.have.responseText('hello world'); }); it('should match status', async () => { const response = new Response(null, { status: 404 }); await expect(Promise.resolve(response)).to.have.status(404); });
Debug
Known issues
gotchaAssertions are asynchronous; forgetting to return/await promise causes false positives.
fix
Always return or await the expect(...).to.have.responseText(...) call.
affects: >=0.0.0
deprecatedPackage has not been updated since 2019; may not support newer fetch specs or ESM.
fix
Consider alternative libraries like chai-fetch-http or native chai-as-promised.
affects: 0.3.1
gotchaPlugin must be installed with chai.use(chaiFetch) before use.
fix
Call chai.use(chaiFetch) after importing.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: chai.use is not a function
chai not imported or chai.use called on wrong object
fix
Ensure chai is imported: const chai = require('chai'); then chai.use(chaiFetch);
AssertionError: expected promise to have response text '...' but none was ever made
Promise not resolved to a Response object; fetch mock not set up
fix
Ensure the tested value is a Promise<Response> or a Response directly.
Upgrade
Version history
0.3.1latest on npm
Audit
Dependencies
chairequiredpeer dependency: chai plugin extending chai assertions
Agent activity
16 hits · last 30 days
node
14
OpenAI (training)
1
Resources
chai-fetch — npm install chai-fetch · libregistry