📒
wiki
  • Introduction
  • Coding
    • Git
    • NPM
    • Yarn
    • VIM
    • tmux
    • Terminal
    • HTML
    • Node
    • JavaScript
      • Types
    • TypeScript
    • React
    • Jest
    • FLow
    • Functional programming
    • Data Structures
    • Coding Exercises
    • Design Systems
    • VSCode
  • Learn
    • Languages
  • Health
  • Bikes
  • Ideas
  • Journals/Wiki
  • Looking back
    • 2019
      • September
      • October
      • November
      • December
Powered by GitBook
On this page
  • Links
  • Asynchronous or time-based events (timers)
  • Verify that function was called with multiple arguments
  • Verify that the same function was called multiple times with different arguments

Was this helpful?

  1. Coding

Jest

PreviousReactNextFLow

Last updated 3 years ago

Was this helpful?

Links

Asynchronous or time-based events (timers)

describe('do some crazy stuff with timers.', () => {
  beforeEach(() => {
    // declare the intent of using timers
    jest.useFakeTimers()
  })

 it('should .', () => {
    // mock if needed
    const callback = jest.fn()

    // mount component

    // follow by expect before timers

    // execute timers
    jest.runAllTimers()

    // expect something after timers have been executed
    expect(callback).toHaveBeenCalledTimes(1)
  })

Verify that function was called with multiple arguments

Also available under the alias: .toBeCalledWith()

Verify that the same function was called multiple times with different arguments

it('should be able to change access rights.', () => {
  expect(onUpdateAccess).not.toBeCalled()

  const newAccess = {
    ...report.access,
    view: {
      type: 'user',
      id: '3',
    },
  }

  component.find(Rights).prop('onChange')(newAccess)

  expect(onUpdateAccess).toBeCalledTimes(2)
  expect(onUpdateAccess).toBeCalledWith('edit', newAccess.edit)
  expect(onUpdateAccess).toBeCalledWith('view', newAccess.view)
})

Mocking Node modules
mockFn.mockClear
mockFn.mockReset
expect.anything()
.toHaveBeenCalledWith(arg1, arg2, ...)