Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@jest-mock/express

Package Overview
Dependencies
Maintainers
2
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jest-mock/express - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

61

dist/src/response/response.js

@@ -11,2 +11,3 @@ "use strict";

const response = {
// express - Response
status: jest.fn(),

@@ -36,6 +37,34 @@ sendStatus: jest.fn(),

headersSent: false,
locals: {},
charset: '',
app: {},
req: {},
// http - ServerResponse
assignSocket: jest.fn(),
detachSocket: jest.fn(),
writeContinue: jest.fn(),
writeHead: jest.fn(),
writeProcessing: jest.fn(),
statusCode: 0,
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: {},
};
// for the function that are chainable, return the response
// express - Response - chainable functions
response.status.mockReturnValue(response);

@@ -58,13 +87,9 @@ response.sendStatus.mockReturnValue(response);

response.append.mockReturnValue(response);
/**
* these are not required to be chainable
* - get
* - render
* - sendFile
* - sendfile
* - download
* - redirect
*/
// http - ServerResponse - chainable functions
response.writeHead.mockReturnValue(response);
// http - OutgoingMessage - chainable functions
response.setTimeout.mockReturnValue(response);
const clearAllMocks = () => {
next.mockClear();
// express - Response
response.status.mockClear();

@@ -93,2 +118,18 @@ response.sendStatus.mockClear();

response.append.mockClear();
// http - ServerResponse
response.assignSocket.mockClear();
response.detachSocket.mockClear();
response.writeContinue.mockClear();
response.writeHead.mockClear();
response.writeProcessing.mockClear();
// http - OutgoingMessage
response.setTimeout.mockClear();
response.setHeader.mockClear();
response.getHeader.mockClear();
response.getHeaders.mockClear();
response.getHeaderNames.mockClear();
response.hasHeader.mockClear();
response.removeHeader.mockClear();
response.addTrailers.mockClear();
response.flushHeaders.mockClear();
};

@@ -95,0 +136,0 @@ return {

373

dist/src/response/response.test.js

@@ -19,3 +19,4 @@ "use strict";

expect(res).toBeTruthy();
expect(Object.keys(res).length).toBe(26);
expect(Object.keys(res).length).toBe(52);
// express - Response
expect(typeof res.status).toBe('function');

@@ -44,2 +45,18 @@ expect(typeof res.sendStatus).toBe('function');

expect(typeof res.append).toBe('function');
// http - ServerResponse
expect(typeof res.assignSocket).toBe('function');
expect(typeof res.detachSocket).toBe('function');
expect(typeof res.writeContinue).toBe('function');
expect(typeof res.writeHead).toBe('function');
expect(typeof res.writeProcessing).toBe('function');
// http - OutgoingMessage
expect(typeof res.setTimeout).toBe('function');
expect(typeof res.setHeader).toBe('function');
expect(typeof res.getHeader).toBe('function');
expect(typeof res.getHeaders).toBe('function');
expect(typeof res.getHeaderNames).toBe('function');
expect(typeof res.hasHeader).toBe('function');
expect(typeof res.removeHeader).toBe('function');
expect(typeof res.addTrailers).toBe('function');
expect(typeof res.flushHeaders).toBe('function');
});

