
Product
Secure Your AI-Generated Code with Socket MCP
Socket MCP brings real-time security checks to AI-generated code, helping developers catch risky dependencies before they enter the codebase.
Chai matchers to make matching fetch responses clear & easy
fetch('http://example.com').then((response) => response.text()).then((text) => expect(text).to.equal('hi there'))
is pretty much the simplest way to assert on the body of a fetch response, and that's a huge horrible mess. This
library takes away that pain, and makes life easier.
Install chai-fetch
with:
npm install --save-dev chai-fetch
Chai-fetch is a commonjs module, and should work out of the box in node, or with bundling tools like browserify & webpack.
Chai-fetch is written in TypeScript, so will work completely fine with JS, but if you're using TypeScript too you'll also get working typings out of the box.
An example test using this (with http-server-mock to fake HTTP responses) would look like:
const chai = require('chai');
const chaiFetch = require('chai-fetch');
chai.use(chaiFetch);
const { expect } = chai;
describe('Chai-fetch', () => {
beforeEach(() => mockServer.start(8080));
afterEach(() => mockServer.stop());
describe('.responseText', () => {
it('should match responses with matching bodies', () => {
mockServer.get('/match').thenReply(200, 'matching body')
.then(() =>
expect(fetch('http://localhost:8080/match')).to.have.responseText('matching body')
);
});
});
});
Remember that the assertions here are all asynchronous, so you need to return
or .then
or await
on them, to
ensure that your test framework waits for the result and catches failures.
Take a look at http-server-mock to mock your server responses.
If you're writing HTTP tests like this, and you're using Babel, TypeScript or just some very modern JS engines, you can make them a little more readable with async/await:
it('should match responses with matching bodies', async () => {
await mockServer.get('/match').thenReply(200, 'matching body');
await expect(fetch('http://localhost:8080/match')).to.have.responseText('matching body');
});
.responseText(expectedText)
e.g. expect(fetch('http://example.com')).to.have.responseText('hi there')
If the object being tested is a fetch response, or a promise for a fetch response, this asserts that the full text of the body is equal to the text given.
You can also pass a regular expression: .responseText(/match a substring/)
.
This does not test the status code (just like fetch itself doesn't), but both normal and negated tests will fail if a passed response promise is rejected entirely (e.g. if you have a network error).
.status(expectedStatus)
e.g. expect(fetch('http://example.com')).to.have.status(200)
If the object being tested is a fetch response, or a promise for a fetch response, this asserts that the status of the response is the status given.
FAQs
Chai matchers to make matching fetch responses clear & easy
The npm package chai-fetch receives a total of 3,540 weekly downloads. As such, chai-fetch popularity was classified as popular.
We found that chai-fetch demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket MCP brings real-time security checks to AI-generated code, helping developers catch risky dependencies before they enter the codebase.
Security News
As vulnerability data bottlenecks grow, the federal government is formally investigating NIST’s handling of the National Vulnerability Database.
Research
Security News
Socket’s Threat Research Team has uncovered 60 npm packages using post-install scripts to silently exfiltrate hostnames, IP addresses, DNS servers, and user directories to a Discord-controlled endpoint.