Registry / testing / node-fetch-response-matchers

node-fetch-response-matchers

JSON →
library1.1.47jsnpmunverified

A Chai plugin providing declarative matchers for node-fetch response promises, enabling concise HTTP response assertions in tests. Version 1.1.47, with no recent updates. Key differentiators: simplifies testing by hiding promise callbacks and supports status, body, and header matchers. Ideal for TDD with node-fetch.

npm install node-fetch-response-matchers
INSTALL
IMPORT
SIG · NODE-FETCH-RESPONS
N
node-fetch-response-matchers
testingjavascriptv1.1.47
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
const nodeFetchMatchers = require('node-fetch-response-matchers');
import nodeFetchMatchers from 'node-fetch-response-matchers';
This package uses CommonJS; ESM import may not work directly. Use require() or dynamic import.
chai.use
chai.use(require('node-fetch-response-matchers'));
Typical usage: pass the plugin to chai.use() before using matchers.
successful
expect(fetch('http://example.com')).to.be.successful();
expect(fetch('http://example.com')).to.be.successful;
Matchers must be called as functions (e.g., .successful()) even if no arguments.

Shows how to install, import, and use the plugin with chai to write declarative HTTP response assertions.

const chai = require('chai'); const expect = chai.expect; const fetch = require('node-fetch'); const nodeFetchMatchers = require('node-fetch-response-matchers'); chai.use(nodeFetchMatchers); describe('HTTP response tests', function() { it('should be successful', function() { return expect(fetch('http://example.com')).to.be.successful(); }); it('should have status 200', function() { return expect(fetch('http://example.com')).to.haveStatus(200); }); it('should have body text', function() { return expect(fetch('http://example.com')).to.haveBodyText('expected text'); }); it('should have header', function() { return expect(fetch('http://example.com')).to.haveHeader('content-type', 'text/html'); }); });
Debug
Known issues
breakingRequires node-fetch promises; does not work with the native fetch API introduced in Node 18+.
fix
Use node-fetch (v2 or v3) instead of native fetch, or migrate to a different matcher library.
affects: >=1.0.0 <2.0.0
gotchaMatchers must be chained after a promise; forgeting to return the promise in tests may cause false positives.
fix
Always return the expect chain from the test function: return expect(fetch(...)).to....
affects: >=1.0.0
gotchaThe 'haveBodyObject' matcher uses deep equality, which may fail if response body contains extra fields.
fix
Use 'haveBodyThat' with a custom predicate if partial matching is needed.
affects: >=1.0.0
deprecatedThe 'rejected' matcher refers to HTTP 403, which is ambiguous with promise rejection semantics.
fix
Use 'haveStatus(403)' instead for clarity.
affects: >=1.0.0
breakingNode engine requirement >=4; may not work on Node versions with breaking http module changes.
fix
Use Node version >=4 and <=14 (tested); newer Node may have issues with node-fetch compatibility.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: nodeFetchMatchers is not a function
Using import statement instead of require, or forgetting to call chai.use with the plugin.
fix
Use require('node-fetch-response-matchers') and pass it to chai.use() as shown in the example.
AssertionError: expected promise to be successful
The HTTP request returned a non-200 status or the promise was rejected (network error).
fix
Check the URL and server status; ensure the server is running and returns 200.
TypeError: expect(...).to.be.successful is not a function
Plugin not registered with chai.use() before using matchers.
fix
Add chai.use(nodeFetchMatchers); before your tests.
Upgrade
Version history
1.1.47latest on npm
Audit
Dependencies
node-fetchrequiredRequired for making HTTP requests that this library matchers test against.
chairequiredRequired as the testing assertion library; this plugin extends chai.
Agent activity
7 hits · last 30 days
node
6
Amazon
1
Resources
node-fetch-response-matchers — npm install node-fetch-response-matchers · libregistry