@@ -68,78 +85,90 @@ test('the mock next function is provided', () => {

next();
res.status(123);
res.sendStatus(123);
res.links({});
res.send();
res.json();
res.jsonp();
res.sendFile('test');
res.sendfile('test');
res.download('test');
res.contentType('test');
res.type('test');
res.format({});
res.attachment();
res.set({});
res.header({});
res.get('test');
res.clearCookie('test');
res.cookie('test', 'test');
res.location('test');
res.redirect('test');
res.render('test');
res.vary('test');
res.append('test');
callAll(res);
// ensure they all report as being called
expect(next.mock.calls.length).toBe(1);
expect(res.status.mock.calls.length).toBe(1);
expect(res.sendStatus.mock.calls.length).toBe(1);
expect(res.links.mock.calls.length).toBe(1);
expect(res.send.mock.calls.length).toBe(1);
expect(res.json.mock.calls.length).toBe(1);
expect(res.jsonp.mock.calls.length).toBe(1);
expect(res.sendFile.mock.calls.length).toBe(1);
expect(res.sendfile.mock.calls.length).toBe(1);
expect(res.download.mock.calls.length).toBe(1);
expect(res.contentType.mock.calls.length).toBe(1);
expect(res.type.mock.calls.length).toBe(1);
expect(res.format.mock.calls.length).toBe(1);
expect(res.attachment.mock.calls.length).toBe(1);
expect(res.set.mock.calls.length).toBe(1);
expect(res.header.mock.calls.length).toBe(1);
expect(res.get.mock.calls.length).toBe(1);
expect(res.clearCookie.mock.calls.length).toBe(1);
expect(res.cookie.mock.calls.length).toBe(1);
expect(res.location.mock.calls.length).toBe(1);
expect(res.redirect.mock.calls.length).toBe(1);
expect(res.render.mock.calls.length).toBe(1);
expect(res.vary.mock.calls.length).toBe(1);
expect(res.append.mock.calls.length).toBe(1);
// express - Response
expect(next).toBeCalledTimes(1);
expect(res.status).toBeCalledTimes(1);
expect(res.sendStatus).toBeCalledTimes(1);
expect(res.links).toBeCalledTimes(1);
expect(res.send).toBeCalledTimes(1);
expect(res.json).toBeCalledTimes(1);
expect(res.jsonp).toBeCalledTimes(1);
expect(res.sendFile).toBeCalledTimes(1);
expect(res.sendfile).toBeCalledTimes(1);
expect(res.download).toBeCalledTimes(1);
expect(res.contentType).toBeCalledTimes(1);
expect(res.type).toBeCalledTimes(1);
expect(res.format).toBeCalledTimes(1);
expect(res.attachment).toBeCalledTimes(1);
expect(res.set).toBeCalledTimes(1);
expect(res.header).toBeCalledTimes(1);
expect(res.get).toBeCalledTimes(1);
expect(res.clearCookie).toBeCalledTimes(1);
expect(res.cookie).toBeCalledTimes(1);
expect(res.location).toBeCalledTimes(1);
expect(res.redirect).toBeCalledTimes(1);
expect(res.render).toBeCalledTimes(1);
expect(res.vary).toBeCalledTimes(1);
expect(res.append).toBeCalledTimes(1);
// http - ServerResponse
expect(res.assignSocket).toBeCalledTimes(1);
expect(res.detachSocket).toBeCalledTimes(1);
expect(res.writeContinue).toBeCalledTimes(1);
expect(res.writeHead).toBeCalledTimes(1);
expect(res.writeProcessing).toBeCalledTimes(1);
// http - OutgoingMessage
expect(res.setTimeout).toBeCalledTimes(1);
expect(res.setHeader).toBeCalledTimes(1);
expect(res.getHeader).toBeCalledTimes(1);
expect(res.getHeaders).toBeCalledTimes(1);
expect(res.getHeaderNames).toBeCalledTimes(1);
expect(res.hasHeader).toBeCalledTimes(1);
expect(res.removeHeader).toBeCalledTimes(1);
expect(res.addTrailers).toBeCalledTimes(1);
expect(res.flushHeaders).toBeCalledTimes(1);
// clear the mock
mockClear();
// ensure they all have been cleared
expect(next.mock.calls.length).toBe(0);
expect(next.mock.calls.length).toBe(0);
expect(res.status.mock.calls.length).toBe(0);
expect(res.sendStatus.mock.calls.length).toBe(0);
expect(res.links.mock.calls.length).toBe(0);
expect(res.send.mock.calls.length).toBe(0);
expect(res.json.mock.calls.length).toBe(0);
expect(res.jsonp.mock.calls.length).toBe(0);
expect(res.sendFile.mock.calls.length).toBe(0);
expect(res.sendfile.mock.calls.length).toBe(0);
expect(res.download.mock.calls.length).toBe(0);
expect(res.contentType.mock.calls.length).toBe(0);
expect(res.type.mock.calls.length).toBe(0);
expect(res.format.mock.calls.length).toBe(0);
expect(res.attachment.mock.calls.length).toBe(0);
expect(res.set.mock.calls.length).toBe(0);
expect(res.header.mock.calls.length).toBe(0);
expect(res.get.mock.calls.length).toBe(0);
expect(res.clearCookie.mock.calls.length).toBe(0);
expect(res.cookie.mock.calls.length).toBe(0);
expect(res.location.mock.calls.length).toBe(0);
expect(res.redirect.mock.calls.length).toBe(0);
expect(res.render.mock.calls.length).toBe(0);
expect(res.vary.mock.calls.length).toBe(0);
expect(res.append.mock.calls.length).toBe(0);
// express - Response
expect(next).not.toBeCalled();
expect(next).not.toBeCalled();
expect(res.status).not.toBeCalled();
expect(res.sendStatus).not.toBeCalled();
expect(res.links).not.toBeCalled();
expect(res.send).not.toBeCalled();
expect(res.json).not.toBeCalled();
expect(res.jsonp).not.toBeCalled();
expect(res.sendFile).not.toBeCalled();
expect(res.sendfile).not.toBeCalled();
expect(res.download).not.toBeCalled();
expect(res.contentType).not.toBeCalled();
expect(res.type).not.toBeCalled();
expect(res.format).not.toBeCalled();
expect(res.attachment).not.toBeCalled();
expect(res.set).not.toBeCalled();
expect(res.header).not.toBeCalled();
expect(res.get).not.toBeCalled();
expect(res.clearCookie).not.toBeCalled();
expect(res.cookie).not.toBeCalled();
expect(res.location).not.toBeCalled();
expect(res.redirect).not.toBeCalled();
expect(res.render).not.toBeCalled();
expect(res.vary).not.toBeCalled();
expect(res.append).not.toBeCalled();
// http - ServerResponse
expect(res.assignSocket).not.toBeCalled();
expect(res.detachSocket).not.toBeCalled();
expect(res.writeContinue).not.toBeCalled();
expect(res.writeHead).not.toBeCalled();
expect(res.writeProcessing).not.toBeCalled();
// http - OutgoingMessage
expect(res.setTimeout).not.toBeCalled();
expect(res.setHeader).not.toBeCalled();
expect(res.getHeader).not.toBeCalled();
expect(res.getHeaders).not.toBeCalled();
expect(res.getHeaderNames).not.toBeCalled();
expect(res.hasHeader).not.toBeCalled();
expect(res.removeHeader).not.toBeCalled();
expect(res.addTrailers).not.toBeCalled();
expect(res.flushHeaders).not.toBeCalled();
});

