Jest 对象·续3
This is useful for scenarios such as one where the module being tested schedules a setTimeout() whose callback schedules another setTimeout() recursively (meaning the scheduling never stops). In these scenarios, it’s useful to be able to run forward in time by a single step at a time. In these scenarios, it’s useful to be able to run forward in time by a single step at a time.
jest.advanceTimersToNextTimer(steps)
Advances all timers by the needed milliseconds so that only the next timeouts/intervals will run.
Optionally, you can provide steps, so it will run steps amount of next timeouts/intervals.
jest.clearAllTimers()
Removes any pending timers from the timer system.
This means, if any timers have been scheduled (but have not yet executed), they will be cleared and will never have the opportunity to execute in the future.
jest.getTimerCount()
Returns the number of fake timers still left to run.
jest.setSystemTime(now?: number | Date)
Set the current system time used by fake timers. Simulates a user changing the system clock while your program is running. Set the current system time used by fake timers. Simulates a user changing the system clock while your program is running. It affects the current time but it does not in itself cause e.g. timers to fire; they will fire exactly as they would have done without the call to jest.setSystemTime().
Note: This function is only available when using modern fake timers implementation
jest.getRealSystemTime()
When mocking time, Date.now() will also be mocked. If you for some reason need access to the real current time, you can invoke this function. If you for some reason need access to the real current time, you can invoke this function.
Note: This function is only available when using modern fake timers implementation
Misc
jest.setTimeout(timeout)
Set the default timeout interval for tests and before/after hooks in milliseconds. This only affects the test file from which this function is called. This only affects the test file from which this function is called.
Note: The default timeout interval is 5 seconds if this method is not called.
Note: If you want to set the timeout for all test files, a good place to do this is in setupFilesAfterEnv.
Example:
jest.setTimeout(1000); // 1 second
jest.retryTimes()
Runs failed tests n-times until they pass or until the max number of retries is exhausted. Runs failed tests n-times until they pass or until the max number of retries is exhausted. This only works with the default jest-circus runner!
Example in a test:
jest.retryTimes(3);test('will fail', () => { expect(true).toBe(false);});
Returns the jest object for chaining.
