Skip to content

lbenie/ts-jest-mock

Repository files navigation

Format code Release Test code Mutation testing badge Commitizen friendly semantic-release npm npm npm bundle size (minified) All Contributors

forthebadge forthebadge

ts-jest-mock

A way to get jest type definition infered automagically.

Pre-requisites

This library takes for granted that you have your tests setup with jest.

Usage

Directly in a test

import { createMock } from 'ts-jest-mock'
import { someFn } from 'module_a'

jest.mock('module_a')

describe('something', () => {
  it('some test', () => {
    const someFnMock = createMock(someFn)

    someFnMock.mockReturnValue('') <-- Type inference yay :)
  })
})

Or with a beforeEach pattern

import { createMock } from 'ts-jest-mock'
import { someFn } from 'module_a'

jest.mock('module_a')

const someFnMock = createMock(someFn)

describe('something', () => {
  beforeEach(() => {
    someFnMock.mockReturnValue('') <-- Type inference yay :)
  })

  it('some test', () => {
    expect(someFnMock).toBe('')
  })
})

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Lucien Bénié
Lucien Bénié

💻 📖
Sébastien Dufour-Beauséjour
Sébastien Dufour-Beauséjour

💻
Ryan Ong
Ryan Ong

💻

This project follows the all-contributors specification. Contributions of any kind welcome!