index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. 'use strict';
  2. function _vm() {
  3. const data = require('vm');
  4. _vm = function () {
  5. return data;
  6. };
  7. return data;
  8. }
  9. function _fakeTimers() {
  10. const data = require('@jest/fake-timers');
  11. _fakeTimers = function () {
  12. return data;
  13. };
  14. return data;
  15. }
  16. function _jestMock() {
  17. const data = require('jest-mock');
  18. _jestMock = function () {
  19. return data;
  20. };
  21. return data;
  22. }
  23. function _jestUtil() {
  24. const data = require('jest-util');
  25. _jestUtil = function () {
  26. return data;
  27. };
  28. return data;
  29. }
  30. function _defineProperty(obj, key, value) {
  31. if (key in obj) {
  32. Object.defineProperty(obj, key, {
  33. value: value,
  34. enumerable: true,
  35. configurable: true,
  36. writable: true
  37. });
  38. } else {
  39. obj[key] = value;
  40. }
  41. return obj;
  42. }
  43. class NodeEnvironment {
  44. constructor(config) {
  45. _defineProperty(this, 'context', void 0);
  46. _defineProperty(this, 'fakeTimers', void 0);
  47. _defineProperty(this, 'fakeTimersModern', void 0);
  48. _defineProperty(this, 'global', void 0);
  49. _defineProperty(this, 'moduleMocker', void 0);
  50. this.context = (0, _vm().createContext)();
  51. const global = (this.global = (0, _vm().runInContext)(
  52. 'this',
  53. Object.assign(this.context, config.testEnvironmentOptions)
  54. ));
  55. global.global = global;
  56. global.clearInterval = clearInterval;
  57. global.clearTimeout = clearTimeout;
  58. global.setInterval = setInterval;
  59. global.setTimeout = setTimeout;
  60. global.Buffer = Buffer;
  61. global.setImmediate = setImmediate;
  62. global.clearImmediate = clearImmediate;
  63. global.ArrayBuffer = ArrayBuffer; // TextEncoder (global or via 'util') references a Uint8Array constructor
  64. // different than the global one used by users in tests. This makes sure the
  65. // same constructor is referenced by both.
  66. global.Uint8Array = Uint8Array; // URL and URLSearchParams are global in Node >= 10
  67. if (typeof URL !== 'undefined' && typeof URLSearchParams !== 'undefined') {
  68. global.URL = URL;
  69. global.URLSearchParams = URLSearchParams;
  70. } // TextDecoder and TextDecoder are global in Node >= 11
  71. if (
  72. typeof TextEncoder !== 'undefined' &&
  73. typeof TextDecoder !== 'undefined'
  74. ) {
  75. global.TextEncoder = TextEncoder;
  76. global.TextDecoder = TextDecoder;
  77. } // queueMicrotask is global in Node >= 11
  78. if (typeof queueMicrotask !== 'undefined') {
  79. global.queueMicrotask = queueMicrotask;
  80. } // AbortController is global in Node >= 15
  81. if (typeof AbortController !== 'undefined') {
  82. global.AbortController = AbortController;
  83. } // AbortSignal is global in Node >= 15
  84. if (typeof AbortSignal !== 'undefined') {
  85. global.AbortSignal = AbortSignal;
  86. } // Event is global in Node >= 15.4
  87. if (typeof Event !== 'undefined') {
  88. global.Event = Event;
  89. } // EventTarget is global in Node >= 15.4
  90. if (typeof EventTarget !== 'undefined') {
  91. global.EventTarget = EventTarget;
  92. } // performance is global in Node >= 16
  93. if (typeof performance !== 'undefined') {
  94. global.performance = performance;
  95. } // atob and btoa are global in Node >= 16
  96. if (typeof atob !== 'undefined' && typeof btoa !== 'undefined') {
  97. global.atob = atob;
  98. global.btoa = btoa;
  99. }
  100. (0, _jestUtil().installCommonGlobals)(global, config.globals);
  101. this.moduleMocker = new (_jestMock().ModuleMocker)(global);
  102. const timerIdToRef = id => ({
  103. id,
  104. ref() {
  105. return this;
  106. },
  107. unref() {
  108. return this;
  109. }
  110. });
  111. const timerRefToId = timer => (timer && timer.id) || undefined;
  112. const timerConfig = {
  113. idToRef: timerIdToRef,
  114. refToId: timerRefToId
  115. };
  116. this.fakeTimers = new (_fakeTimers().LegacyFakeTimers)({
  117. config,
  118. global,
  119. moduleMocker: this.moduleMocker,
  120. timerConfig
  121. });
  122. this.fakeTimersModern = new (_fakeTimers().ModernFakeTimers)({
  123. config,
  124. global
  125. });
  126. }
  127. async setup() {}
  128. async teardown() {
  129. if (this.fakeTimers) {
  130. this.fakeTimers.dispose();
  131. }
  132. if (this.fakeTimersModern) {
  133. this.fakeTimersModern.dispose();
  134. }
  135. this.context = null;
  136. this.fakeTimers = null;
  137. this.fakeTimersModern = null;
  138. }
  139. getVmContext() {
  140. return this.context;
  141. }
  142. }
  143. module.exports = NodeEnvironment;