modernFakeTimers.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. target: this._global,
  100. toFake
  101. });
  102. this._fakingTime = true;
  103. }
  104. }
  105. reset() {
  106. if (this._checkFakeTimers()) {
  107. const {now} = this._clock;
  108. this._clock.reset();
  109. this._clock.setSystemTime(now);
  110. }
  111. }
  112. setSystemTime(now) {
  113. if (this._checkFakeTimers()) {
  114. this._clock.setSystemTime(now);
  115. }
  116. }
  117. getRealSystemTime() {
  118. return Date.now();
  119. }
  120. getTimerCount() {
  121. if (this._checkFakeTimers()) {
  122. return this._clock.countTimers();
  123. }
  124. return 0;
  125. }
  126. _checkFakeTimers() {
  127. if (!this._fakingTime) {
  128. this._global.console.warn(
  129. 'A function to advance timers was called but the timers API is not ' +
  130. 'mocked with fake timers. Call `jest.useFakeTimers()` in this test or ' +
  131. 'enable fake timers globally by setting `"timers": "fake"` in the ' +
  132. 'configuration file\nStack Trace:\n' +
  133. (0, _jestMessageUtil().formatStackTrace)(
  134. new Error().stack,
  135. this._config,
  136. {
  137. noStackTrace: false
  138. }
  139. )
  140. );
  141. }
  142. return this._fakingTime;
  143. }
  144. }
  145. exports.default = FakeTimers;