@@ -150,80 +179,134 @@ test('clearMockRes clears all mocks', () => {

next();
res.status(123);
res.sendStatus(123);
res.links({});
res.send();
res.json();
res.jsonp();
res.sendFile('test');
res.sendfile('test');
res.download('test');
res.contentType('test');
res.type('test');
res.format({});
res.attachment();
res.set({});
res.header({});
res.get('test');
res.clearCookie('test');
res.cookie('test', 'test');
res.location('test');
res.redirect('test');
res.render('test');
res.vary('test');
res.append('test');
callAll(res);
// ensure they all report as being called
expect(next.mock.calls.length).toBe(1);
expect(res.status.mock.calls.length).toBe(1);
expect(res.sendStatus.mock.calls.length).toBe(1);
expect(res.links.mock.calls.length).toBe(1);
expect(res.send.mock.calls.length).toBe(1);
expect(res.json.mock.calls.length).toBe(1);
expect(res.jsonp.mock.calls.length).toBe(1);
expect(res.sendFile.mock.calls.length).toBe(1);
expect(res.sendfile.mock.calls.length).toBe(1);
expect(res.download.mock.calls.length).toBe(1);
expect(res.contentType.mock.calls.length).toBe(1);
expect(res.type.mock.calls.length).toBe(1);
expect(res.format.mock.calls.length).toBe(1);
expect(res.attachment.mock.calls.length).toBe(1);
expect(res.set.mock.calls.length).toBe(1);
expect(res.header.mock.calls.length).toBe(1);
expect(res.get.mock.calls.length).toBe(1);
expect(res.clearCookie.mock.calls.length).toBe(1);
expect(res.cookie.mock.calls.length).toBe(1);
expect(res.location.mock.calls.length).toBe(1);
expect(res.redirect.mock.calls.length).toBe(1);
expect(res.render.mock.calls.length).toBe(1);
expect(res.vary.mock.calls.length).toBe(1);
expect(res.append.mock.calls.length).toBe(1);
// express - Response
expect(next).toBeCalledTimes(1);
expect(res.status).toBeCalledTimes(1);
expect(res.sendStatus).toBeCalledTimes(1);
expect(res.links).toBeCalledTimes(1);
expect(res.send).toBeCalledTimes(1);
expect(res.json).toBeCalledTimes(1);
expect(res.jsonp).toBeCalledTimes(1);
expect(res.sendFile).toBeCalledTimes(1);
expect(res.sendfile).toBeCalledTimes(1);
expect(res.download).toBeCalledTimes(1);
expect(res.contentType).toBeCalledTimes(1);
expect(res.type).toBeCalledTimes(1);
expect(res.format).toBeCalledTimes(1);
expect(res.attachment).toBeCalledTimes(1);
expect(res.set).toBeCalledTimes(1);
expect(res.header).toBeCalledTimes(1);
expect(res.get).toBeCalledTimes(1);
expect(res.clearCookie).toBeCalledTimes(1);
expect(res.cookie).toBeCalledTimes(1);
expect(res.location).toBeCalledTimes(1);
expect(res.redirect).toBeCalledTimes(1);
expect(res.render).toBeCalledTimes(1);
expect(res.vary).toBeCalledTimes(1);
expect(res.append).toBeCalledTimes(1);
// http - ServerResponse
expect(res.assignSocket).toBeCalledTimes(1);
expect(res.detachSocket).toBeCalledTimes(1);
expect(res.writeContinue).toBeCalledTimes(1);
expect(res.writeHead).toBeCalledTimes(1);
expect(res.writeProcessing).toBeCalledTimes(1);
// http - OutgoingMessage
expect(res.setTimeout).toBeCalledTimes(1);
expect(res.setHeader).toBeCalledTimes(1);
expect(res.getHeader).toBeCalledTimes(1);
expect(res.getHeaders).toBeCalledTimes(1);
expect(res.getHeaderNames).toBeCalledTimes(1);
expect(res.hasHeader).toBeCalledTimes(1);
expect(res.removeHeader).toBeCalledTimes(1);
expect(res.addTrailers).toBeCalledTimes(1);
expect(res.flushHeaders).toBeCalledTimes(1);
// clear the mock
clearMockRes();
// ensure they all have been cleared
expect(next.mock.calls.length).toBe(0);
expect(next.mock.calls.length).toBe(0);
expect(res.status.mock.calls.length).toBe(0);
expect(res.sendStatus.mock.calls.length).toBe(0);
expect(res.links.mock.calls.length).toBe(0);
expect(res.send.mock.calls.length).toBe(0);
expect(res.json.mock.calls.length).toBe(0);
expect(res.jsonp.mock.calls.length).toBe(0);
expect(res.sendFile.mock.calls.length).toBe(0);
expect(res.sendfile.mock.calls.length).toBe(0);
expect(res.download.mock.calls.length).toBe(0);
expect(res.contentType.mock.calls.length).toBe(0);
expect(res.type.mock.calls.length).toBe(0);
expect(res.format.mock.calls.length).toBe(0);
expect(res.attachment.mock.calls.length).toBe(0);
expect(res.set.mock.calls.length).toBe(0);
expect(res.header.mock.calls.length).toBe(0);
expect(res.get.mock.calls.length).toBe(0);
expect(res.clearCookie.mock.calls.length).toBe(0);
expect(res.cookie.mock.calls.length).toBe(0);
expect(res.location.mock.calls.length).toBe(0);
expect(res.redirect.mock.calls.length).toBe(0);
expect(res.render.mock.calls.length).toBe(0);
expect(res.vary.mock.calls.length).toBe(0);
expect(res.append.mock.calls.length).toBe(0);
// express - Response
expect(next).not.toBeCalled();
expect(next).not.toBeCalled();
expect(res.status).not.toBeCalled();
expect(res.sendStatus).not.toBeCalled();
expect(res.links).not.toBeCalled();
expect(res.send).not.toBeCalled();
expect(res.json).not.toBeCalled();
expect(res.jsonp).not.toBeCalled();
expect(res.sendFile).not.toBeCalled();
expect(res.sendfile).not.toBeCalled();
expect(res.download).not.toBeCalled();
expect(res.contentType).not.toBeCalled();
expect(res.type).not.toBeCalled();
expect(res.format).not.toBeCalled();
expect(res.attachment).not.toBeCalled();
expect(res.set).not.toBeCalled();
expect(res.header).not.toBeCalled();
expect(res.get).not.toBeCalled();
expect(res.clearCookie).not.toBeCalled();
expect(res.cookie).not.toBeCalled();
expect(res.location).not.toBeCalled();
expect(res.redirect).not.toBeCalled();
expect(res.render).not.toBeCalled();
expect(res.vary).not.toBeCalled();
expect(res.append).not.toBeCalled();
// http - ServerResponse
expect(res.assignSocket).not.toBeCalled();
expect(res.detachSocket).not.toBeCalled();
expect(res.writeContinue).not.toBeCalled();
expect(res.writeHead).not.toBeCalled();
expect(res.writeProcessing).not.toBeCalled();
// http - OutgoingMessage
expect(res.setTimeout).not.toBeCalled();
expect(res.setHeader).not.toBeCalled();
expect(res.getHeader).not.toBeCalled();
expect(res.getHeaders).not.toBeCalled();
expect(res.getHeaderNames).not.toBeCalled();
expect(res.hasHeader).not.toBeCalled();
expect(res.removeHeader).not.toBeCalled();
expect(res.addTrailers).not.toBeCalled();
expect(res.flushHeaders).not.toBeCalled();
});
});
function callAll(res) {
// express - Response
res.status(123);
res.sendStatus(123);
res.links({});
res.send();
res.json();
res.jsonp();
res.sendFile('test');
res.sendfile('test');
res.download('test');
res.contentType('test');
res.type('test');
res.format({});
res.attachment();
res.set({});
res.header({});
res.get('test');
res.clearCookie('test');
res.cookie('test', 'test');
res.location('test');
res.redirect('test');
res.render('test');
res.vary('test');
res.append('test');
// http - ServerResponse
res.assignSocket({});
res.detachSocket({});
res.writeContinue();
res.writeHead(123);
res.writeProcessing();
// http - OutgoingMessage
res.setTimeout(123);
res.setHeader('test', 'test');
res.getHeader('test');
res.getHeaders();
res.getHeaderNames();
res.hasHeader('test');
res.removeHeader('test');
res.addTrailers([]);
res.flushHeaders();
}
//# sourceMappingURL=response.test.js.map
{
"name": "@jest-mock/express",
"version": "1.0.0",
"version": "1.1.0",
"description": "A lightweight Jest mock for unit testing Express",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -0,1 +1,5 @@

// Types
import type { Socket } from 'net'
import type { Response } from 'express'
// Tested Module

@@ -17,3 +21,5 @@ import getMockRes from './response'

expect(res).toBeTruthy()
expect(Object.keys(res).length).toBe(26)
expect(Object.keys(res).length).toBe(52)
// express - Response
expect(typeof res.status).toBe('function')

@@ -42,2 +48,18 @@ expect(typeof res.sendStatus).toBe('function')

expect(typeof res.append).toBe('function')
// http - ServerResponse
expect(typeof res.assignSocket).toBe('function')
expect(typeof res.detachSocket).toBe('function')
expect(typeof res.writeContinue).toBe('function')
expect(typeof res.writeHead).toBe('function')
expect(typeof res.writeProcessing).toBe('function')
// http - OutgoingMessage
expect(typeof res.setTimeout).toBe('function')
expect(typeof res.setHeader).toBe('function')
expect(typeof res.getHeader).toBe('function')
expect(typeof res.getHeaders).toBe('function')
expect(typeof res.getHeaderNames).toBe('function')
expect(typeof res.hasHeader).toBe('function')
expect(typeof res.removeHeader).toBe('function')
expect(typeof res.addTrailers).toBe('function')
expect(typeof res.flushHeaders).toBe('function')
})

