index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. 'use strict';
  2. function _vm() {
  3. const data = _interopRequireDefault(require('vm'));
  4. _vm = function _vm() {
  5. return data;
  6. };
  7. return data;
  8. }
  9. function _jestMock() {
  10. const data = require('jest-mock');
  11. _jestMock = function _jestMock() {
  12. return data;
  13. };
  14. return data;
  15. }
  16. function _jestUtil() {
  17. const data = require('jest-util');
  18. _jestUtil = function _jestUtil() {
  19. return data;
  20. };
  21. return data;
  22. }
  23. function _fakeTimers() {
  24. const data = require('@jest/fake-timers');
  25. _fakeTimers = function _fakeTimers() {
  26. return data;
  27. };
  28. return data;
  29. }
  30. function _interopRequireDefault(obj) {
  31. return obj && obj.__esModule ? obj : {default: obj};
  32. }
  33. function _defineProperty(obj, key, value) {
  34. if (key in obj) {
  35. Object.defineProperty(obj, key, {
  36. value: value,
  37. enumerable: true,
  38. configurable: true,
  39. writable: true
  40. });
  41. } else {
  42. obj[key] = value;
  43. }
  44. return obj;
  45. }
  46. class NodeEnvironment {
  47. constructor(config) {
  48. _defineProperty(this, 'context', void 0);
  49. _defineProperty(this, 'fakeTimers', void 0);
  50. _defineProperty(this, 'global', void 0);
  51. _defineProperty(this, 'moduleMocker', void 0);
  52. this.context = _vm().default.createContext();
  53. const global = (this.global = _vm().default.runInContext(
  54. 'this',
  55. Object.assign(this.context, config.testEnvironmentOptions)
  56. ));
  57. global.global = global;
  58. global.clearInterval = clearInterval;
  59. global.clearTimeout = clearTimeout;
  60. global.setInterval = setInterval;
  61. global.setTimeout = setTimeout;
  62. global.ArrayBuffer = ArrayBuffer; // URL and URLSearchParams are global in Node >= 10
  63. if (typeof URL !== 'undefined' && typeof URLSearchParams !== 'undefined') {
  64. /* global URL, URLSearchParams */
  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, TextDecoder */
  73. global.TextEncoder = TextEncoder;
  74. global.TextDecoder = TextDecoder;
  75. }
  76. (0, _jestUtil().installCommonGlobals)(global, config.globals);
  77. this.moduleMocker = new (_jestMock()).ModuleMocker(global);
  78. const timerIdToRef = id => ({
  79. id,
  80. ref() {
  81. return this;
  82. },
  83. unref() {
  84. return this;
  85. }
  86. });
  87. const timerRefToId = timer => (timer && timer.id) || undefined;
  88. const timerConfig = {
  89. idToRef: timerIdToRef,
  90. refToId: timerRefToId
  91. };
  92. this.fakeTimers = new (_fakeTimers()).JestFakeTimers({
  93. config,
  94. global,
  95. moduleMocker: this.moduleMocker,
  96. timerConfig
  97. });
  98. }
  99. setup() {
  100. return Promise.resolve();
  101. }
  102. teardown() {
  103. if (this.fakeTimers) {
  104. this.fakeTimers.dispose();
  105. }
  106. this.context = null;
  107. this.fakeTimers = null;
  108. return Promise.resolve();
  109. } // TS infers the return type to be `any`, since that's what `runInContext`
  110. // returns.
  111. runScript(script) {
  112. if (this.context) {
  113. return script.runInContext(this.context);
  114. }
  115. return null;
  116. }
  117. }
  118. module.exports = NodeEnvironment;