What is @types/supertest?
The @types/supertest package provides TypeScript type definitions for the Supertest library, which is a SuperAgent driven library for testing HTTP servers. These type definitions allow developers to use Supertest in TypeScript projects with the benefits of type checking and IntelliSense in their code editors.
What are @types/supertest's main functionalities?
HTTP Assertions
This feature allows you to make HTTP assertions with Supertest. The code sample demonstrates how to test a GET request to '/user' endpoint expecting a JSON response and a 200 status code.
import supertest from 'supertest';
import app from './app';
describe('GET /user', () => {
it('responds with json', (done) => {
supertest(app)
.get('/user')
.expect('Content-Type', /json/)
.expect(200, done);
});
});
Type Checking
This feature provides type checking for Supertest objects. The code sample shows how to create a typed Supertest instance and use it to make a GET request.
import supertest, { SuperTest, Test } from 'supertest';
import app from './app';
const request: SuperTest<Test> = supertest(app);
request.get('/user').expect(200);
Other packages similar to @types/supertest
chai-http
Chai-HTTP is a plugin for the Chai assertion library that enables HTTP assertions. It is similar to Supertest in that it allows for testing HTTP servers, but it is designed to work seamlessly with Chai's assertion methods.
axios
Axios is a promise-based HTTP client for the browser and Node.js. While not specifically designed for testing like Supertest, it can be used in tests to make HTTP requests and handle responses.
nock
Nock is an HTTP server mocking and expectations library for Node.js. It allows developers to test modules that perform HTTP requests in isolation, by intercepting requests and providing programmed responses. Unlike Supertest, which performs actual HTTP requests, Nock intercepts and simulates them.
jest-fetch-mock
Jest Fetch Mock allows you to mock the fetch API in Jest tests, providing a way to test components that make fetch calls without making real HTTP requests. It is similar to Nock but designed to work with Jest and the fetch API.