@@ -74,51 +96,46 @@

next()
res.status(123)
res.sendStatus(123)
res.links({})
res.send()
res.json()
res.jsonp()
res.sendFile('test')
res.sendfile('test')
res.download('test')
res.contentType('test')
res.type('test')
res.format({})
res.attachment()
res.set({})
res.header({})
res.get('test')
res.clearCookie('test')
res.cookie('test', 'test')
res.location('test')
res.redirect('test')
res.render('test')
res.vary('test')
res.append('test')
callAll(res)
// ensure they all report as being called
expect((next as jest.Mock).mock.calls.length).toBe(1)
expect((res.status as jest.Mock).mock.calls.length).toBe(1)
expect((res.sendStatus as jest.Mock).mock.calls.length).toBe(1)
expect((res.links as jest.Mock).mock.calls.length).toBe(1)
expect((res.send as jest.Mock).mock.calls.length).toBe(1)
expect((res.json as jest.Mock).mock.calls.length).toBe(1)
expect((res.jsonp as jest.Mock).mock.calls.length).toBe(1)
expect((res.sendFile as jest.Mock).mock.calls.length).toBe(1)
expect((res.sendfile as jest.Mock).mock.calls.length).toBe(1)
expect((res.download as jest.Mock).mock.calls.length).toBe(1)
expect((res.contentType as jest.Mock).mock.calls.length).toBe(1)
expect((res.type as jest.Mock).mock.calls.length).toBe(1)
expect((res.format as jest.Mock).mock.calls.length).toBe(1)
expect((res.attachment as jest.Mock).mock.calls.length).toBe(1)
expect((res.set as jest.Mock).mock.calls.length).toBe(1)
expect((res.header as jest.Mock).mock.calls.length).toBe(1)
expect((res.get as jest.Mock).mock.calls.length).toBe(1)
expect((res.clearCookie as jest.Mock).mock.calls.length).toBe(1)
expect((res.cookie as jest.Mock).mock.calls.length).toBe(1)
expect((res.location as jest.Mock).mock.calls.length).toBe(1)
expect((res.redirect as jest.Mock).mock.calls.length).toBe(1)
expect((res.render as jest.Mock).mock.calls.length).toBe(1)
expect((res.vary as jest.Mock).mock.calls.length).toBe(1)
expect((res.append as jest.Mock).mock.calls.length).toBe(1)
// express - Response
expect(next as jest.Mock).toBeCalledTimes(1)
expect(res.status as jest.Mock).toBeCalledTimes(1)
expect(res.sendStatus as jest.Mock).toBeCalledTimes(1)
expect(res.links as jest.Mock).toBeCalledTimes(1)
expect(res.send as jest.Mock).toBeCalledTimes(1)
expect(res.json as jest.Mock).toBeCalledTimes(1)
expect(res.jsonp as jest.Mock).toBeCalledTimes(1)
expect(res.sendFile as jest.Mock).toBeCalledTimes(1)
expect(res.sendfile as jest.Mock).toBeCalledTimes(1)
expect(res.download as jest.Mock).toBeCalledTimes(1)
expect(res.contentType as jest.Mock).toBeCalledTimes(1)
expect(res.type as jest.Mock).toBeCalledTimes(1)
expect(res.format as jest.Mock).toBeCalledTimes(1)
expect(res.attachment as jest.Mock).toBeCalledTimes(1)
expect(res.set as jest.Mock).toBeCalledTimes(1)
expect(res.header as jest.Mock).toBeCalledTimes(1)
expect(res.get as jest.Mock).toBeCalledTimes(1)
expect(res.clearCookie as jest.Mock).toBeCalledTimes(1)
expect(res.cookie as jest.Mock).toBeCalledTimes(1)
expect(res.location as jest.Mock).toBeCalledTimes(1)
expect(res.redirect as jest.Mock).toBeCalledTimes(1)
expect(res.render as jest.Mock).toBeCalledTimes(1)
expect(res.vary as jest.Mock).toBeCalledTimes(1)
expect(res.append as jest.Mock).toBeCalledTimes(1)
// http - ServerResponse
expect(res.assignSocket as jest.Mock).toBeCalledTimes(1)
expect(res.detachSocket as jest.Mock).toBeCalledTimes(1)
expect(res.writeContinue as jest.Mock).toBeCalledTimes(1)
expect(res.writeHead as jest.Mock).toBeCalledTimes(1)
expect(res.writeProcessing as jest.Mock).toBeCalledTimes(1)
// http - OutgoingMessage
expect(res.setTimeout as jest.Mock).toBeCalledTimes(1)
expect(res.setHeader as jest.Mock).toBeCalledTimes(1)
expect(res.getHeader as jest.Mock).toBeCalledTimes(1)
expect(res.getHeaders as jest.Mock).toBeCalledTimes(1)
expect(res.getHeaderNames as jest.Mock).toBeCalledTimes(1)
expect(res.hasHeader as jest.Mock).toBeCalledTimes(1)
expect(res.removeHeader as jest.Mock).toBeCalledTimes(1)
expect(res.addTrailers as jest.Mock).toBeCalledTimes(1)
expect(res.flushHeaders as jest.Mock).toBeCalledTimes(1)

