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

A lightweight Jest mock for unit testing Express

  • 0.4.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
221K
increased by17.2%
Maintainers
2
Weekly downloads
 
Created
Source

@jest-mock/express

A lightweight Jest mock for unit testing Express

Build Status Coverage Status

Getting Started

Installation:

yarn add --dev @jest-mock/express

npm install --save-dev @jest-mock/express

Importing:

import { getMockReq, getMockRes } from '@jest-mock/express'

Usage

getMockReq

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.

const mockReq = getMockReq()

To create mock requests with values you can just provide them to the function in any order and all are optional. The advantage of this is that it ensures all of the other properties are also provided with default values.

// an example GET request to retrieve an entity
const mockReq = getMockReq({ params: { id: '123' } })
// an example PUT request to update a person
const mockReq = getMockReq({
  params: { id: 564 },
  body: { firstname: 'James', lastname: 'Smith', age: 34 }
})

getMockRes

getMockRes will provided a mocked res that will provided jest mock functions for all common requirements.

const mockRes = getMockRes()

All of the returned mocked functions can be cleared with a single call to mockClear.

beforeEach(() => {
  mockRes.mockClear()
})

It will also provide a mocked next function as that is commonly used and will also be cleared as part of mockClear.

const mockRes = getMockRes()

test('will respond with the entity from the service', async () => {
  // generate a mock request with getMockReq (above)
  const mockReq = getMockReq({ params: { id: '123' } })

  await myController.get(mockReq, mockRes.res, mockRes.next)

  expect(mockRes.res.json).toHaveBeenCalledWith({
    message: expect.any(String),
    entity: theExpectedEntity
  })
  expect(mockRes.next).toHaveBeenCalledWith()
})

Available Scripts

In the project directory, you can run:

yarn build

Transpiles the TypeScript source to the dist folder.

yarn clean

Clears the dist folder.

yarn test

Runs the tests and produces the coverage output.

yarn test-watch

Runs the tests in watch mode.

yarn lint

Runs the linter over the project.

Keywords

FAQs

Package last updated on 20 Oct 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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