Giter Club home page Giter Club logo

jest-extensions's Introduction

Jest extensions

Usage

some-test.js:

const jestExtensions = require('jest-extensions')
jestExtensions.extend(expect)

test('success', () => {
  const mock = jest.fn()
  mock('arg1', 'arg2')
  mock('arg3', 'arg4')
  expect(mock).toBeCalledTimes(2)
  expect(mock).toBeCalledNthWith(0, 'arg1', 'arg2')
  expect(mock).toBeCalledNthWith(1, 'arg3', 'arg4')
})

test('fail', () => {
  const mock = jest.fn()
  expect(mock).toBeCalledOnce()
})

Assertions

  • toBeCalledTimes(number) is an alias for toHaveBeenCalledTimes(number)
  • toBeCalledOnce() is an alias for toHaveBeenCalledTimes(1)
  • toBeCalledNthWith(index, arg1, arg2, ...) is equivalent to:
expect(mockedFunction.mock.calls[index][0]).toEqual(arg1)
expect(mockedFunction.mock.calls[index][1]).toEqual(arg2)
// ...

Asymmetric matchers

  • expect.and(matcher1, matcher2, ...) - logical AND operator over many matchers.
  • expect.or(matcher1, matcher2, ...) - logical OR operator over many matchers.
  • expect.toContain(<value>) - checks that the value is contained in the actual collection.
  • expect.toHaveLength(<number>) - checks that the actual has a .length property and it is set to a certain numeric value.

Utilities

  • mockModuleClass gets a new class with all methods mocked, except those specified in arguments. For example:

./src/MyClass.js:

export default class MyClass {
  
  now() {
    return new Date
  }

  async getFromApi(date) {
    // asynchronously request external API
  }

  async getNowData() {
    return await this.getFromApi(this.now())
  }

}

./src/__tests__/MyClass.test.js:

import { mockModuleClass } from 'jest-extensions'

function mockMyClass(...unmockedMethods) {
  return mockModuleClass(__dirname + '/../MyClass.js', ...unmockedMethods)
}

describe('MyClass', () => {

  describe('getNowData', () => {

    it('succeeds', async () => {
      const sut = new mockMyClass('getNowData')()

      // All other methods are already mocked.
      // Even if mock implementations aren't defined,
      // no accidental side effect evaluations or state changes can occur.
      sut.now.mockImplementationOnce(() => new Date('2017-01-16'))
      sut.getFromApi.mockImplementation(() => ({ data: 'value' }))

      const result = await sut.getNowData()

      expect(sut.getFromApi).toBeCalledWith(new Date('2017-01-16'))
      expect(result).toEqual({ data: 'value' })
    })

  })

})

jest-extensions's People

Contributors

dependabot[bot] avatar rstuven avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  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.