@@ -129,27 +146,44 @@ // clear the mock

// ensure they all have been cleared
expect((next as jest.Mock).mock.calls.length).toBe(0)
expect((next as jest.Mock).mock.calls.length).toBe(0)
expect((res.status as jest.Mock).mock.calls.length).toBe(0)
expect((res.sendStatus as jest.Mock).mock.calls.length).toBe(0)
expect((res.links as jest.Mock).mock.calls.length).toBe(0)
expect((res.send as jest.Mock).mock.calls.length).toBe(0)
expect((res.json as jest.Mock).mock.calls.length).toBe(0)
expect((res.jsonp as jest.Mock).mock.calls.length).toBe(0)
expect((res.sendFile as jest.Mock).mock.calls.length).toBe(0)
expect((res.sendfile as jest.Mock).mock.calls.length).toBe(0)
expect((res.download as jest.Mock).mock.calls.length).toBe(0)
expect((res.contentType as jest.Mock).mock.calls.length).toBe(0)
expect((res.type as jest.Mock).mock.calls.length).toBe(0)
expect((res.format as jest.Mock).mock.calls.length).toBe(0)
expect((res.attachment as jest.Mock).mock.calls.length).toBe(0)
expect((res.set as jest.Mock).mock.calls.length).toBe(0)
expect((res.header as jest.Mock).mock.calls.length).toBe(0)
expect((res.get as jest.Mock).mock.calls.length).toBe(0)
expect((res.clearCookie as jest.Mock).mock.calls.length).toBe(0)
expect((res.cookie as jest.Mock).mock.calls.length).toBe(0)
expect((res.location as jest.Mock).mock.calls.length).toBe(0)
expect((res.redirect as jest.Mock).mock.calls.length).toBe(0)
expect((res.render as jest.Mock).mock.calls.length).toBe(0)
expect((res.vary as jest.Mock).mock.calls.length).toBe(0)
expect((res.append as jest.Mock).mock.calls.length).toBe(0)
// express - Response
expect(next as jest.Mock).not.toBeCalled()
expect(next as jest.Mock).not.toBeCalled()
expect(res.status as jest.Mock).not.toBeCalled()
expect(res.sendStatus as jest.Mock).not.toBeCalled()
expect(res.links as jest.Mock).not.toBeCalled()
expect(res.send as jest.Mock).not.toBeCalled()
expect(res.json as jest.Mock).not.toBeCalled()
expect(res.jsonp as jest.Mock).not.toBeCalled()
expect(res.sendFile as jest.Mock).not.toBeCalled()
expect(res.sendfile as jest.Mock).not.toBeCalled()
expect(res.download as jest.Mock).not.toBeCalled()
expect(res.contentType as jest.Mock).not.toBeCalled()
expect(res.type as jest.Mock).not.toBeCalled()
expect(res.format as jest.Mock).not.toBeCalled()
expect(res.attachment as jest.Mock).not.toBeCalled()
expect(res.set as jest.Mock).not.toBeCalled()
expect(res.header as jest.Mock).not.toBeCalled()
expect(res.get as jest.Mock).not.toBeCalled()
expect(res.clearCookie as jest.Mock).not.toBeCalled()
expect(res.cookie as jest.Mock).not.toBeCalled()
expect(res.location as jest.Mock).not.toBeCalled()
expect(res.redirect as jest.Mock).not.toBeCalled()
expect(res.render as jest.Mock).not.toBeCalled()
expect(res.vary as jest.Mock).not.toBeCalled()
expect(res.append as jest.Mock).not.toBeCalled()
// http - ServerResponse
expect(res.assignSocket as jest.Mock).not.toBeCalled()
expect(res.detachSocket as jest.Mock).not.toBeCalled()
expect(res.writeContinue as jest.Mock).not.toBeCalled()
expect(res.writeHead as jest.Mock).not.toBeCalled()
expect(res.writeProcessing as jest.Mock).not.toBeCalled()
// http - OutgoingMessage
expect(res.setTimeout as jest.Mock).not.toBeCalled()
expect(res.setHeader as jest.Mock).not.toBeCalled()
expect(res.getHeader as jest.Mock).not.toBeCalled()
expect(res.getHeaders as jest.Mock).not.toBeCalled()
expect(res.getHeaderNames as jest.Mock).not.toBeCalled()
expect(res.hasHeader as jest.Mock).not.toBeCalled()
expect(res.removeHeader as jest.Mock).not.toBeCalled()
expect(res.addTrailers as jest.Mock).not.toBeCalled()
expect(res.flushHeaders as jest.Mock).not.toBeCalled()
})

