Gathering detailed insights and metrics for jest-express
Gathering detailed insights and metrics for jest-express
Gathering detailed insights and metrics for jest-express
Gathering detailed insights and metrics for jest-express
@jest-mock/express
A lightweight Jest mock for unit testing Express
@nrwl/express
The Nx Plugin for Express contains executors and generators for allowing your workspace to create powerful Express Node applications and APIs.
@nx/express
The Nx Plugin for Express contains executors and generators for allowing your workspace to create powerful Express Node applications and APIs.
jest-mock-express
Mock express for testing with Jest
npm install jest-express
Typescript
Module System
Node Version
NPM Version
98.6
Supply Chain
100
Quality
76
Maintenance
100
Vulnerability
100
License
TypeScript (98.89%)
JavaScript (1.11%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
10,903,359
Last Day
5,845
Last Week
38,854
Last Month
172,745
Last Year
2,040,229
MIT License
181 Stars
1,006 Commits
27 Forks
9 Watchers
14 Branches
15 Contributors
Updated on Oct 30, 2024
Minified
Minified + Gzipped
Latest Version
1.12.0
Package Id
jest-express@1.12.0
Unpacked Size
170.73 kB
Size
27.65 kB
File Count
46
NPM Version
6.14.4
Node Version
10.20.1
Cumulative downloads
Total Downloads
Last Day
-27.1%
5,845
Compared to previous day
Last Week
1.3%
38,854
Compared to previous week
Last Month
49.9%
172,745
Compared to previous month
Last Year
-18%
2,040,229
Compared to previous year
1
24
Mock Express for testing with Jest
Other
express()
How to setup Application to use in Jest:
1jest.mock('express', () => { 2 return require('jest-express'); 3});
express.json()
Ways to use this API:
1expect(express.json).toHaveBeenCalledWith([options]);
express.static()
Ways to use this API:
1expect(express.static).toHaveBeenCalledWith(root, [options]);
express.Router()
Ways to use this API:
1expect(express.Router).toHaveBeenCalledWith([options]);
express.urlencoded()
Ways to use this API:
1expect(express.urlencoded).toHaveBeenCalledWith([options]);
Application
How to setup Application to use in Jest:
1import { Express } from 'jest-express/lib/express'; 2import { server } from '../src/server.js'; 3 4let app; 5 6describe('Server', () => { 7 beforeEach(() => { 8 app = new Express(); 9 }); 10 11 afterEach(() => { 12 app.resetMocked(); 13 }); 14 15 test('should setup server', () => { 16 const options = { 17 port: 3000, 18 }; 19 20 server(app, options); 21 22 expect(app.set).toBeCalledWith('port', options.port); 23 }); 24});
app.locals
Ways to use this API:
Setup:
1beforeEach(() => { 2 app = new Express(); 3 app.setLocals('title', 'My App'); 4});
app.mountpath
Ways to use this API:
Setup:
1beforeEach(() => { 2 app = new Express(); 3 app.setMountPath('/admin'); 4});
app.all()
Ways to use this API:
1expect(app.all).toBeCalledWith(path, callback [, callback ...]);
app.get()
Ways to use this API:
1expect(app.get).toBeCalledWith(path, callback [, callback ...]);
app.head()
Ways to use this API:
1expect(app.head).toBeCalledWith(path, callback [, callback ...]);
app.post()
Ways to use this API:
1expect(app.post).toBeCalledWith(path, callback [, callback ...]);
app.put()
Ways to use this API:
1expect(app.put).toBeCalledWith(path, callback [, callback ...]);
app.delete()
Ways to use this API:
1expect(app.delete).toBeCalledWith(path, callback [, callback ...]);
app.connect()
Ways to use this API:
1expect(app.connect).toBeCalledWith(path, callback [, callback ...]);
app.options()
Ways to use this API:
1expect(app.options).toBeCalledWith(path, callback [, callback ...]);
app.trace()
Ways to use this API:
1expect(app.trace).toBeCalledWith(path, callback [, callback ...]);
app.patch()
Ways to use this API:
1expect(app.patch).toBeCalledWith(path, callback [, callback ...]);
app.param()
Ways to use this API:
1expect(app.param).toBeCalledWith([name], callback);
app.render()
Ways to use this API:
1expect(app.param).toBeCalledWith(view, [locals], callback);
app.use()
Ways to use this API:
1expect(app.use).toBeCalledWith([path,] callback [, callback...]);
Request
How to setup Request to use in Jest:
1import { Request } from 'jest-express/lib/request'; 2import { endpoint } from '../src/endpoint.js'; 3 4let request; 5 6describe('Endpoint', () => { 7 beforeEach(() => { 8 request = new Request('/users?sort=desc', { 9 headers: { 10 Accept: 'text/html' 11 } 12 }); 13 }); 14 15 afterEach(() => { 16 request.resetMocked(); 17 }); 18 19 test('should setup endpoint', () => { 20 endpoint(request); 21 22 expect(request).toBeCalled(); 23 }); 24});
request.baseUrl
Ways to use this API:
Setup:
1beforeEach(() => { 2 request = new Request(); 3 request.setBaseUrl(baseUrl); 4});
request.body
Ways to use this API:
Setup:
1beforeEach(() => { 2 request = new Request(); 3 request.setBody(body); 4});
request.cookies
Ways to use this API:
Setup:
1beforeEach(() => { 2 request = new Request(); 3 request.setCookies(cookies); 4});
request.fresh
Ways to use this API:
Setup:
1beforeEach(() => { 2 request = new Request(); 3 request.setFresh(boolean); 4});
request.hostname
Ways to use this API:
Setup:
1beforeEach(() => { 2 request = new Request(); 3 request.setHostname(string); 4});
request.setHeaders
Ways to use this API:
Setup:
1beforeEach(() => { 2 request = new Request(); 3 request.setHeaders("X-Custom-Header", "foo"); 4}); 5 6// or 7 8beforeEach(() => { 9 request = new Request(); 10 request.setHeaders({ 11 "X-Custom-Header", "foo", 12 "X-Custom-Header-2", "bar" 13 }); 14}); 15
request.ip
Ways to use this API:
Setup:
1beforeEach(() => { 2 request = new Request(); 3 request.setIp(ip); 4});
request.ips
Ways to use this API:
Setup:
1beforeEach(() => { 2 request = new Request(); 3 request.setIps(ips); 4});
request.method
Ways to use this API:
Setup:
1beforeEach(() => { 2 request = new Request(); 3 request.setMethod(method); 4});
request.originalUrl
Ways to use this API:
Setup:
1beforeEach(() => { 2 request = new Request(); 3 request.setOriginalUrl(originalUrl); 4});
request.params
Ways to use this API:
Setup:
1beforeEach(() => { 2 request = new Request(); 3 request.setParams(params); 4});
request.path
Ways to use this API:
Setup:
1beforeEach(() => { 2 request = new Request(); 3 request.setPath(path); 4});
request.protocol
Ways to use this API:
Setup:
1beforeEach(() => { 2 request = new Request(); 3 request.setProtocol(protocol); 4});
request.query
You can use it by passing key value pair:
Setup:
1beforeEach(() => { 2 request = new Request(); 3 request.setQuery('Accept', 'text/html'); 4});
Or by passing an object:
1beforeEach(() => { 2 request = new Request(); 3 request.setQuery({ 'Accept': 'text/html', 'Accept-Language': 'en' }); 4});
request.route
Ways to use this API:
Setup:
1beforeEach(() => { 2 request = new Request(); 3 request.setRoute(route); 4});
request.secure
Ways to use this API:
Setup:
1beforeEach(() => { 2 request = new Request(); 3 request.setSecure(secure); 4});
request.signedCookies
Ways to use this API:
Setup:
1beforeEach(() => { 2 request = new Request(); 3 request.setSignedCookies(signedCookies); 4});
request.stale
Ways to use this API:
Setup:
1beforeEach(() => { 2 request = new Request(); 3 request.setStale(boolean); 4});
request.subdomains
Ways to use this API:
Setup:
1beforeEach(() => { 2 request = new Request(); 3 request.setSubdomains(subdomains); 4});
request.xhr
Ways to use this API:
Setup:
1beforeEach(() => { 2 request = new Request(); 3 request.setXhr(boolean); 4});
request.accepts()
Ways to use this API:
1expect(request.accepts).toBeCalledWith(types);
request.acceptsCharsets()
Ways to use this API:
1expect(request.acceptsCharsets).toBeCalledWith(charset [, ...]);
request.acceptsEncodings()
Ways to use this API:
1expect(request.acceptsEncodings).toBeCalledWith(encoding [, ...]);
request.acceptsLanguages()
Ways to use this API:
1expect(request.acceptsLanguages).toBeCalledWith(lang [, ...]);
request.get()
Ways to use this API:
1expect(request.get).toBeCalledWith(field);
request.is()
Ways to use this API:
1expect(request.is).toBeCalledWith(type);
request.param()
Ways to use this API:
1expect(request.param).toBeCalledWith(name [, defaultValue]);
request.range()
Ways to use this API:
1expect(request.range).toBeCalledWith(size[, options]);
Response
How to setup Response to use in Jest:
1import { Response } from 'jest-express/lib/response'; 2import { endpoint } from '../src/endpoint.js'; 3 4let response; 5 6describe('Endpoint', () => { 7 beforeEach(() => { 8 response = new Response(); 9 }); 10 11 afterEach(() => { 12 response.resetMocked(); 13 }); 14 15 test('should setup endpoint', () => { 16 endpoint(response); 17 18 expect(response).toBeCalled(); 19 }); 20});
response.setHeader
Ways to use this API:
Setup:
1beforeEach(() => {
2 response = new Response();
3 response.setHeader(key, value);
4 expect(response.setHeader).toBeCalledWith(key, value);
5});
response.removeHeader
Ways to use this API:
Setup:
1beforeEach(() => { 2 response = new Response(); 3 response.removeHeader(key); 4 expect(response.removeHeader).toBeCalledWith(key); 5});
response.headersSent
Ways to use this API:
Setup:
1beforeEach(() => { 2 response = new Response(); 3 response.setHeadersSent(boolean); 4});
response.locals
Ways to use this API:
Setup:
1beforeEach(() => { 2 response = new Response(); 3 response.setLocals('title', 'My App'); 4});
response.append()
Ways to use this API:
1expect(response.append).toBeCalledWith(field [, value]);
response.attachment()
Ways to use this API:
1expect(response.attachment).toBeCalledWith([filename]);
reponse.body
Ways to use this API:
1expect(response.body).toEqual(value);
response.cookie()
Ways to use this API:
1expect(response.cookie).toBeCalledWith(name, value [, options]);
response.clearCookie()
Ways to use this API:
1expect(response.clearCookie).toBeCalledWith(name [, options]);
response.download()
Ways to use this API:
1expect(response.download).toBeCalledWith(path [, filename] [, options] [, fn]);
response.end()
Ways to use this API:
1expect(response.end).toBeCalledWith([data] [, encoding]);
response.format()
Ways to use this API:
1expect(response.format).toBeCalledWith(object);
response.get()
Ways to use this API:
1expect(response.get).toBeCalledWith(field);
response.getHeader()
Ways to use this API:
1response.setHeader('Accept', 'text/html') 2expect(response.getHeader('Accept')).toEqual('text/html');
response.header()
An alias for response.set()
1expect(response.header).toBeCalledWith(field, [value]);
response.json()
Ways to use this API:
1expect(response.json).toBeCalledWith([body]);
response.jsonp()
Ways to use this API:
1expect(response.jsonp).toBeCalledWith([body]);
response.links()
Ways to use this API:
1expect(response.links).toBeCalledWith(links);
response.location()
Ways to use this API:
1expect(response.location).toBeCalledWith(path);
response.redirect()
Ways to use this API:
1expect(response.redirect).toBeCalledWith([status,] path);
response.render()
Ways to use this API:
1expect(response.render).toBeCalledWith(view [, locals] [, callback]);
response.send()
Ways to use this API:
1expect(response.send).toBeCalledWith([body]);
response.sendFile()
Ways to use this API:
1expect(response.sendFile).toBeCalledWith(path [, options] [, fn]);
response.sendStatus()
Ways to use this API:
1expect(response.sendStatus).toBeCalledWith(statusCode);
response.set()
Sets headers. It is calling response.setHeader()
internally.
1expect(response.set).toBeCalledWith(field [, value]);
response.status()
Ways to use this API:
1expect(response.status).toBeCalledWith(code);
response.statusCode
ways to use this API:
1expect(response.statusCode).toEqual(code);
response.type()
Ways to use this API:
1expect(response.type).toBeCalledWith(type);
response.vary()
Ways to use this API:
1expect(response.vary).toBeCalledWith(field);
Router
How to setup Response to use in Jest:
1import { Router } from 'jest-express/lib/router'; 2import { endpoint } from '../src/endpoint.js'; 3 4let router; 5 6describe('Endpoint', () => { 7 beforeEach(() => { 8 router = new Router(); 9 }); 10 11 afterEach(() => { 12 router.resetMocked(); 13 }); 14 15 test('should setup endpoint', () => { 16 endpoint(router); 17 18 expect(router).toBeCalled(); 19 }); 20});
router.all()
Ways to use this API:
1expect(router.all).toBeCalledWith(path, [callback, ...] callback);
router.get()
Ways to use this API:
1expect(router.get).toBeCalledWith(path, [callback, ...] callback);
router.param()
Ways to use this API:
1expect(router.param).toBeCalledWith(name, callback);
router.route()
Ways to use this API:
1expect(router.route).toBeCalledWith(path);
router.use()
Ways to use this API:
1expect(router.use).toBeCalledWith([path], [function, ...] function);
resetMocked()
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.
1$ git clone git@github.com:jameswlane/jest-express.git 2$ cd jest-express 3$ npm install
Linters:
1$ npm run tslint
Tests:
1$ npm test
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
12 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-02-10
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More