Top AI Repos — open-source AI, indexed and scored
Top AI Repos tracks AI repositories on GitHub and answers two different questions about each one: is it moving right now, and would you bet a product on it.
Top AI Repos tracks AI repositories on GitHub and answers two different questions about each one: is it moving right now, and would you bet a product on it.
Mock 'http' objects for testing Express,js, Next.js and Koa routing functions
| Date | Stars |
|---|---|
| 2026-07-24 | 773 |
| 2026-07-25 | 773 |
| 2026-07-28 | 773 |
| 2026-07-30 | 773 |
| 2026-08-06 | 773 |
Today
— stars today
This week
— stars this week
This month
— stars this month
Momentum
35.0
growth rate 0.00%/day
[](https://github.com/eugef/node-mocks-http)
---
[![NPM version][npm-badge]][npm-url]
Mock 'http' objects for testing [Express][express-url], [Next.js][nextjs-url] and [Koa][koa-url] routing functions,
but could be used for testing any [Node.js][node-url] web server applications that have code that requires mockups of the `request` and `response` objects.
## Installation
This project is available as a
[NPM package][npm-url].
```bash
$ npm install node-mocks-http --save-dev
$ npm install @types/node @types/express --save-dev # when using TypeScript
```
or
```bash
$ yarn add node-mocks-http --dev
$ yarn add @types/node @types/express --dev # when using TypeScript
```
After installing the package include the following in your test files:
```js
const httpMocks = require('node-mocks-http');
```
## Usage
Suppose you have the following Express route:
```js
app.get('/user/:id', routeHandler);
```
And you have created a function to handle that route's call:
```js
const routeHandler = function( request, response ) { ... };
```
You can easily test the `routeHandler` function with some code like
this using the testing framework of your choice:
```js
exports['routeHandler - Simple testing'] = function (test) {
const request = httpMocks.createRequest({
method: 'GET',
url: '/user/42',
params: {
id: 42
}
});
const response = httpMocks.createResponse();
routeHandler(request, response);
const data = response._getJSONData(); // short-hand for JSON.parse( response._getData() );
test.equal('Bob Dog', data.name);
test.equal(42, data.age);
test.equal('[email protected]', data.email);
test.equal(200, response.statusCode);
test.ok(response._isEndCalled());
test.ok(response._isJSON());
test.ok(response._isUTF8());
test.done();
};
```
### TypeScript typings
The typings for TypeScript are bundled with this project. In particular, the `.createRequest()`, `.createResponse()` and `.createMocks()` methods are typed and are generic. Unless specified explicitly, they will be return an Express-based request/response object:
```ts
it('should handle expressjs requests', () => {
const mockExpressRequest = httpMocks.createRequest({
method: 'GET',
url: '/user/42',
params: {
id: 42
}
});
const mockExpressResponse = httpMocks.createResponse();
routeHandler(request, response);
const data = response._getJSONData();
test.equal('Bob Dog', data.name);
test.equal(42, data.age);
test.equal('[email protected]', data.email);
test.equal(200, response.statusCode);
test.ok(response._isEndCalled());
test.ok(response._isJSON());
test.ok(response._isUTF8());
test.done();
});
```
The expected type parameter in the mock request and response expects any type that extends the NodeJS
`http.IncomingRequest` interface or Fetch API `Request` class. This means you can also mock requests
coming from other frameworks too. An example for NextJS request will look like this:
```ts
it('should handle nextjs requests', () => {
const mockExpressRequest = httpMocks.createRequest<NextApiRequest>({
method: 'GET',
url: '/user/42',
params: {
id: 42
}
});
const mockExpressResponse = httpMocks.createResponse<NextApiResponse>();
// ... the rest of the test as above.
});
```
It is also possible to mock requests from the NextJS new AppRouter:
```ts
it('should handle nextjs app reouter requests', () => {
const mockExpressRequest = httpMocks.createRequest<NextRequest>({
method: 'GET',
url: '/user/42',
params: {
id: 42
}
});
const mockExpressResponse = httpMocks.createResponse<NextResponse>();
// ... the rest of the test as above.
});
```
## API
### .createRequest()
```
httpMExcerpt of 9,219 characters
Read on GitHub98
60
11
11
8
7
5
5
4
4
4
4
4
3
3
3
3
3
3
2
Would you bet a product on this? Bounded 0–100 and slow moving.
matched fp:65553efe34e25fe6, topic:testing