@@ -162,51 +196,46 @@

next()
res.status(123)
res.sendStatus(123)
res.links({})
res.send()
res.json()
res.jsonp()
res.sendFile('test')
res.sendfile('test')
res.download('test')
res.contentType('test')
res.type('test')
res.format({})
res.attachment()
res.set({})
res.header({})
res.get('test')
res.clearCookie('test')
res.cookie('test', 'test')
res.location('test')
res.redirect('test')
res.render('test')
res.vary('test')
res.append('test')
callAll(res)
// ensure they all report as being called
expect((next as jest.Mock).mock.calls.length).toBe(1)
expect((res.status as jest.Mock).mock.calls.length).toBe(1)
expect((res.sendStatus as jest.Mock).mock.calls.length).toBe(1)
expect((res.links as jest.Mock).mock.calls.length).toBe(1)
expect((res.send as jest.Mock).mock.calls.length).toBe(1)
expect((res.json as jest.Mock).mock.calls.length).toBe(1)
expect((res.jsonp as jest.Mock).mock.calls.length).toBe(1)
expect((res.sendFile as jest.Mock).mock.calls.length).toBe(1)
expect((res.sendfile as jest.Mock).mock.calls.length).toBe(1)
expect((res.download as jest.Mock).mock.calls.length).toBe(1)
expect((res.contentType as jest.Mock).mock.calls.length).toBe(1)
expect((res.type as jest.Mock).mock.calls.length).toBe(1)
expect((res.format as jest.Mock).mock.calls.length).toBe(1)
expect((res.attachment as jest.Mock).mock.calls.length).toBe(1)
expect((res.set as jest.Mock).mock.calls.length).toBe(1)
expect((res.header as jest.Mock).mock.calls.length).toBe(1)
expect((res.get as jest.Mock).mock.calls.length).toBe(1)
expect((res.clearCookie as jest.Mock).mock.calls.length).toBe(1)
expect((res.cookie as jest.Mock).mock.calls.length).toBe(1)
expect((res.location as jest.Mock).mock.calls.length).toBe(1)
expect((res.redirect as jest.Mock).mock.calls.length).toBe(1)
expect((res.render as jest.Mock).mock.calls.length).toBe(1)
expect((res.vary as jest.Mock).mock.calls.length).toBe(1)
expect((res.append as jest.Mock).mock.calls.length).toBe(1)
// express - Response
expect(next as jest.Mock).toBeCalledTimes(1)
expect(res.status as jest.Mock).toBeCalledTimes(1)
expect(res.sendStatus as jest.Mock).toBeCalledTimes(1)
expect(res.links as jest.Mock).toBeCalledTimes(1)
expect(res.send as jest.Mock).toBeCalledTimes(1)
expect(res.json as jest.Mock).toBeCalledTimes(1)
expect(res.jsonp as jest.Mock).toBeCalledTimes(1)
expect(res.sendFile as jest.Mock).toBeCalledTimes(1)
expect(res.sendfile as jest.Mock).toBeCalledTimes(1)
expect(res.download as jest.Mock).toBeCalledTimes(1)
expect(res.contentType as jest.Mock).toBeCalledTimes(1)
expect(res.type as jest.Mock).toBeCalledTimes(1)
expect(res.format as jest.Mock).toBeCalledTimes(1)
expect(res.attachment as jest.Mock).toBeCalledTimes(1)
expect(res.set as jest.Mock).toBeCalledTimes(1)
expect(res.header as jest.Mock).toBeCalledTimes(1)
expect(res.get as jest.Mock).toBeCalledTimes(1)
expect(res.clearCookie as jest.Mock).toBeCalledTimes(1)
expect(res.cookie as jest.Mock).toBeCalledTimes(1)
expect(res.location as jest.Mock).toBeCalledTimes(1)
expect(res.redirect as jest.Mock).toBeCalledTimes(1)
expect(res.render as jest.Mock).toBeCalledTimes(1)
expect(res.vary as jest.Mock).toBeCalledTimes(1)
expect(res.append as jest.Mock).toBeCalledTimes(1)
// http - ServerResponse
expect(res.assignSocket as jest.Mock).toBeCalledTimes(1)
expect(res.detachSocket as jest.Mock).toBeCalledTimes(1)
expect(res.writeContinue as jest.Mock).toBeCalledTimes(1)
expect(res.writeHead as jest.Mock).toBeCalledTimes(1)
expect(res.writeProcessing as jest.Mock).toBeCalledTimes(1)
// http - OutgoingMessage
expect(res.setTimeout as jest.Mock).toBeCalledTimes(1)
expect(res.setHeader as jest.Mock).toBeCalledTimes(1)
expect(res.getHeader as jest.Mock).toBeCalledTimes(1)
expect(res.getHeaders as jest.Mock).toBeCalledTimes(1)
expect(res.getHeaderNames as jest.Mock).toBeCalledTimes(1)
expect(res.hasHeader as jest.Mock).toBeCalledTimes(1)
expect(res.removeHeader as jest.Mock).toBeCalledTimes(1)
expect(res.addTrailers as jest.Mock).toBeCalledTimes(1)
expect(res.flushHeaders as jest.Mock).toBeCalledTimes(1)

