legacyFakeTimers.d.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. import { StackTraceConfig } from 'jest-message-util';
  8. import type { ModuleMocker } from 'jest-mock';
  9. declare type Callback = (...args: Array<unknown>) => void;
  10. declare type TimerConfig<Ref> = {
  11. idToRef: (id: number) => Ref;
  12. refToId: (ref: Ref) => number | void;
  13. };
  14. declare type GlobalThis = typeof globalThis;
  15. interface FakeTimersGlobal extends GlobalThis {
  16. cancelAnimationFrame: (handle: number) => void;
  17. requestAnimationFrame: (callback: (time: number) => void) => number;
  18. }
  19. export default class FakeTimers<TimerRef> {
  20. private _cancelledTicks;
  21. private _config;
  22. private _disposed?;
  23. private _fakeTimerAPIs;
  24. private _global;
  25. private _immediates;
  26. private _maxLoops;
  27. private _moduleMocker;
  28. private _now;
  29. private _ticks;
  30. private _timerAPIs;
  31. private _timers;
  32. private _uuidCounter;
  33. private _timerConfig;
  34. constructor({ global, moduleMocker, timerConfig, config, maxLoops, }: {
  35. global: FakeTimersGlobal;
  36. moduleMocker: ModuleMocker;
  37. timerConfig: TimerConfig<TimerRef>;
  38. config: StackTraceConfig;
  39. maxLoops?: number;
  40. });
  41. clearAllTimers(): void;
  42. dispose(): void;
  43. reset(): void;
  44. runAllTicks(): void;
  45. runAllImmediates(): void;
  46. private _runImmediate;
  47. runAllTimers(): void;
  48. runOnlyPendingTimers(): void;
  49. advanceTimersToNextTimer(steps?: number): void;
  50. advanceTimersByTime(msToRun: number): void;
  51. runWithRealTimers(cb: Callback): void;
  52. useRealTimers(): void;
  53. useFakeTimers(): void;
  54. getTimerCount(): number;
  55. private _checkFakeTimers;
  56. private _createMocks;
  57. private _fakeClearTimer;
  58. private _fakeClearImmediate;
  59. private _fakeNextTick;
  60. private _fakeRequestAnimationFrame;
  61. private _fakeSetImmediate;
  62. private _fakeSetInterval;
  63. private _fakeSetTimeout;
  64. private _getNextTimerHandle;
  65. private _runTimerHandle;
  66. }
  67. export {};