Retyped Chai-HTTP Typings
This package provides TypeScript ambient type declarations for the `chai-http` library. It originates from the `retyped` project, which aimed to offer curated typings for various JavaScript libraries. However, the `retyped` project itself appears to be abandoned, with most packages, including this one, maintaining a `0.0.0-0` version number, indicating no active development or updates. Modern `chai-http` (version 4.2.4 and above) ships with its own built-in TypeScript definitions, making external typing packages like `retyped-chai-http-tsd-ambient` largely obsolete. Users are generally advised to rely on `chai-http`'s native types rather than this unmaintained package or even the stub `@types/chai-http` package.
Common errors
-
Property 'request' does not exist on type 'ChaiStatic'.
cause The TypeScript compiler cannot find the `request` property augmentation on the `chai` object, typically due to missing or incorrectly loaded `chai-http` type declarations.fixEnsure `chai-http` is installed and updated to a version with built-in types. Remove `retyped-chai-http-tsd-ambient` and `@types/chai-http`. If using an older `chai-http` version, ensure `import chaiHttp = require('chai-http');` is used in CommonJS contexts. -
Property 'get' does not exist on type 'Agent'.
cause The types for `superagent`, which `chai-http` extends, are not correctly applied, leading to missing HTTP method declarations on the `chai.request` object.fixInstall `@types/superagent` as a development dependency: `npm install --save-dev @types/superagent`. This provides the necessary declarations for the HTTP methods. -
Could not find a declaration file for module 'chai-http'.
cause TypeScript cannot locate the type definition files (`.d.ts`) for `chai-http`.fixEnsure `chai-http` is installed and that its version includes built-in types. If not, consider upgrading `chai-http` or manually installing `@types/chai-http` (though `chai-http`'s native types are preferred if available). Confirm your `tsconfig.json` includes `node_modules/@types` in `typeRoots` or similar. -
Parameter 'res' implicitly has an 'any' type.
cause When using `chai-http` with Promises or callbacks, TypeScript might not infer the type of the `res` (response) object if `chai-http`'s types or `superagent`'s types are not fully resolved, or if `chai.use(chaiHttp)` is not correctly applied.fixEnsure all relevant typing packages (`chai`, `chai-http`, `superagent`) are installed and properly configured. Explicitly type the callback parameters (e.g., `(err: any, res: ChaiHttp.Response) => { ... }`) as a temporary workaround, but the root cause is usually missing or conflicting types.
Warnings
- breaking The `retyped` project, which this package is part of, appears to be abandoned (last activity on GitHub for the main repo was in 2017). This package is unlikely to receive updates for new `chai-http` versions or TypeScript changes.
- gotcha `chai-http` (versions 4.2.4 and newer) provides its own type declarations directly within the package. Installing `retyped-chai-http-tsd-ambient` or `@types/chai-http` is unnecessary and can potentially lead to type conflicts or outdated definitions.
- gotcha When using `chai-http` with TypeScript, particularly in CommonJS environments, incorrect import syntax can prevent type augmentation of `chai`. For example, `const chaiHttp = require('chai-http')` might not correctly apply types.
- gotcha TypeScript errors such as 'Property 'get' does not exist on type 'Agent'' can occur if the types for `superagent` (which `chai-http` uses internally) are not correctly resolved or are missing. This is because `chai-http` extends `superagent`'s request objects.
Install
-
npm install retyped-chai-http-tsd-ambient -
yarn add retyped-chai-http-tsd-ambient -
pnpm add retyped-chai-http-tsd-ambient
Imports
- chai
const chai = require('chai');import * as chai from 'chai';
- chaiHttp
import { chaiHttp } from 'chai-http'; // Incorrect named import const chaiHttp = require('chai-http'); // Might lose type augmentation without specific TS syntaximport chaiHttp from 'chai-http';
- request
import * as request from 'chai-http'; // Incorrect if only `request` is needed
import { request } from 'chai-http';
Quickstart
import * as chai from 'chai';
import chaiHttp from 'chai-http';
import { Application } from 'express'; // Assuming Express.js application
import 'mocha'; // For test runner globals
// Optionally import superagent types if not implicitly resolved by chai-http's built-in types
// import '@types/superagent';
// Initialize Chai HTTP plugin
chai.use(chaiHttp);
// Create a dummy Express app for testing
const app: Application = require('express')();
app.get('/test', (req, res) => res.status(200).send('Hello Test!'));
app.post('/data', (req, res) => res.status(201).json({ message: 'Data received' }));
describe('Chai-HTTP with TypeScript', () => {
it('should get a successful response from /test', (done) => {
chai.request(app)
.get('/test')
.end((err, res) => {
chai.expect(err).to.be.null;
chai.expect(res).to.have.status(200);
chai.expect(res.text).to.equal('Hello Test!');
done();
});
});
it('should post data and get a created response from /data', (done) => {
chai.request(app)
.post('/data')
.send({ item: 'test' })
.end((err, res) => {
chai.expect(err).to.be.null;
chai.expect(res).to.have.status(201);
chai.expect(res).to.be.json;
chai.expect(res.body.message).to.equal('Data received');
done();
});
});
});