@@ -217,28 +246,88 @@ // clear the mock

// ensure they all have been cleared
expect((next as jest.Mock).mock.calls.length).toBe(0)
expect((next as jest.Mock).mock.calls.length).toBe(0)
expect((res.status as jest.Mock).mock.calls.length).toBe(0)
expect((res.sendStatus as jest.Mock).mock.calls.length).toBe(0)
expect((res.links as jest.Mock).mock.calls.length).toBe(0)
expect((res.send as jest.Mock).mock.calls.length).toBe(0)
expect((res.json as jest.Mock).mock.calls.length).toBe(0)
expect((res.jsonp as jest.Mock).mock.calls.length).toBe(0)
expect((res.sendFile as jest.Mock).mock.calls.length).toBe(0)
expect((res.sendfile as jest.Mock).mock.calls.length).toBe(0)
expect((res.download as jest.Mock).mock.calls.length).toBe(0)
expect((res.contentType as jest.Mock).mock.calls.length).toBe(0)
expect((res.type as jest.Mock).mock.calls.length).toBe(0)
expect((res.format as jest.Mock).mock.calls.length).toBe(0)
expect((res.attachment as jest.Mock).mock.calls.length).toBe(0)
expect((res.set as jest.Mock).mock.calls.length).toBe(0)
expect((res.header as jest.Mock).mock.calls.length).toBe(0)
expect((res.get as jest.Mock).mock.calls.length).toBe(0)
expect((res.clearCookie as jest.Mock).mock.calls.length).toBe(0)
expect((res.cookie as jest.Mock).mock.calls.length).toBe(0)
expect((res.location as jest.Mock).mock.calls.length).toBe(0)
expect((res.redirect as jest.Mock).mock.calls.length).toBe(0)
expect((res.render as jest.Mock).mock.calls.length).toBe(0)
expect((res.vary as jest.Mock).mock.calls.length).toBe(0)
expect((res.append as jest.Mock).mock.calls.length).toBe(0)
// express - Response
expect(next as jest.Mock).not.toBeCalled()
expect(next as jest.Mock).not.toBeCalled()
expect(res.status as jest.Mock).not.toBeCalled()
expect(res.sendStatus as jest.Mock).not.toBeCalled()
expect(res.links as jest.Mock).not.toBeCalled()
expect(res.send as jest.Mock).not.toBeCalled()
expect(res.json as jest.Mock).not.toBeCalled()
expect(res.jsonp as jest.Mock).not.toBeCalled()
expect(res.sendFile as jest.Mock).not.toBeCalled()
expect(res.sendfile as jest.Mock).not.toBeCalled()
expect(res.download as jest.Mock).not.toBeCalled()
expect(res.contentType as jest.Mock).not.toBeCalled()
expect(res.type as jest.Mock).not.toBeCalled()
expect(res.format as jest.Mock).not.toBeCalled()
expect(res.attachment as jest.Mock).not.toBeCalled()
expect(res.set as jest.Mock).not.toBeCalled()
expect(res.header as jest.Mock).not.toBeCalled()
expect(res.get as jest.Mock).not.toBeCalled()
expect(res.clearCookie as jest.Mock).not.toBeCalled()
expect(res.cookie as jest.Mock).not.toBeCalled()
expect(res.location as jest.Mock).not.toBeCalled()
expect(res.redirect as jest.Mock).not.toBeCalled()
expect(res.render as jest.Mock).not.toBeCalled()
expect(res.vary as jest.Mock).not.toBeCalled()
expect(res.append as jest.Mock).not.toBeCalled()
// http - ServerResponse
expect(res.assignSocket as jest.Mock).not.toBeCalled()
expect(res.detachSocket as jest.Mock).not.toBeCalled()
expect(res.writeContinue as jest.Mock).not.toBeCalled()
expect(res.writeHead as jest.Mock).not.toBeCalled()
expect(res.writeProcessing as jest.Mock).not.toBeCalled()
// http - OutgoingMessage
expect(res.setTimeout as jest.Mock).not.toBeCalled()
expect(res.setHeader as jest.Mock).not.toBeCalled()
expect(res.getHeader as jest.Mock).not.toBeCalled()
expect(res.getHeaders as jest.Mock).not.toBeCalled()
expect(res.getHeaderNames as jest.Mock).not.toBeCalled()
expect(res.hasHeader as jest.Mock).not.toBeCalled()
expect(res.removeHeader as jest.Mock).not.toBeCalled()
expect(res.addTrailers as jest.Mock).not.toBeCalled()
expect(res.flushHeaders as jest.Mock).not.toBeCalled()
})
})
function callAll(res: Response) {
// express - Response
res.status(123)
res.sendStatus(123)
res.links({})
res.send()
res.json()
res.jsonp()
res.sendFile('test')
res.sendfile('test')
res.download('test')
res.contentType('test')
res.type('test')
res.format({})
res.attachment()
res.set({})
res.header({})
res.get('test')
res.clearCookie('test')
res.cookie('test', 'test')
res.location('test')
res.redirect('test')
res.render('test')
res.vary('test')
res.append('test')
// http - ServerResponse
res.assignSocket({} as Socket)
res.detachSocket({} as Socket)
res.writeContinue()
res.writeHead(123)
res.writeProcessing()
// http - OutgoingMessage
res.setTimeout(123)
res.setHeader('test', 'test')
res.getHeader('test')
res.getHeaders()
res.getHeaderNames()
res.hasHeader('test')
res.removeHeader('test')
res.addTrailers([])
res.flushHeaders()
}

