Chai HTTP is an assertion plugin for the Chai Assertion Library, designed specifically for conducting HTTP integration tests. It enables developers to test HTTP APIs by composing requests and asserting on their responses, supporting both web applications (like Express or Connect apps) and external URLs. The library uses Superagent under the hood for making HTTP requests. The current stable version is 5.1.2, which maintains an active release cadence with multiple updates in the past year, indicating sustainable maintenance and a popular standing within the Node.js ecosystem. Key differentiators include its fluent, chainable API for request creation, automatic server management for local applications (starting and stopping it), and comprehensive assertions for common HTTP tasks like status codes, headers, and body content. It ships with TypeScript types, enhancing developer experience in TypeScript projects.
npm install chai-httpVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates setting up Chai HTTP with an Express app, sending a GET request, and asserting the response status and text, and a POST request with data.
Migrate your project to use ESM `import` statements. Ensure your `package.json` has `"type": "module"` or use `.mjs` extensions.
Upgrade your Node.js environment to version 16.20.0 or higher.
Review `superagent` v9 changelogs for any breaking changes that might affect your tests. Update your test cases if necessary.
For multiple requests, use `const requester = request.Request(app).keepOpen();` then call `requester.close()` when all requests are done.
If targeting web browsers for testing, stick to `chai-http` v4.x. Be aware of browser same-origin policy limitations on reading certain HTTP headers.
Convert your test files to use ES Module `import` syntax (e.g., `import * as chai from 'chai';`). Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`).
Import `request` directly from `chai-http`: `import { request } from 'chai-http';`.Ensure you are using the default import for `chaiHttp` and passing it to `chai.use()`: `import chaiHttp from 'chai-http'; chai.use(chaiHttp);`
Verify that your application/API is sending the correct status code. For asynchronous tests, ensure `done()` is called for Mocha, or return the promise if using promise-based tests. Increase test timeout if necessary.