Preparation conditions

Build on the directories and code created in the tutorial in Section 1. If you haven’t seen the first tutorial, follow me for previous articles in this series

This tutorial will explain how to test a timer in jest. Make a copy of the code in Section 1 and empty the contents of the index.js and index.test.js files

Write some methods to be tested in index.js

export const timer1 = (callback) = > {
    setTimeout((a)= > {
        callback()
    }, 3000)}export const timer2 = (callback) = > {
    setTimeout((a)= > {
        callback()
        setTimeout((a)= > {
            callback()
        }, 3000)},3000)}Copy the code

1. Use the done method

Write test cases in index.test.js

import { timer1, timer2 } from './index'

/** * Test the timer with done */
// it is an alias for test
it('Use done, test timer', (done) => {
  timer1((a)= > {
    expect(1).toBe(1)
    done()
  })
})
Copy the code

The problem is that if the timer is long, the test case will take a long time to run, so it is not recommended

2. Run the useFakeTimers + runAllTimers method

/** * Test timer1 using the useFakeTimers + runAllTimers method */
// it is an alias for test
it('Test timer1 using the useFakeTimers + runAllTimers method', () = > {const fn = jest.fn()
  timer1(fn)
  jest.runAllTimers()
  expect(fn).toHaveBeenCalledTimes(1) // Test the number of function calls
})

/** * Use the useFakeTimers + runAllTimers method to test timer2 */
// it is an alias for test
it('Use the useFakeTimers + runAllTimers method to test timer2', () = > {const fn = jest.fn()
  timer2(fn)

  // Number of tests
  jest.runAllTimers()
  expect(fn).toHaveBeenCalledTimes(2) // Test the number of function calls

  // If you only want to test once, you can use the following methods
  // jest.runOnlyPendingTimers()
  // expect(fn).toHaveBeenCalledTimes(1)

})
Copy the code

This method does not require waiting for the timer to complete, which is my preferred method

  • usejest.useFakeTimers()Declare the time to use the virtual
  • To usejest.runAllTimers()End all timers immediately
  • If there are multiple timers internally, only one timer can be used to runjest.runOnlyPendingTimers()

3. Run the useFakeTimers + advanceTimersByTime method

import { timer1, timer2 } from './index'

// The statement uses a false time
jest.useFakeTimers()

/** * Test timer2 using the useFakeTimers + advanceTimersByTime method */
// it is an alias for test
it('Test timer2 using the useFakeTimers + advanceTimersByTime method', () = > {const fn = jest.fn()
  timer2(fn)
  jest.advanceTimersByTime(3000) // Set the time to 3000s immediately
  expect(fn).toHaveBeenCalledTimes(1)
  jest.advanceTimersByTime(3000) // Set the time to 3000s immediately later
  expect(fn).toHaveBeenCalledTimes(2)})Copy the code
  • usejest.advanceTimersByTime()You can immediately set how many milliseconds to delay

These are the three methods to test the timer, and I recommend the second method

The next tutorial will cover how to test snapshots in JEST

My ability is limited, the article may have incorrect or inappropriate parts, I hope you can point out

Pay attention to the public number, and I learn the front-end necessary skills, front-end automation test JEST