@@ -16,2 +16,3 @@ // Libraries

const response = {
// express - Response
status: jest.fn(),

@@ -41,7 +42,35 @@ sendStatus: jest.fn(),

headersSent: false,
locals: {},
charset: '',
app: {},
req: {},
// http - ServerResponse
assignSocket: jest.fn(),
detachSocket: jest.fn(),
writeContinue: jest.fn(),
writeHead: jest.fn(),
writeProcessing: jest.fn(),
statusCode: 0,
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: {},
}
// for the function that are chainable, return the response
// express - Response - chainable functions
response.status.mockReturnValue(response)

@@ -64,15 +93,10 @@ response.sendStatus.mockReturnValue(response)

response.append.mockReturnValue(response)
// http - ServerResponse - chainable functions
response.writeHead.mockReturnValue(response)
// http - OutgoingMessage - chainable functions
response.setTimeout.mockReturnValue(response)
/**
* these are not required to be chainable
* - get
* - render
* - sendFile
* - sendfile
* - download
* - redirect
*/
const clearAllMocks = (): void => {
next.mockClear()
// express - Response
response.status.mockClear()

@@ -101,2 +125,18 @@ response.sendStatus.mockClear()

response.append.mockClear()
// http - ServerResponse
response.assignSocket.mockClear()
response.detachSocket.mockClear()
response.writeContinue.mockClear()
response.writeHead.mockClear()
response.writeProcessing.mockClear()
// http - OutgoingMessage
response.setTimeout.mockClear()
response.setHeader.mockClear()
response.getHeader.mockClear()
response.getHeaders.mockClear()
response.getHeaderNames.mockClear()
response.hasHeader.mockClear()
response.removeHeader.mockClear()
response.addTrailers.mockClear()
response.flushHeaders.mockClear()
}

@@ -103,0 +143,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc