Mock Express for testing with Jest
- express()
- Application
- Request
- baseUrl
- body
- cookies
- fresh
- hostname
- ip
- ips
- method
- originalUrl
- params
- path
- protocol
- query
- route
- baseUrl
- secure
- signedCookies
- stale
- subdomains
- xhr
- accepts()
- acceptsCharsets()
- acceptsEncodings()
- acceptsLanguages()
- get()
- is()
- range()
- setBaseUrl()
- setBody()
- setCookies()
- setFresh()
- setHostname()
- setHeaders()
- setIp()
- setIps()
- setMethod()
- setOriginalUrl()
- setParams()
- setPath()
- setProtocol()
- setQuery()
- setRoute()
- setSecure()
- setSignedCookies()
- setStale()
- setSubdomains()
- setXhr()
- resetMocked()
- Response
- headersSent
- locals
- append()
- attachment()
- body
- cookie()
- clearCookie()
- download()
- end()
- format()
- get()
- getHeader()
- header()
- json()
- jsonp()
- links()
- location()
- redirect()
- render()
- send()
- sendFile()
- sendStatus()
- set()
- status()
- statusCode
- type()
- vary()
- setHeader()
- removeHeader()
- setHeadersSent()
- setLocals()
- resetMocked()
- Router
Other
How to setup Application to use in Jest:
jest.mock('express', () => {
return require('jest-express');
});
Ways to use this API:
expect(express.json).toHaveBeenCalledWith([options]);
Ways to use this API:
expect(express.static).toHaveBeenCalledWith(root, [options]);
Ways to use this API:
expect(express.Router).toHaveBeenCalledWith([options]);
Ways to use this API:
expect(express.urlencoded).toHaveBeenCalledWith([options]);
How to setup Application to use in Jest:
import { Express } from 'jest-express/lib/express';
import { server } from '../src/server.js';
let app;
describe('Server', () => {
beforeEach(() => {
app = new Express();
});
afterEach(() => {
app.resetMocked();
});
test('should setup server', () => {
const options = {
port: 3000,
};
server(app, options);
expect(app.set).toBeCalledWith('port', options.port);
});
});
Ways to use this API:
Setup:
beforeEach(() => {
app = new Express();
app.setLocals('title', 'My App');
});
Ways to use this API:
Setup:
beforeEach(() => {
app = new Express();
app.setMountPath('/admin');
});
Ways to use this API:
expect(app.all).toBeCalledWith(path, callback [, callback ...]);
Ways to use this API:
expect(app.get).toBeCalledWith(path, callback [, callback ...]);
Ways to use this API:
expect(app.head).toBeCalledWith(path, callback [, callback ...]);
Ways to use this API:
expect(app.post).toBeCalledWith(path, callback [, callback ...]);
Ways to use this API:
expect(app.put).toBeCalledWith(path, callback [, callback ...]);
Ways to use this API:
expect(app.delete).toBeCalledWith(path, callback [, callback ...]);
Ways to use this API:
expect(app.connect).toBeCalledWith(path, callback [, callback ...]);
Ways to use this API:
expect(app.options).toBeCalledWith(path, callback [, callback ...]);
Ways to use this API:
expect(app.trace).toBeCalledWith(path, callback [, callback ...]);
Ways to use this API:
expect(app.patch).toBeCalledWith(path, callback [, callback ...]);
Ways to use this API:
expect(app.param).toBeCalledWith([name], callback);
Ways to use this API:
expect(app.param).toBeCalledWith(view, [locals], callback);
Ways to use this API:
expect(app.use).toBeCalledWith([path,] callback [, callback...]);
How to setup Request to use in Jest:
import { Request } from 'jest-express/lib/request';
import { endpoint } from '../src/endpoint.js';
let request;
describe('Endpoint', () => {
beforeEach(() => {
request = new Request('/users?sort=desc', {
headers: {
Accept: 'text/html'
}
});
});
afterEach(() => {
request.resetMocked();
});
test('should setup endpoint', () => {
endpoint(request);
expect(request).toBeCalled();
});
});
Ways to use this API:
Setup:
beforeEach(() => {
request = new Request();
request.setBaseUrl(baseUrl);
});
Ways to use this API:
Setup:
beforeEach(() => {
request = new Request();
request.setBody(body);
});
Ways to use this API:
Setup:
beforeEach(() => {
request = new Request();
request.setCookies(cookies);
});
Ways to use this API:
Setup:
beforeEach(() => {
request = new Request();
request.setFresh(boolean);
});
Ways to use this API:
Setup:
beforeEach(() => {
request = new Request();
request.setHostname(string);
});
Ways to use this API:
Setup:
beforeEach(() => {
request = new Request();
request.setHeaders("X-Custom-Header", "foo");
});
// or
beforeEach(() => {
request = new Request();
request.setHeaders({
"X-Custom-Header", "foo",
"X-Custom-Header-2", "bar"
});
});
Ways to use this API:
Setup:
beforeEach(() => {
request = new Request();
request.setIp(ip);
});
Ways to use this API:
Setup:
beforeEach(() => {
request = new Request();
request.setIps(ips);
});
Ways to use this API:
Setup:
beforeEach(() => {
request = new Request();
request.setMethod(method);
});
Ways to use this API:
Setup:
beforeEach(() => {
request = new Request();
request.setOriginalUrl(originalUrl);
});
Ways to use this API:
Setup:
beforeEach(() => {
request = new Request();
request.setParams(params);
});
Ways to use this API:
Setup:
beforeEach(() => {
request = new Request();
request.setPath(path);
});
Ways to use this API:
Setup:
beforeEach(() => {
request = new Request();
request.setProtocol(protocol);
});
You can use it by passing key value pair:
Setup:
beforeEach(() => {
request = new Request();
request.setQuery('Accept', 'text/html');
});
Or by passing an object:
beforeEach(() => {
request = new Request();
request.setQuery({ 'Accept': 'text/html', 'Accept-Language': 'en' });
});
Ways to use this API:
Setup:
beforeEach(() => {
request = new Request();
request.setRoute(route);
});
Ways to use this API:
Setup:
beforeEach(() => {
request = new Request();
request.setSecure(secure);
});
Ways to use this API:
Setup:
beforeEach(() => {
request = new Request();
request.setSignedCookies(signedCookies);
});
Ways to use this API:
Setup:
beforeEach(() => {
request = new Request();
request.setStale(boolean);
});
Ways to use this API:
Setup:
beforeEach(() => {
request = new Request();
request.setSubdomains(subdomains);
});
Ways to use this API:
Setup:
beforeEach(() => {
request = new Request();
request.setXhr(boolean);
});
Ways to use this API:
expect(request.accepts).toBeCalledWith(types);
Ways to use this API:
expect(request.acceptsCharsets).toBeCalledWith(charset [, ...]);
Ways to use this API:
expect(request.acceptsEncodings).toBeCalledWith(encoding [, ...]);
Ways to use this API:
expect(request.acceptsLanguages).toBeCalledWith(lang [, ...]);
Ways to use this API:
expect(request.get).toBeCalledWith(field);
Ways to use this API:
expect(request.is).toBeCalledWith(type);
Ways to use this API:
expect(request.param).toBeCalledWith(name [, defaultValue]);
Ways to use this API:
expect(request.range).toBeCalledWith(size[, options]);
How to setup Response to use in Jest:
import { Response } from 'jest-express/lib/response';
import { endpoint } from '../src/endpoint.js';
let response;
describe('Endpoint', () => {
beforeEach(() => {
response = new Response();
});
afterEach(() => {
response.resetMocked();
});
test('should setup endpoint', () => {
endpoint(response);
expect(response).toBeCalled();
});
});
Ways to use this API:
Setup:
beforeEach(() => {
response = new Response();
response.setHeader(key, value);
expect(response.setHeader).toBeCalledWith(key, value);
});
Ways to use this API:
Setup:
beforeEach(() => {
response = new Response();
response.removeHeader(key);
expect(response.removeHeader).toBeCalledWith(key);
});
Ways to use this API:
Setup:
beforeEach(() => {
response = new Response();
response.setHeadersSent(boolean);
});
Ways to use this API:
Setup:
beforeEach(() => {
response = new Response();
response.setLocals('title', 'My App');
});
Ways to use this API:
expect(response.append).toBeCalledWith(field [, value]);
Ways to use this API:
expect(response.attachment).toBeCalledWith([filename]);
Ways to use this API:
expect(response.body).toEqual(value);
Ways to use this API:
expect(response.cookie).toBeCalledWith(name, value [, options]);
Ways to use this API:
expect(response.clearCookie).toBeCalledWith(name [, options]);
Ways to use this API:
expect(response.download).toBeCalledWith(path [, filename] [, options] [, fn]);
Ways to use this API:
expect(response.end).toBeCalledWith([data] [, encoding]);
Ways to use this API:
expect(response.format).toBeCalledWith(object);
Ways to use this API:
expect(response.get).toBeCalledWith(field);
Ways to use this API:
response.setHeader('Accept', 'text/html')
expect(response.getHeader('Accept')).toEqual('text/html');
An alias for response.set()
expect(response.header).toBeCalledWith(field, [value]);
Ways to use this API:
expect(response.json).toBeCalledWith([body]);
Ways to use this API:
expect(response.jsonp).toBeCalledWith([body]);
Ways to use this API:
expect(response.links).toBeCalledWith(links);
Ways to use this API:
expect(response.location).toBeCalledWith(path);
Ways to use this API:
expect(response.redirect).toBeCalledWith([status,] path);
Ways to use this API:
expect(response.render).toBeCalledWith(view [, locals] [, callback]);
Ways to use this API:
expect(response.send).toBeCalledWith([body]);
Ways to use this API:
expect(response.sendFile).toBeCalledWith(path [, options] [, fn]);
Ways to use this API:
expect(response.sendStatus).toBeCalledWith(statusCode);
Sets headers. It is calling response.setHeader()
internally.
expect(response.set).toBeCalledWith(field [, value]);
Ways to use this API:
expect(response.status).toBeCalledWith(code);
ways to use this API:
expect(response.statusCode).toEqual(code);
Ways to use this API:
expect(response.type).toBeCalledWith(type);
Ways to use this API:
expect(response.vary).toBeCalledWith(field);
How to setup Response to use in Jest:
import { Router } from 'jest-express/lib/router';
import { endpoint } from '../src/endpoint.js';
let router;
describe('Endpoint', () => {
beforeEach(() => {
router = new Router();
});
afterEach(() => {
router.resetMocked();
});
test('should setup endpoint', () => {
endpoint(router);
expect(router).toBeCalled();
});
});
Ways to use this API:
expect(router.all).toBeCalledWith(path, [callback, ...] callback);
Ways to use this API:
expect(router.get).toBeCalledWith(path, [callback, ...] callback);
Ways to use this API:
expect(router.param).toBeCalledWith(name, callback);
Ways to use this API:
expect(router.route).toBeCalledWith(path);
Ways to use this API:
expect(router.use).toBeCalledWith([path], [function, ...] function);
Resets all information stored in the mock, including any initial implementation and mock name given.
This is useful when you want to completely restore a mock back to its initial state.
$ git clone [email protected]:jameswlane/jest-express.git
$ cd jest-express
$ npm install
Linters:
$ npm run tslint
Tests:
$ npm test
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!