modernFakeTimers.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _fakeTimers() {
  7. const data = require('@sinonjs/fake-timers');
  8. _fakeTimers = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _jestMessageUtil() {
  14. const data = require('jest-message-util');
  15. _jestMessageUtil = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _defineProperty(obj, key, value) {
  21. if (key in obj) {
  22. Object.defineProperty(obj, key, {
  23. value: value,
  24. enumerable: true,
  25. configurable: true,
  26. writable: true
  27. });
  28. } else {
  29. obj[key] = value;
  30. }
  31. return obj;
  32. }
  33. class FakeTimers {
  34. constructor({global, config, maxLoops}) {
  35. _defineProperty(this, '_clock', void 0);
  36. _defineProperty(this, '_config', void 0);
  37. _defineProperty(this, '_fakingTime', void 0);
  38. _defineProperty(this, '_global', void 0);
  39. _defineProperty(this, '_fakeTimers', void 0);
  40. _defineProperty(this, '_maxLoops', void 0);
  41. this._global = global;
  42. this._config = config;
  43. this._maxLoops = maxLoops || 100000;
  44. this._fakingTime = false;
  45. this._fakeTimers = (0, _fakeTimers().withGlobal)(global);
  46. }
  47. clearAllTimers() {
  48. if (this._fakingTime) {
  49. this._clock.reset();
  50. }
  51. }
  52. dispose() {
  53. this.useRealTimers();
  54. }
  55. runAllTimers() {
  56. if (this._checkFakeTimers()) {
  57. this._clock.runAll();
  58. }
  59. }
  60. runOnlyPendingTimers() {
  61. if (this._checkFakeTimers()) {
  62. this._clock.runToLast();
  63. }
  64. }
  65. advanceTimersToNextTimer(steps = 1) {
  66. if (this._checkFakeTimers()) {
  67. for (let i = steps; i > 0; i--) {
  68. this._clock.next(); // Fire all timers at this point: https://github.com/sinonjs/fake-timers/issues/250
  69. this._clock.tick(0);
  70. if (this._clock.countTimers() === 0) {
  71. break;
  72. }
  73. }
  74. }
  75. }
  76. advanceTimersByTime(msToRun) {
  77. if (this._checkFakeTimers()) {
  78. this._clock.tick(msToRun);
  79. }
  80. }
  81. runAllTicks() {
  82. if (this._checkFakeTimers()) {
  83. // @ts-expect-error
  84. this._clock.runMicrotasks();
  85. }
  86. }
  87. useRealTimers() {
  88. if (this._fakingTime) {
  89. this._clock.uninstall();
  90. this._fakingTime = false;
  91. }
  92. }
  93. useFakeTimers() {
  94. if (!this._fakingTime) {
  95. const toFake = Object.keys(this._fakeTimers.timers);
  96. this._clock = this._fakeTimers.install({
  97. loopLimit: this._maxLoops,
  98. now: Date.now(),
  99. toFake
  100. });
  101. this._fakingTime = true;
  102. }
  103. }
  104. reset() {
  105. if (this._checkFakeTimers()) {
  106. const {now} = this._clock;
  107. this._clock.reset();
  108. this._clock.setSystemTime(now);
  109. }
  110. }
  111. setSystemTime(now) {
  112. if (this._checkFakeTimers()) {
  113. this._clock.setSystemTime(now);
  114. }
  115. }
  116. getRealSystemTime() {
  117. return Date.now();
  118. }
  119. getTimerCount() {
  120. if (this._checkFakeTimers()) {
  121. return this._clock.countTimers();
  122. }
  123. return 0;
  124. }
  125. _checkFakeTimers() {
  126. if (!this._fakingTime) {
  127. this._global.console.warn(
  128. 'A function to advance timers was called but the timers API is not ' +
  129. 'mocked with fake timers. Call `jest.useFakeTimers()` in this test or ' +
  130. 'enable fake timers globally by setting `"timers": "fake"` in the ' +
  131. 'configuration file\nStack Trace:\n' +
  132. (0, _jestMessageUtil().formatStackTrace)(
  133. new Error().stack,
  134. this._config,
  135. {
  136. noStackTrace: false
  137. }
  138. )
  139. );
  140. }
  141. return this._fakingTime;
  142. }
  143. }
  144. exports.default = FakeTimers;