ryanong / ts-jest-mock

A type definition and helper for jest mocks in TypeScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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é

💻 📖

Sébastien Dufour-Beauséjour

💻

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

About

A type definition and helper for jest mocks in TypeScript

License:MIT License


Languages

Language:JavaScript 50.7%Language:TypeScript 49.3%