@jest-mock/express
Advanced tools
Comparing version 1.1.9 to 1.2.0
@@ -56,3 +56,4 @@ import { MediaType } from 'express-serve-static-core'; | ||
next?: any; | ||
[key: string]: any; | ||
} | ||
export {}; |
import { Request } from 'express'; | ||
import { MockRequest } from './index'; | ||
import type { MockRequest } from './index'; | ||
export declare const getMockReq: <T extends Request<import("express-serve-static-core").ParamsDictionary, any, any, import("express-serve-static-core").Query>>(values?: MockRequest) => T; | ||
export default getMockReq; |
"use strict"; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getMockReq = void 0; | ||
exports.getMockReq = (values = {}) => { | ||
return { | ||
params: (values && values.params) || {}, | ||
query: (values && values.query) || {}, | ||
body: (values && values.body) || {}, | ||
cookies: (values && values.cookies) || {}, | ||
method: (values && values.method) || '', | ||
protocol: (values && values.protocol) || '', | ||
secure: (values && values.secure) || false, | ||
ip: (values && values.ip) || '', | ||
ips: (values && values.ips) || [], | ||
subdomains: (values && values.subdomains) || [], | ||
path: (values && values.path) || '', | ||
hostname: (values && values.hostname) || '', | ||
host: (values && values.host) || '', | ||
fresh: (values && values.fresh) || false, | ||
stale: (values && values.stale) || false, | ||
xhr: (values && values.xhr) || false, | ||
route: (values && values.route) || {}, | ||
signedCookies: (values && values.signedCookies) || {}, | ||
originalUrl: (values && values.originalUrl) || '', | ||
url: (values && values.url) || '', | ||
baseUrl: (values && values.baseUrl) || '', | ||
accepted: (values && values.accepted) || [], | ||
get: (values && values.get) || jest.fn().mockName('get mock default'), | ||
header: (values && values.header) || jest.fn().mockName('header mock default'), | ||
accepts: (values && values.accepts) || jest.fn().mockName('accepts mock default'), | ||
acceptsCharsets: (values && values.acceptsCharsets) || jest.fn().mockName('acceptsCharsets mock default'), | ||
acceptsEncodings: (values && values.acceptsEncodings) || jest.fn().mockName('acceptsEncodings mock default'), | ||
acceptsLanguages: (values && values.acceptsLanguages) || jest.fn().mockName('acceptsLanguages mock default'), | ||
range: (values && values.range) || jest.fn().mockName('range mock default'), | ||
param: (values && values.param) || jest.fn().mockName('param mock default'), | ||
is: (values && values.is) || jest.fn().mockName('is mock default'), | ||
app: (values && values.app) || {}, | ||
res: (values && values.res) || jest.fn().mockName('res mock default'), | ||
next: (values && values.next) || jest.fn().mockName('next mock default'), | ||
const { params = {}, query = {}, body = {}, cookies = {}, method = '', protocol = '', secure = false, ip = '', ips = [], subdomains = [], path = '', hostname = '', host = '', fresh = false, stale = false, xhr = false, route = {}, signedCookies = {}, originalUrl = '', url = '', baseUrl = '', accepted = [], get = jest.fn().mockName('get mock default'), header = jest.fn().mockName('header mock default'), accepts = jest.fn().mockName('accepts mock default'), acceptsCharsets = jest.fn().mockName('acceptsCharsets mock default'), acceptsEncodings = jest.fn().mockName('acceptsEncodings mock default'), acceptsLanguages = jest.fn().mockName('acceptsLanguages mock default'), range = jest.fn().mockName('range mock default'), param = jest.fn().mockName('param mock default'), is = jest.fn().mockName('is mock default'), app = {}, res = jest.fn().mockName('res mock default'), next = jest.fn().mockName('next mock default'), aborted = false, httpVersion = '', httpVersionMajor = 0, httpVersionMinor = 0, complete = false, connection = {}, socket = {}, headers = {}, rawHeaders = [], trailers = {}, rawTrailers = [], setTimeout = jest.fn().mockName('setTimeout mock default'), statusCode = 0, statusMessage = '', destroy = jest.fn().mockName('destroy mock default') } = values, extraProvidedValues = __rest(values, ["params", "query", "body", "cookies", "method", "protocol", "secure", "ip", "ips", "subdomains", "path", "hostname", "host", "fresh", "stale", "xhr", "route", "signedCookies", "originalUrl", "url", "baseUrl", "accepted", "get", "header", "accepts", "acceptsCharsets", "acceptsEncodings", "acceptsLanguages", "range", "param", "is", "app", "res", "next", "aborted", "httpVersion", "httpVersionMajor", "httpVersionMinor", "complete", "connection", "socket", "headers", "rawHeaders", "trailers", "rawTrailers", "setTimeout", "statusCode", "statusMessage", "destroy"]); | ||
return Object.assign({ params, | ||
query, | ||
body, | ||
cookies, | ||
method, | ||
protocol, | ||
secure, | ||
ip, | ||
ips, | ||
subdomains, | ||
path, | ||
hostname, | ||
host, | ||
fresh, | ||
stale, | ||
xhr, | ||
route, | ||
signedCookies, | ||
originalUrl, | ||
url, | ||
baseUrl, | ||
accepted, | ||
get, | ||
header, | ||
accepts, | ||
acceptsCharsets, | ||
acceptsEncodings, | ||
acceptsLanguages, | ||
range, | ||
param, | ||
is, | ||
app, | ||
res, | ||
next, | ||
// http - IncomingMessage | ||
aborted: (values && values.aborted) || false, | ||
httpVersion: (values && values.httpVersion) || '', | ||
httpVersionMajor: (values && values.httpVersionMajor) || 0, | ||
httpVersionMinor: (values && values.httpVersionMinor) || 0, | ||
complete: (values && values.complete) || false, | ||
connection: (values && values.connection) || {}, | ||
socket: (values && values.socket) || {}, | ||
headers: (values && values.headers) || {}, | ||
rawHeaders: (values && values.rawHeaders) || [], | ||
trailers: (values && values.trailers) || {}, | ||
rawTrailers: (values && values.rawTrailers) || [], | ||
setTimeout: (values && values.setTimeout) || jest.fn().mockName('setTimeout mock default'), | ||
statusCode: (values && values.statusCode) || 0, | ||
statusMessage: (values && values.statusMessage) || '', | ||
destroy: (values && values.destroy) || jest.fn().mockName('destroy mock default'), | ||
}; | ||
aborted, | ||
httpVersion, | ||
httpVersionMajor, | ||
httpVersionMinor, | ||
complete, | ||
connection, | ||
socket, | ||
headers, | ||
rawHeaders, | ||
trailers, | ||
rawTrailers, | ||
setTimeout, | ||
statusCode, | ||
statusMessage, | ||
destroy }, extraProvidedValues); | ||
}; | ||
exports.default = exports.getMockReq; | ||
//# sourceMappingURL=request.js.map |
@@ -12,19 +12,19 @@ "use strict"; | ||
// the request contains values | ||
expect(testReq).toBeTruthy(); | ||
expect(testReq).toBeDefined(); | ||
expect(testReq).toBeInstanceOf(Object); | ||
expect(Object.keys(testReq).length).toBe(49); | ||
// req.params is an empty object | ||
expect(testReq.params).toBeTruthy(); | ||
expect(testReq.params).toBeDefined(); | ||
expect(testReq.params).toBeInstanceOf(Object); | ||
expect(Object.keys(testReq.params).length).toBe(0); | ||
// req.query is an empty object | ||
expect(testReq.query).toBeTruthy(); | ||
expect(testReq.query).toBeDefined(); | ||
expect(testReq.query).toBeInstanceOf(Object); | ||
expect(Object.keys(testReq.query).length).toBe(0); | ||
// req.body is an empty object | ||
expect(testReq.body).toBeTruthy(); | ||
expect(testReq.body).toBeDefined(); | ||
expect(testReq.body).toBeInstanceOf(Object); | ||
expect(Object.keys(testReq.body).length).toBe(0); | ||
// req.cookies is an empty object | ||
expect(testReq.cookies).toBeTruthy(); | ||
expect(testReq.cookies).toBeDefined(); | ||
expect(testReq.cookies).toBeInstanceOf(Object); | ||
@@ -59,7 +59,7 @@ expect(Object.keys(testReq.cookies).length).toBe(0); | ||
// req.route is an empty object | ||
expect(testReq.route).toBeTruthy(); | ||
expect(testReq.route).toBeDefined(); | ||
expect(testReq.route).toBeInstanceOf(Object); | ||
expect(Object.keys(testReq.route).length).toBe(0); | ||
// req.signedCookies is an empty object | ||
expect(testReq.signedCookies).toBeTruthy(); | ||
expect(testReq.signedCookies).toBeDefined(); | ||
expect(testReq.signedCookies).toBeInstanceOf(Object); | ||
@@ -95,3 +95,3 @@ expect(Object.keys(testReq.signedCookies).length).toBe(0); | ||
// req.app is an empty object | ||
expect(testReq.app).toBeTruthy(); | ||
expect(testReq.app).toBeDefined(); | ||
expect(testReq.app).toBeInstanceOf(Object); | ||
@@ -114,11 +114,11 @@ expect(Object.keys(testReq.app).length).toBe(0); | ||
// req.connection is an empty object | ||
expect(testReq.connection).toBeTruthy(); | ||
expect(testReq.connection).toBeDefined(); | ||
expect(testReq.connection).toBeInstanceOf(Object); | ||
expect(Object.keys(testReq.connection).length).toBe(0); | ||
// req.socket is an empty object | ||
expect(testReq.socket).toBeTruthy(); | ||
expect(testReq.socket).toBeDefined(); | ||
expect(testReq.socket).toBeInstanceOf(Object); | ||
expect(Object.keys(testReq.socket).length).toBe(0); | ||
// req.headers is an empty object | ||
expect(testReq.headers).toBeTruthy(); | ||
expect(testReq.headers).toBeDefined(); | ||
expect(testReq.headers).toBeInstanceOf(Object); | ||
@@ -130,3 +130,3 @@ expect(Object.keys(testReq.headers).length).toBe(0); | ||
// req.trailers is an empty object | ||
expect(testReq.trailers).toBeTruthy(); | ||
expect(testReq.trailers).toBeDefined(); | ||
expect(testReq.trailers).toBeInstanceOf(Object); | ||
@@ -149,3 +149,3 @@ expect(Object.keys(testReq.trailers).length).toBe(0); | ||
// req.params has the provided arguments | ||
expect(testReq.params).toBeTruthy(); | ||
expect(testReq.params).toBeDefined(); | ||
expect(testReq.params).toBeInstanceOf(Object); | ||
@@ -159,3 +159,3 @@ expect(Object.keys(testReq.params).length).toBe(2); | ||
// req.query has the provided arguments | ||
expect(testReq.query).toBeTruthy(); | ||
expect(testReq.query).toBeDefined(); | ||
expect(testReq.query).toBeInstanceOf(Object); | ||
@@ -169,3 +169,3 @@ expect(Object.keys(testReq.query).length).toBe(2); | ||
// req.body has the provided arguments | ||
expect(testReq.body).toBeTruthy(); | ||
expect(testReq.body).toBeDefined(); | ||
expect(testReq.body).toBeInstanceOf(Object); | ||
@@ -185,3 +185,3 @@ expect(Object.keys(testReq.body).length).toBe(2); | ||
// req.headers has the provided arguments | ||
expect(testReq.headers).toBeTruthy(); | ||
expect(testReq.headers).toBeDefined(); | ||
expect(testReq.headers).toBeInstanceOf(Object); | ||
@@ -193,2 +193,26 @@ expect(Object.keys(testReq.headers).length).toBe(3); | ||
}); | ||
test('allows custom properties', () => { | ||
const mockUser = { | ||
id: '123', | ||
name: 'Bob', | ||
}; | ||
const testReq = request_1.default({ | ||
user: mockUser, | ||
query: { | ||
id: '123', | ||
limit: 10, | ||
page: 2, | ||
}, | ||
}); | ||
// req.user has the provided arguments | ||
expect(testReq.user).toBeDefined(); | ||
expect(testReq.user).toBe(mockUser); | ||
// req.query has the provided arguments | ||
expect(testReq.query).toBeDefined(); | ||
expect(testReq.query).toBeInstanceOf(Object); | ||
expect(Object.keys(testReq.query).length).toBe(3); | ||
expect(testReq.query['id']).toBe('123'); | ||
expect(testReq.query['limit']).toBe(10); | ||
expect(testReq.query['page']).toBe(2); | ||
}); | ||
test('issue #6', () => { | ||
@@ -209,3 +233,3 @@ let AppOS; | ||
// req.query has the provided arguments | ||
expect(testReq.query).toBeTruthy(); | ||
expect(testReq.query).toBeDefined(); | ||
expect(testReq.query).toBeInstanceOf(Object); | ||
@@ -216,3 +240,3 @@ expect(Object.keys(testReq.query).length).toBe(2); | ||
// req.headers has the provided arguments | ||
expect(testReq.headers).toBeTruthy(); | ||
expect(testReq.headers).toBeDefined(); | ||
expect(testReq.headers).toBeInstanceOf(Object); | ||
@@ -222,3 +246,15 @@ expect(Object.keys(testReq.headers).length).toBe(1); | ||
}); | ||
test('issue #27', () => { | ||
const testReq = request_1.default({ | ||
locals: { var: 'hi there' }, | ||
customProperty: 'value', | ||
}); | ||
// req.locals has the provided arguments | ||
expect(testReq.locals).toBeDefined(); | ||
expect(testReq.locals).toEqual({ var: 'hi there' }); | ||
// req.customProperty has the provided arguments | ||
expect(testReq.customProperty).toBeDefined(); | ||
expect(testReq.customProperty).toEqual('value'); | ||
}); | ||
}); | ||
//# sourceMappingURL=request.test.js.map |
import { Response, NextFunction } from 'express'; | ||
import type { MockResponse } from './index'; | ||
/** | ||
@@ -6,3 +7,3 @@ * Will return a typed and mocked version of key Express components. | ||
*/ | ||
export declare const getMockRes: () => { | ||
export declare const getMockRes: (values?: MockResponse) => { | ||
res: Response; | ||
@@ -9,0 +10,0 @@ next: NextFunction; |
"use strict"; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -8,61 +19,24 @@ exports.getMockRes = void 0; | ||
*/ | ||
exports.getMockRes = () => { | ||
exports.getMockRes = (values = {}) => { | ||
const next = jest.fn(); | ||
const response = { | ||
const { headersSent = false, locals = {}, charset = '', app = {}, req = {}, statusCode = 0, statusMessage = '', upgrading = false, chunkedEncoding = false, shouldKeepAlive = false, useChunkedEncodingByDefault = false, sendDate = false, finished = false, connection = {}, socket = {} } = values, extraProvidedValues = __rest(values, ["headersSent", "locals", "charset", "app", "req", "statusCode", "statusMessage", "upgrading", "chunkedEncoding", "shouldKeepAlive", "useChunkedEncodingByDefault", "sendDate", "finished", "connection", "socket"]); | ||
const response = Object.assign({ | ||
// express - Response | ||
status: jest.fn(), | ||
sendStatus: jest.fn(), | ||
links: jest.fn(), | ||
send: jest.fn(), | ||
json: jest.fn(), | ||
jsonp: jest.fn(), | ||
sendFile: jest.fn(), | ||
sendfile: jest.fn(), | ||
download: jest.fn(), | ||
contentType: jest.fn(), | ||
type: jest.fn(), | ||
format: jest.fn(), | ||
attachment: jest.fn(), | ||
set: jest.fn(), | ||
header: jest.fn(), | ||
get: jest.fn(), | ||
clearCookie: jest.fn(), | ||
cookie: jest.fn(), | ||
location: jest.fn(), | ||
redirect: jest.fn(), | ||
render: jest.fn(), | ||
vary: jest.fn(), | ||
append: jest.fn(), | ||
headersSent: false, | ||
locals: {}, | ||
charset: '', | ||
app: {}, | ||
req: {}, | ||
status: jest.fn(), sendStatus: jest.fn(), links: jest.fn(), send: jest.fn(), json: jest.fn(), jsonp: jest.fn(), sendFile: jest.fn(), sendfile: jest.fn(), download: jest.fn(), contentType: jest.fn(), type: jest.fn(), format: jest.fn(), attachment: jest.fn(), set: jest.fn(), header: jest.fn(), get: jest.fn(), clearCookie: jest.fn(), cookie: jest.fn(), location: jest.fn(), redirect: jest.fn(), render: jest.fn(), vary: jest.fn(), append: jest.fn(), headersSent, | ||
locals, | ||
charset, | ||
app, | ||
req, | ||
// http - ServerResponse | ||
assignSocket: jest.fn(), | ||
detachSocket: jest.fn(), | ||
writeContinue: jest.fn(), | ||
writeHead: jest.fn(), | ||
writeProcessing: jest.fn(), | ||
statusCode: 0, | ||
statusMessage: '', | ||
assignSocket: jest.fn(), detachSocket: jest.fn(), writeContinue: jest.fn(), writeHead: jest.fn(), writeProcessing: jest.fn(), statusCode, | ||
statusMessage, | ||
// http - OutgoingMessage | ||
setTimeout: jest.fn(), | ||
setHeader: jest.fn(), | ||
getHeader: jest.fn(), | ||
getHeaders: jest.fn(), | ||
getHeaderNames: jest.fn(), | ||
hasHeader: jest.fn(), | ||
removeHeader: jest.fn(), | ||
addTrailers: jest.fn(), | ||
flushHeaders: jest.fn(), | ||
upgrading: false, | ||
chunkedEncoding: false, | ||
shouldKeepAlive: false, | ||
useChunkedEncodingByDefault: false, | ||
sendDate: false, | ||
finished: false, | ||
connection: {}, | ||
socket: {}, | ||
}; | ||
setTimeout: jest.fn(), setHeader: jest.fn(), getHeader: jest.fn(), getHeaders: jest.fn(), getHeaderNames: jest.fn(), hasHeader: jest.fn(), removeHeader: jest.fn(), addTrailers: jest.fn(), flushHeaders: jest.fn(), upgrading, | ||
chunkedEncoding, | ||
shouldKeepAlive, | ||
useChunkedEncodingByDefault, | ||
sendDate, | ||
finished, | ||
connection, | ||
socket }, extraProvidedValues); | ||
// express - Response - chainable functions | ||
@@ -69,0 +43,0 @@ response.status.mockReturnValue(response); |
@@ -8,2 +8,3 @@ "use strict"; | ||
const response_1 = __importDefault(require("./response")); | ||
const DEFAULT_RES_KEY_LENGTH = 52; | ||
describe('getMockRes', () => { | ||
@@ -19,4 +20,4 @@ test('returns expected object', () => { | ||
// res contains the expected functions | ||
expect(res).toBeTruthy(); | ||
expect(Object.keys(res).length).toBe(52); | ||
expect(res).toBeDefined(); | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH); | ||
// express - Response | ||
@@ -63,6 +64,195 @@ expect(typeof res.status).toBe('function'); | ||
}); | ||
test('provides default for, and allows overrides for headersSent', () => { | ||
// default value is provided | ||
const { res: defaultRes } = response_1.default(); | ||
expect(defaultRes.headersSent).toBe(false); | ||
// allows setting an override value | ||
const { res } = response_1.default({ headersSent: true }); | ||
// does not change the core res object | ||
expect(res).toBeDefined(); | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH); | ||
expect(res.headersSent).toBe(true); | ||
}); | ||
test('provides default for, and allows overrides for locals', () => { | ||
// default value is provided | ||
const { res: defaultRes } = response_1.default(); | ||
expect(defaultRes.locals).toEqual({}); | ||
// allows setting an override value | ||
const mockLocals = { one: 1, two: 2 }; | ||
const { res } = response_1.default({ locals: mockLocals }); | ||
// does not change the core res object | ||
expect(res).toBeDefined(); | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH); | ||
expect(res.locals).toBe(mockLocals); | ||
}); | ||
test('provides default for, and allows overrides for charset', () => { | ||
// default value is provided | ||
const { res: defaultRes } = response_1.default(); | ||
expect(defaultRes.charset).toBe(''); | ||
// allows setting an override value | ||
const { res } = response_1.default({ charset: 'custom' }); | ||
// does not change the core res object | ||
expect(res).toBeDefined(); | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH); | ||
expect(res.charset).toBe('custom'); | ||
}); | ||
test('provides default for, and allows overrides for app', () => { | ||
// default value is provided | ||
const { res: defaultRes } = response_1.default(); | ||
expect(defaultRes.app).toEqual({}); | ||
// allows setting an override value | ||
const mockApp = { one: 1, two: 2 }; | ||
const { res } = response_1.default({ app: mockApp }); | ||
// does not change the core res object | ||
expect(res).toBeDefined(); | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH); | ||
expect(res.app).toBe(mockApp); | ||
}); | ||
test('provides default for, and allows overrides for req', () => { | ||
// default value is provided | ||
const { res: defaultRes } = response_1.default(); | ||
expect(defaultRes.req).toEqual({}); | ||
// allows setting an override value | ||
const mockReq = { one: 1, two: 2 }; | ||
const { res } = response_1.default({ req: mockReq }); | ||
// does not change the core res object | ||
expect(res).toBeDefined(); | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH); | ||
expect(res.req).toBe(mockReq); | ||
}); | ||
test('provides default for, and allows overrides for statusCode', () => { | ||
// default value is provided | ||
const { res: defaultRes } = response_1.default(); | ||
expect(defaultRes.statusCode).toBe(0); | ||
// allows setting an override value | ||
const { res } = response_1.default({ statusCode: 500 }); | ||
// does not change the core res object | ||
expect(res).toBeDefined(); | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH); | ||
expect(res.statusCode).toBe(500); | ||
}); | ||
test('provides default for, and allows overrides for statusMessage', () => { | ||
// default value is provided | ||
const { res: defaultRes } = response_1.default(); | ||
expect(defaultRes.statusMessage).toBe(''); | ||
// allows setting an override value | ||
const { res } = response_1.default({ statusMessage: 'custom' }); | ||
// does not change the core res object | ||
expect(res).toBeDefined(); | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH); | ||
expect(res.statusMessage).toBe('custom'); | ||
}); | ||
test('provides default for, and allows overrides for upgrading', () => { | ||
// default value is provided | ||
const { res: defaultRes } = response_1.default(); | ||
expect(defaultRes.upgrading).toBe(false); | ||
// allows setting an override value | ||
const { res } = response_1.default({ upgrading: true }); | ||
// does not change the core res object | ||
expect(res).toBeDefined(); | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH); | ||
expect(res.upgrading).toBe(true); | ||
}); | ||
test('provides default for, and allows overrides for chunkedEncoding', () => { | ||
// default value is provided | ||
const { res: defaultRes } = response_1.default(); | ||
expect(defaultRes.chunkedEncoding).toBe(false); | ||
// allows setting an override value | ||
const { res } = response_1.default({ chunkedEncoding: true }); | ||
// does not change the core res object | ||
expect(res).toBeDefined(); | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH); | ||
expect(res.chunkedEncoding).toBe(true); | ||
}); | ||
test('provides default for, and allows overrides for shouldKeepAlive', () => { | ||
// default value is provided | ||
const { res: defaultRes } = response_1.default(); | ||
expect(defaultRes.shouldKeepAlive).toBe(false); | ||
// allows setting an override value | ||
const { res } = response_1.default({ shouldKeepAlive: true }); | ||
// does not change the core res object | ||
expect(res).toBeDefined(); | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH); | ||
expect(res.shouldKeepAlive).toBe(true); | ||
}); | ||
test('provides default for, and allows overrides for useChunkedEncodingByDefault', () => { | ||
// default value is provided | ||
const { res: defaultRes } = response_1.default(); | ||
expect(defaultRes.useChunkedEncodingByDefault).toBe(false); | ||
// allows setting an override value | ||
const { res } = response_1.default({ useChunkedEncodingByDefault: true }); | ||
// does not change the core res object | ||
expect(res).toBeDefined(); | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH); | ||
expect(res.useChunkedEncodingByDefault).toBe(true); | ||
}); | ||
test('provides default for, and allows overrides for sendDate', () => { | ||
// default value is provided | ||
const { res: defaultRes } = response_1.default(); | ||
expect(defaultRes.sendDate).toBe(false); | ||
// allows setting an override value | ||
const { res } = response_1.default({ sendDate: true }); | ||
// does not change the core res object | ||
expect(res).toBeDefined(); | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH); | ||
expect(res.sendDate).toBe(true); | ||
}); | ||
test('provides default for, and allows overrides for finished', () => { | ||
// default value is provided | ||
const { res: defaultRes } = response_1.default(); | ||
expect(defaultRes.finished).toBe(false); | ||
// allows setting an override value | ||
const { res } = response_1.default({ finished: true }); | ||
// does not change the core res object | ||
expect(res).toBeDefined(); | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH); | ||
expect(res.finished).toBe(true); | ||
}); | ||
test('provides default for, and allows overrides for connection', () => { | ||
// default value is provided | ||
const { res: defaultRes } = response_1.default(); | ||
expect(defaultRes.connection).toEqual({}); | ||
// allows setting an override value | ||
const mockConnection = { one: 1, two: 2 }; | ||
const { res } = response_1.default({ connection: mockConnection }); | ||
// does not change the core res object | ||
expect(res).toBeDefined(); | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH); | ||
expect(res.connection).toBe(mockConnection); | ||
}); | ||
test('provides default for, and allows overrides for socket', () => { | ||
// default value is provided | ||
const { res: defaultRes } = response_1.default(); | ||
expect(defaultRes.socket).toEqual({}); | ||
// allows setting an override value | ||
const mockSocket = { one: 1, two: 2 }; | ||
const { res } = response_1.default({ socket: mockSocket }); | ||
// does not change the core res object | ||
expect(res).toBeDefined(); | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH); | ||
expect(res.socket).toBe(mockSocket); | ||
}); | ||
test('allows custom properties, with casting', () => { | ||
const mockUser = { | ||
id: '123', | ||
name: 'Bob', | ||
}; | ||
// default value is not provided | ||
const { res: defaultRes } = response_1.default(); | ||
const castedDefaultRes = defaultRes; | ||
expect(castedDefaultRes.user).toBeUndefined(); | ||
// allows setting a custom property | ||
const { res } = response_1.default({ sendDate: true, user: mockUser }); | ||
const castedRes = res; | ||
// adds and extra property to the res object | ||
expect(castedRes).toBeDefined(); | ||
expect(Object.keys(castedRes).length).toBe(DEFAULT_RES_KEY_LENGTH + 1); | ||
// both properties are available | ||
expect(castedRes.sendDate).toBe(true); | ||
expect(castedRes.user).toBe(mockUser); | ||
}); | ||
test('the mock next function is provided', () => { | ||
const { next } = response_1.default(); | ||
// next is part of the mock response | ||
expect(next).toBeTruthy(); | ||
expect(next).toBeDefined(); | ||
expect(typeof next).toBe('function'); | ||
@@ -73,3 +263,3 @@ }); | ||
// a mock clear function is part of the mock response | ||
expect(mockClear).toBeTruthy(); | ||
expect(mockClear).toBeDefined(); | ||
expect(typeof mockClear).toBe('function'); | ||
@@ -80,3 +270,3 @@ }); | ||
// a mock clear alias function is part of the mock response | ||
expect(clearMockRes).toBeTruthy(); | ||
expect(clearMockRes).toBeDefined(); | ||
expect(typeof clearMockRes).toBe('function'); | ||
@@ -83,0 +273,0 @@ }); |
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
coveragePathIgnorePatterns: ['<rootDir>/dist'], | ||
} |
{ | ||
"name": "@jest-mock/express", | ||
"version": "1.1.9", | ||
"version": "1.2.0", | ||
"description": "A lightweight Jest mock for unit testing Express", | ||
@@ -43,15 +43,15 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@types/express": "^4.17.8", | ||
"@types/jest": "^26.0.14", | ||
"@typescript-eslint/eslint-plugin": "^4.4.0", | ||
"@typescript-eslint/parser": "^4.4.0", | ||
"@types/express": "^4.17.9", | ||
"@types/jest": "^26.0.15", | ||
"@typescript-eslint/eslint-plugin": "^4.8.1", | ||
"@typescript-eslint/parser": "^4.8.1", | ||
"coveralls": "^3.0.7", | ||
"eslint": "^7.11.0", | ||
"eslint-config-prettier": "^6.12.0", | ||
"eslint": "^7.13.0", | ||
"eslint-config-prettier": "^6.15.0", | ||
"eslint-plugin-prettier": "^3.1.1", | ||
"jest": "^26.5.2", | ||
"jest": "^26.6.3", | ||
"prettier": "^2.1.2", | ||
"snyk": "^1.413.2", | ||
"ts-jest": "^26.4.1", | ||
"typescript": "^4.0.3" | ||
"snyk": "^1.428.0", | ||
"ts-jest": "^26.4.4", | ||
"typescript": "^4.0.5" | ||
}, | ||
@@ -58,0 +58,0 @@ "dependencies": {}, |
@@ -30,3 +30,3 @@ # @jest-mock/express | ||
`getMockReq` is intended to mock the `req` object as easily as possible. In its simplest form you can call it with no arguments to return a standard `req` with no values. | ||
`getMockReq` is intended to mock the `req` object as easily as possible. In its simplest form you can call it with no arguments to return a standard `req` object with mocked functions and default values for properties. | ||
@@ -37,3 +37,3 @@ ```typescript | ||
To create mock requests with values, you can simply provide them to the function in any order with all being optional. The advantage of this is that it ensures all of the other properties are not undefined. | ||
To create mock requests with specific values, you can simply provide them to the function in any order, with all being optional. The advantage of this is that it ensures the other properties are not undefined. Loose type definitions for standard properties are provided, custom properties (`[key: string]: any`) will be passed through to the returned `req` object. | ||
@@ -73,2 +73,12 @@ ```typescript | ||
To create mock responses with values, you can simply provide them to the function in any order, with all being optional. Loose type definitions for standard properties are provided, custom properties (`[key: string]: any`) will be passed through to the returned `res` object. | ||
```typescript | ||
const { res, next, clearMockRes } = getMockRes({ | ||
locals: { | ||
user: getLoggedInUser(), | ||
}, | ||
}) | ||
``` | ||
### Example | ||
@@ -79,9 +89,14 @@ | ||
```typescript | ||
const { res, next } = getMockRes() | ||
// generate a mocked response and next function, with provided values | ||
const { res, next } = getMockRes({ | ||
locals: { | ||
isPremiumUser: true, | ||
}, | ||
}) | ||
test('will respond with the entity from the service', async () => { | ||
// generate a mock request | ||
// generate a mock request with params | ||
const req = getMockReq({ params: { id: 'abc-def' } }) | ||
// provide the mock req, res, and next to check against | ||
// provide the mock req, res, and next to assert | ||
await myController.getEntity(req, res, next) | ||
@@ -88,0 +103,0 @@ |
@@ -57,2 +57,4 @@ // Types | ||
next?: any | ||
// allow custom properties to be provided | ||
[key: string]: any | ||
} |
@@ -0,1 +1,4 @@ | ||
// Types | ||
import type { Request } from 'express' | ||
// Tested Module | ||
@@ -9,3 +12,3 @@ import getMockReq from './request' | ||
// the request contains values | ||
expect(testReq).toBeTruthy() | ||
expect(testReq).toBeDefined() | ||
expect(testReq).toBeInstanceOf(Object) | ||
@@ -15,3 +18,3 @@ expect(Object.keys(testReq).length).toBe(49) | ||
// req.params is an empty object | ||
expect(testReq.params).toBeTruthy() | ||
expect(testReq.params).toBeDefined() | ||
expect(testReq.params).toBeInstanceOf(Object) | ||
@@ -21,3 +24,3 @@ expect(Object.keys(testReq.params).length).toBe(0) | ||
// req.query is an empty object | ||
expect(testReq.query).toBeTruthy() | ||
expect(testReq.query).toBeDefined() | ||
expect(testReq.query).toBeInstanceOf(Object) | ||
@@ -27,3 +30,3 @@ expect(Object.keys(testReq.query).length).toBe(0) | ||
// req.body is an empty object | ||
expect(testReq.body).toBeTruthy() | ||
expect(testReq.body).toBeDefined() | ||
expect(testReq.body).toBeInstanceOf(Object) | ||
@@ -33,3 +36,3 @@ expect(Object.keys(testReq.body).length).toBe(0) | ||
// req.cookies is an empty object | ||
expect(testReq.cookies).toBeTruthy() | ||
expect(testReq.cookies).toBeDefined() | ||
expect(testReq.cookies).toBeInstanceOf(Object) | ||
@@ -77,3 +80,3 @@ expect(Object.keys(testReq.cookies).length).toBe(0) | ||
// req.route is an empty object | ||
expect(testReq.route).toBeTruthy() | ||
expect(testReq.route).toBeDefined() | ||
expect(testReq.route).toBeInstanceOf(Object) | ||
@@ -83,3 +86,3 @@ expect(Object.keys(testReq.route).length).toBe(0) | ||
// req.signedCookies is an empty object | ||
expect(testReq.signedCookies).toBeTruthy() | ||
expect(testReq.signedCookies).toBeDefined() | ||
expect(testReq.signedCookies).toBeInstanceOf(Object) | ||
@@ -129,3 +132,3 @@ expect(Object.keys(testReq.signedCookies).length).toBe(0) | ||
// req.app is an empty object | ||
expect(testReq.app).toBeTruthy() | ||
expect(testReq.app).toBeDefined() | ||
expect(testReq.app).toBeInstanceOf(Object) | ||
@@ -156,3 +159,3 @@ expect(Object.keys(testReq.app).length).toBe(0) | ||
// req.connection is an empty object | ||
expect(testReq.connection).toBeTruthy() | ||
expect(testReq.connection).toBeDefined() | ||
expect(testReq.connection).toBeInstanceOf(Object) | ||
@@ -162,3 +165,3 @@ expect(Object.keys(testReq.connection).length).toBe(0) | ||
// req.socket is an empty object | ||
expect(testReq.socket).toBeTruthy() | ||
expect(testReq.socket).toBeDefined() | ||
expect(testReq.socket).toBeInstanceOf(Object) | ||
@@ -168,3 +171,3 @@ expect(Object.keys(testReq.socket).length).toBe(0) | ||
// req.headers is an empty object | ||
expect(testReq.headers).toBeTruthy() | ||
expect(testReq.headers).toBeDefined() | ||
expect(testReq.headers).toBeInstanceOf(Object) | ||
@@ -178,3 +181,3 @@ expect(Object.keys(testReq.headers).length).toBe(0) | ||
// req.trailers is an empty object | ||
expect(testReq.trailers).toBeTruthy() | ||
expect(testReq.trailers).toBeDefined() | ||
expect(testReq.trailers).toBeInstanceOf(Object) | ||
@@ -204,3 +207,3 @@ expect(Object.keys(testReq.trailers).length).toBe(0) | ||
// req.params has the provided arguments | ||
expect(testReq.params).toBeTruthy() | ||
expect(testReq.params).toBeDefined() | ||
expect(testReq.params).toBeInstanceOf(Object) | ||
@@ -216,3 +219,3 @@ expect(Object.keys(testReq.params).length).toBe(2) | ||
// req.query has the provided arguments | ||
expect(testReq.query).toBeTruthy() | ||
expect(testReq.query).toBeDefined() | ||
expect(testReq.query).toBeInstanceOf(Object) | ||
@@ -228,3 +231,3 @@ expect(Object.keys(testReq.query).length).toBe(2) | ||
// req.body has the provided arguments | ||
expect(testReq.body).toBeTruthy() | ||
expect(testReq.body).toBeDefined() | ||
expect(testReq.body).toBeInstanceOf(Object) | ||
@@ -246,3 +249,3 @@ expect(Object.keys(testReq.body).length).toBe(2) | ||
// req.headers has the provided arguments | ||
expect(testReq.headers).toBeTruthy() | ||
expect(testReq.headers).toBeDefined() | ||
expect(testReq.headers).toBeInstanceOf(Object) | ||
@@ -255,2 +258,38 @@ expect(Object.keys(testReq.headers).length).toBe(3) | ||
test('allows custom properties', () => { | ||
interface User { | ||
id: string | ||
name: string | ||
} | ||
interface CustomRequest extends Request { | ||
user: User | ||
} | ||
const mockUser: User = { | ||
id: '123', | ||
name: 'Bob', | ||
} | ||
const testReq = getMockReq<CustomRequest>({ | ||
user: mockUser, | ||
query: { | ||
id: '123', | ||
limit: 10, | ||
page: 2, | ||
}, | ||
}) | ||
// req.user has the provided arguments | ||
expect(testReq.user).toBeDefined() | ||
expect(testReq.user).toBe(mockUser) | ||
// req.query has the provided arguments | ||
expect(testReq.query).toBeDefined() | ||
expect(testReq.query).toBeInstanceOf(Object) | ||
expect(Object.keys(testReq.query).length).toBe(3) | ||
expect(testReq.query['id']).toBe('123') | ||
expect(testReq.query['limit']).toBe(10) | ||
expect(testReq.query['page']).toBe(2) | ||
}) | ||
test('issue #6', () => { | ||
@@ -272,3 +311,3 @@ enum AppOS { | ||
// req.query has the provided arguments | ||
expect(testReq.query).toBeTruthy() | ||
expect(testReq.query).toBeDefined() | ||
expect(testReq.query).toBeInstanceOf(Object) | ||
@@ -280,3 +319,3 @@ expect(Object.keys(testReq.query).length).toBe(2) | ||
// req.headers has the provided arguments | ||
expect(testReq.headers).toBeTruthy() | ||
expect(testReq.headers).toBeDefined() | ||
expect(testReq.headers).toBeInstanceOf(Object) | ||
@@ -286,2 +325,22 @@ expect(Object.keys(testReq.headers).length).toBe(1) | ||
}) | ||
test('issue #27', () => { | ||
interface CustomRequest extends Request { | ||
locals: any | ||
customProperty: string | ||
} | ||
const testReq = getMockReq<CustomRequest>({ | ||
locals: { var: 'hi there' }, | ||
customProperty: 'value', | ||
}) | ||
// req.locals has the provided arguments | ||
expect(testReq.locals).toBeDefined() | ||
expect(testReq.locals).toEqual({ var: 'hi there' }) | ||
// req.customProperty has the provided arguments | ||
expect(testReq.customProperty).toBeDefined() | ||
expect(testReq.customProperty).toEqual('value') | ||
}) | ||
}) |
@@ -5,57 +5,113 @@ // Libraries | ||
// Types | ||
import { MockRequest } from './index' | ||
import type { MockRequest } from './index' | ||
export const getMockReq = <T extends Request>(values: MockRequest = {}): T => { | ||
const { | ||
params = {}, | ||
query = {}, | ||
body = {}, | ||
cookies = {}, | ||
method = '', | ||
protocol = '', | ||
secure = false, | ||
ip = '', | ||
ips = [], | ||
subdomains = [], | ||
path = '', | ||
hostname = '', | ||
host = '', | ||
fresh = false, | ||
stale = false, | ||
xhr = false, | ||
route = {}, | ||
signedCookies = {}, | ||
originalUrl = '', | ||
url = '', | ||
baseUrl = '', | ||
accepted = [], | ||
get = jest.fn().mockName('get mock default'), | ||
header = jest.fn().mockName('header mock default'), | ||
accepts = jest.fn().mockName('accepts mock default'), | ||
acceptsCharsets = jest.fn().mockName('acceptsCharsets mock default'), | ||
acceptsEncodings = jest.fn().mockName('acceptsEncodings mock default'), | ||
acceptsLanguages = jest.fn().mockName('acceptsLanguages mock default'), | ||
range = jest.fn().mockName('range mock default'), | ||
param = jest.fn().mockName('param mock default'), | ||
is = jest.fn().mockName('is mock default'), | ||
app = {}, | ||
res = jest.fn().mockName('res mock default'), | ||
next = jest.fn().mockName('next mock default'), | ||
aborted = false, | ||
httpVersion = '', | ||
httpVersionMajor = 0, | ||
httpVersionMinor = 0, | ||
complete = false, | ||
connection = {}, | ||
socket = {}, | ||
headers = {}, | ||
rawHeaders = [], | ||
trailers = {}, | ||
rawTrailers = [], | ||
setTimeout = jest.fn().mockName('setTimeout mock default'), | ||
statusCode = 0, | ||
statusMessage = '', | ||
destroy = jest.fn().mockName('destroy mock default'), | ||
...extraProvidedValues | ||
} = values | ||
return { | ||
params: (values && values.params) || {}, | ||
query: (values && values.query) || {}, | ||
body: (values && values.body) || {}, | ||
cookies: (values && values.cookies) || {}, | ||
method: (values && values.method) || '', | ||
protocol: (values && values.protocol) || '', | ||
secure: (values && values.secure) || false, | ||
ip: (values && values.ip) || '', | ||
ips: (values && values.ips) || [], | ||
subdomains: (values && values.subdomains) || [], | ||
path: (values && values.path) || '', | ||
hostname: (values && values.hostname) || '', | ||
host: (values && values.host) || '', | ||
fresh: (values && values.fresh) || false, | ||
stale: (values && values.stale) || false, | ||
xhr: (values && values.xhr) || false, | ||
route: (values && values.route) || {}, | ||
signedCookies: (values && values.signedCookies) || {}, | ||
originalUrl: (values && values.originalUrl) || '', | ||
url: (values && values.url) || '', | ||
baseUrl: (values && values.baseUrl) || '', | ||
accepted: (values && values.accepted) || [], | ||
get: (values && values.get) || jest.fn().mockName('get mock default'), | ||
header: (values && values.header) || jest.fn().mockName('header mock default'), | ||
accepts: (values && values.accepts) || jest.fn().mockName('accepts mock default'), | ||
acceptsCharsets: (values && values.acceptsCharsets) || jest.fn().mockName('acceptsCharsets mock default'), | ||
acceptsEncodings: (values && values.acceptsEncodings) || jest.fn().mockName('acceptsEncodings mock default'), | ||
acceptsLanguages: (values && values.acceptsLanguages) || jest.fn().mockName('acceptsLanguages mock default'), | ||
range: (values && values.range) || jest.fn().mockName('range mock default'), | ||
param: (values && values.param) || jest.fn().mockName('param mock default'), | ||
is: (values && values.is) || jest.fn().mockName('is mock default'), | ||
app: (values && values.app) || {}, | ||
res: (values && values.res) || jest.fn().mockName('res mock default'), | ||
next: (values && values.next) || jest.fn().mockName('next mock default'), | ||
params, | ||
query, | ||
body, | ||
cookies, | ||
method, | ||
protocol, | ||
secure, | ||
ip, | ||
ips, | ||
subdomains, | ||
path, | ||
hostname, | ||
host, | ||
fresh, | ||
stale, | ||
xhr, | ||
route, | ||
signedCookies, | ||
originalUrl, | ||
url, | ||
baseUrl, | ||
accepted, | ||
get, | ||
header, | ||
accepts, | ||
acceptsCharsets, | ||
acceptsEncodings, | ||
acceptsLanguages, | ||
range, | ||
param, | ||
is, | ||
app, | ||
res, | ||
next, | ||
// http - IncomingMessage | ||
aborted: (values && values.aborted) || false, | ||
httpVersion: (values && values.httpVersion) || '', | ||
httpVersionMajor: (values && values.httpVersionMajor) || 0, | ||
httpVersionMinor: (values && values.httpVersionMinor) || 0, | ||
complete: (values && values.complete) || false, | ||
connection: (values && values.connection) || {}, | ||
socket: (values && values.socket) || {}, | ||
headers: (values && values.headers) || {}, | ||
rawHeaders: (values && values.rawHeaders) || [], | ||
trailers: (values && values.trailers) || {}, | ||
rawTrailers: (values && values.rawTrailers) || [], | ||
setTimeout: (values && values.setTimeout) || jest.fn().mockName('setTimeout mock default'), | ||
statusCode: (values && values.statusCode) || 0, | ||
statusMessage: (values && values.statusMessage) || '', | ||
destroy: (values && values.destroy) || jest.fn().mockName('destroy mock default'), | ||
aborted, | ||
httpVersion, | ||
httpVersionMajor, | ||
httpVersionMinor, | ||
complete, | ||
connection, | ||
socket, | ||
headers, | ||
rawHeaders, | ||
trailers, | ||
rawTrailers, | ||
setTimeout, | ||
statusCode, | ||
statusMessage, | ||
destroy, | ||
// custom values | ||
...extraProvidedValues, | ||
} as T | ||
@@ -62,0 +118,0 @@ } |
@@ -8,2 +8,4 @@ // Types | ||
const DEFAULT_RES_KEY_LENGTH = 52 | ||
describe('getMockRes', () => { | ||
@@ -21,4 +23,4 @@ test('returns expected object', () => { | ||
// res contains the expected functions | ||
expect(res).toBeTruthy() | ||
expect(Object.keys(res).length).toBe(52) | ||
expect(res).toBeDefined() | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH) | ||
@@ -67,2 +69,264 @@ // express - Response | ||
test('provides default for, and allows overrides for headersSent', () => { | ||
// default value is provided | ||
const { res: defaultRes } = getMockRes() | ||
expect(defaultRes.headersSent).toBe(false) | ||
// allows setting an override value | ||
const { res } = getMockRes({ headersSent: true }) | ||
// does not change the core res object | ||
expect(res).toBeDefined() | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH) | ||
expect(res.headersSent).toBe(true) | ||
}) | ||
test('provides default for, and allows overrides for locals', () => { | ||
// default value is provided | ||
const { res: defaultRes } = getMockRes() | ||
expect(defaultRes.locals).toEqual({}) | ||
// allows setting an override value | ||
const mockLocals = { one: 1, two: 2 } | ||
const { res } = getMockRes({ locals: mockLocals }) | ||
// does not change the core res object | ||
expect(res).toBeDefined() | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH) | ||
expect(res.locals).toBe(mockLocals) | ||
}) | ||
test('provides default for, and allows overrides for charset', () => { | ||
// default value is provided | ||
const { res: defaultRes } = getMockRes() | ||
expect(defaultRes.charset).toBe('') | ||
// allows setting an override value | ||
const { res } = getMockRes({ charset: 'custom' }) | ||
// does not change the core res object | ||
expect(res).toBeDefined() | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH) | ||
expect(res.charset).toBe('custom') | ||
}) | ||
test('provides default for, and allows overrides for app', () => { | ||
// default value is provided | ||
const { res: defaultRes } = getMockRes() | ||
expect(defaultRes.app).toEqual({}) | ||
// allows setting an override value | ||
const mockApp = { one: 1, two: 2 } | ||
const { res } = getMockRes({ app: mockApp }) | ||
// does not change the core res object | ||
expect(res).toBeDefined() | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH) | ||
expect(res.app).toBe(mockApp) | ||
}) | ||
test('provides default for, and allows overrides for req', () => { | ||
// default value is provided | ||
const { res: defaultRes } = getMockRes() | ||
expect(defaultRes.req).toEqual({}) | ||
// allows setting an override value | ||
const mockReq = { one: 1, two: 2 } | ||
const { res } = getMockRes({ req: mockReq }) | ||
// does not change the core res object | ||
expect(res).toBeDefined() | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH) | ||
expect(res.req).toBe(mockReq) | ||
}) | ||
test('provides default for, and allows overrides for statusCode', () => { | ||
// default value is provided | ||
const { res: defaultRes } = getMockRes() | ||
expect(defaultRes.statusCode).toBe(0) | ||
// allows setting an override value | ||
const { res } = getMockRes({ statusCode: 500 }) | ||
// does not change the core res object | ||
expect(res).toBeDefined() | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH) | ||
expect(res.statusCode).toBe(500) | ||
}) | ||
test('provides default for, and allows overrides for statusMessage', () => { | ||
// default value is provided | ||
const { res: defaultRes } = getMockRes() | ||
expect(defaultRes.statusMessage).toBe('') | ||
// allows setting an override value | ||
const { res } = getMockRes({ statusMessage: 'custom' }) | ||
// does not change the core res object | ||
expect(res).toBeDefined() | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH) | ||
expect(res.statusMessage).toBe('custom') | ||
}) | ||
test('provides default for, and allows overrides for upgrading', () => { | ||
// default value is provided | ||
const { res: defaultRes } = getMockRes() | ||
expect(defaultRes.upgrading).toBe(false) | ||
// allows setting an override value | ||
const { res } = getMockRes({ upgrading: true }) | ||
// does not change the core res object | ||
expect(res).toBeDefined() | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH) | ||
expect(res.upgrading).toBe(true) | ||
}) | ||
test('provides default for, and allows overrides for chunkedEncoding', () => { | ||
// default value is provided | ||
const { res: defaultRes } = getMockRes() | ||
expect(defaultRes.chunkedEncoding).toBe(false) | ||
// allows setting an override value | ||
const { res } = getMockRes({ chunkedEncoding: true }) | ||
// does not change the core res object | ||
expect(res).toBeDefined() | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH) | ||
expect(res.chunkedEncoding).toBe(true) | ||
}) | ||
test('provides default for, and allows overrides for shouldKeepAlive', () => { | ||
// default value is provided | ||
const { res: defaultRes } = getMockRes() | ||
expect(defaultRes.shouldKeepAlive).toBe(false) | ||
// allows setting an override value | ||
const { res } = getMockRes({ shouldKeepAlive: true }) | ||
// does not change the core res object | ||
expect(res).toBeDefined() | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH) | ||
expect(res.shouldKeepAlive).toBe(true) | ||
}) | ||
test('provides default for, and allows overrides for useChunkedEncodingByDefault', () => { | ||
// default value is provided | ||
const { res: defaultRes } = getMockRes() | ||
expect(defaultRes.useChunkedEncodingByDefault).toBe(false) | ||
// allows setting an override value | ||
const { res } = getMockRes({ useChunkedEncodingByDefault: true }) | ||
// does not change the core res object | ||
expect(res).toBeDefined() | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH) | ||
expect(res.useChunkedEncodingByDefault).toBe(true) | ||
}) | ||
test('provides default for, and allows overrides for sendDate', () => { | ||
// default value is provided | ||
const { res: defaultRes } = getMockRes() | ||
expect(defaultRes.sendDate).toBe(false) | ||
// allows setting an override value | ||
const { res } = getMockRes({ sendDate: true }) | ||
// does not change the core res object | ||
expect(res).toBeDefined() | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH) | ||
expect(res.sendDate).toBe(true) | ||
}) | ||
test('provides default for, and allows overrides for finished', () => { | ||
// default value is provided | ||
const { res: defaultRes } = getMockRes() | ||
expect(defaultRes.finished).toBe(false) | ||
// allows setting an override value | ||
const { res } = getMockRes({ finished: true }) | ||
// does not change the core res object | ||
expect(res).toBeDefined() | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH) | ||
expect(res.finished).toBe(true) | ||
}) | ||
test('provides default for, and allows overrides for connection', () => { | ||
// default value is provided | ||
const { res: defaultRes } = getMockRes() | ||
expect(defaultRes.connection).toEqual({}) | ||
// allows setting an override value | ||
const mockConnection = { one: 1, two: 2 } | ||
const { res } = getMockRes({ connection: mockConnection }) | ||
// does not change the core res object | ||
expect(res).toBeDefined() | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH) | ||
expect(res.connection).toBe(mockConnection) | ||
}) | ||
test('provides default for, and allows overrides for socket', () => { | ||
// default value is provided | ||
const { res: defaultRes } = getMockRes() | ||
expect(defaultRes.socket).toEqual({}) | ||
// allows setting an override value | ||
const mockSocket = { one: 1, two: 2 } | ||
const { res } = getMockRes({ socket: mockSocket }) | ||
// does not change the core res object | ||
expect(res).toBeDefined() | ||
expect(Object.keys(res).length).toBe(DEFAULT_RES_KEY_LENGTH) | ||
expect(res.socket).toBe(mockSocket) | ||
}) | ||
test('allows custom properties, with casting', () => { | ||
interface User { | ||
id: string | ||
name: string | ||
} | ||
interface CustomResponse extends Response { | ||
user: User | ||
} | ||
const mockUser: User = { | ||
id: '123', | ||
name: 'Bob', | ||
} | ||
// default value is not provided | ||
const { res: defaultRes } = getMockRes() | ||
const castedDefaultRes = (defaultRes as unknown) as CustomResponse | ||
expect(castedDefaultRes.user).toBeUndefined() | ||
// allows setting a custom property | ||
const { res } = getMockRes({ sendDate: true, user: mockUser }) | ||
const castedRes = (res as unknown) as CustomResponse | ||
// adds and extra property to the res object | ||
expect(castedRes).toBeDefined() | ||
expect(Object.keys(castedRes).length).toBe(DEFAULT_RES_KEY_LENGTH + 1) | ||
// both properties are available | ||
expect(castedRes.sendDate).toBe(true) | ||
expect(castedRes.user).toBe(mockUser) | ||
}) | ||
test('the mock next function is provided', () => { | ||
@@ -72,3 +336,3 @@ const { next } = getMockRes() | ||
// next is part of the mock response | ||
expect(next).toBeTruthy() | ||
expect(next).toBeDefined() | ||
expect(typeof next).toBe('function') | ||
@@ -81,3 +345,3 @@ }) | ||
// a mock clear function is part of the mock response | ||
expect(mockClear).toBeTruthy() | ||
expect(mockClear).toBeDefined() | ||
expect(typeof mockClear).toBe('function') | ||
@@ -90,3 +354,3 @@ }) | ||
// a mock clear alias function is part of the mock response | ||
expect(clearMockRes).toBeTruthy() | ||
expect(clearMockRes).toBeDefined() | ||
expect(typeof clearMockRes).toBe('function') | ||
@@ -93,0 +357,0 @@ }) |
// Libraries | ||
import { Response, NextFunction } from 'express' | ||
// Types | ||
import type { MockResponse } from './index' | ||
/** | ||
@@ -8,3 +11,5 @@ * Will return a typed and mocked version of key Express components. | ||
*/ | ||
export const getMockRes = (): { | ||
export const getMockRes = ( | ||
values: MockResponse = {}, | ||
): { | ||
res: Response | ||
@@ -16,2 +21,20 @@ next: NextFunction | ||
const next = jest.fn() | ||
const { | ||
headersSent = false, | ||
locals = {}, | ||
charset = '', | ||
app = {}, | ||
req = {}, | ||
statusCode = 0, | ||
statusMessage = '', | ||
upgrading = false, | ||
chunkedEncoding = false, | ||
shouldKeepAlive = false, | ||
useChunkedEncodingByDefault = false, | ||
sendDate = false, | ||
finished = false, | ||
connection = {}, | ||
socket = {}, | ||
...extraProvidedValues | ||
} = values | ||
const response = { | ||
@@ -42,7 +65,7 @@ // express - Response | ||
append: jest.fn(), | ||
headersSent: false, | ||
locals: {}, | ||
charset: '', | ||
app: {}, | ||
req: {}, | ||
headersSent, | ||
locals, | ||
charset, | ||
app, | ||
req, | ||
// http - ServerResponse | ||
@@ -54,4 +77,4 @@ assignSocket: jest.fn(), | ||
writeProcessing: jest.fn(), | ||
statusCode: 0, | ||
statusMessage: '', | ||
statusCode, | ||
statusMessage, | ||
// http - OutgoingMessage | ||
@@ -67,10 +90,12 @@ setTimeout: jest.fn(), | ||
flushHeaders: jest.fn(), | ||
upgrading: false, | ||
chunkedEncoding: false, | ||
shouldKeepAlive: false, | ||
useChunkedEncodingByDefault: false, | ||
sendDate: false, | ||
finished: false, | ||
connection: {}, | ||
socket: {}, | ||
upgrading, | ||
chunkedEncoding, | ||
shouldKeepAlive, | ||
useChunkedEncodingByDefault, | ||
sendDate, | ||
finished, | ||
connection, | ||
socket, | ||
// custom values | ||
...extraProvidedValues, | ||
} | ||
@@ -77,0 +102,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
342790
39
2207
108