Giter Club home page Giter Club logo

jest-mock-express's Introduction

@jest-mock/express

A lightweight Jest mock for unit testing Express

Build Status Coverage Status Known Vulnerabilities GitHub package.json version npm NPM

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

Request - 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 object with mocked functions and default values for properties.

const req = getMockReq()

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.

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

Response - getMockRes

getMockRes will provide a res object with Jest mock functions. Chaining has been implemented for the applicable functions.

const { res, next, clearMockRes } = getMockRes()

All of the returned mock functions can be cleared with a single call to mockClear. An alias is also provided called clearMockRes.

const { res, next, clearMockRes } = getMockRes()

beforeEach(() => {
  clearMockRes()
})

It will also provide a mock next function for convenience. That will also be cleared as part of the call to mockClear/clearMockRes.

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.

const { res, next, clearMockRes } = getMockRes({
  locals: {
    user: getLoggedInUser(),
  },
})

Example

A full example could be:

// 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 with params
  const req = getMockReq({ params: { id: 'abc-def' } })

  // provide the mock req, res, and next to assert
  await myController.getEntity(req, res, next)

  expect(res.json).toHaveBeenCalledWith(
    expect.objectContaining({
      id: 'abc-def',
    }),
  )
  expect(next).toBeCalled()
})

jest-mock-express's People

Contributors

aboyce avatar bikk-uk avatar dependabot[bot] avatar saramorillon avatar snyk-bot avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.