index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.ArrayBuffer = ArrayBuffer; // TextEncoder (global or via 'util') references a Uint8Array constructor
  61. // different than the global one used by users in tests. This makes sure the
  62. // same constructor is referenced by both.
  63. global.Uint8Array = Uint8Array; // URL and URLSearchParams are global in Node >= 10
  64. if (typeof URL !== 'undefined' && typeof URLSearchParams !== 'undefined') {
  65. global.URL = URL;
  66. global.URLSearchParams = URLSearchParams;
  67. } // TextDecoder and TextDecoder are global in Node >= 11
  68. if (
  69. typeof TextEncoder !== 'undefined' &&
  70. typeof TextDecoder !== 'undefined'
  71. ) {
  72. global.TextEncoder = TextEncoder;
  73. global.TextDecoder = TextDecoder;
  74. } // queueMicrotask is global in Node >= 11
  75. if (typeof queueMicrotask !== 'undefined') {
  76. global.queueMicrotask = queueMicrotask;
  77. }
  78. (0, _jestUtil().installCommonGlobals)(global, config.globals);
  79. this.moduleMocker = new (_jestMock().ModuleMocker)(global);
  80. const timerIdToRef = id => ({
  81. id,
  82. ref() {
  83. return this;
  84. },
  85. unref() {
  86. return this;
  87. }
  88. });
  89. const timerRefToId = timer => (timer && timer.id) || undefined;
  90. const timerConfig = {
  91. idToRef: timerIdToRef,
  92. refToId: timerRefToId
  93. };
  94. this.fakeTimers = new (_fakeTimers().LegacyFakeTimers)({
  95. config,
  96. global,
  97. moduleMocker: this.moduleMocker,
  98. timerConfig
  99. });
  100. this.fakeTimersModern = new (_fakeTimers().ModernFakeTimers)({
  101. config,
  102. global
  103. });
  104. }
  105. async setup() {}
  106. async teardown() {
  107. if (this.fakeTimers) {
  108. this.fakeTimers.dispose();
  109. }
  110. if (this.fakeTimersModern) {
  111. this.fakeTimersModern.dispose();
  112. }
  113. this.context = null;
  114. this.fakeTimers = null;
  115. this.fakeTimersModern = null;
  116. } // TS infers the return type to be `any`, since that's what `runInContext`
  117. // returns.
  118. runScript(script) {
  119. if (this.context) {
  120. return script.runInContext(this.context);
  121. }
  122. return null;
  123. }
  124. getVmContext() {
  125. return this.context;
  126. }
  127. }
  128. module.exports = NodeEnvironment;