index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. 'use strict';
  2. function _jsdom() {
  3. const data = require('jsdom');
  4. _jsdom = 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 JSDOMEnvironment {
  44. constructor(config, options = {}) {
  45. _defineProperty(this, 'dom', void 0);
  46. _defineProperty(this, 'fakeTimers', void 0);
  47. _defineProperty(this, 'fakeTimersModern', void 0);
  48. _defineProperty(this, 'global', void 0);
  49. _defineProperty(this, 'errorEventListener', void 0);
  50. _defineProperty(this, 'moduleMocker', void 0);
  51. this.dom = new (_jsdom().JSDOM)('<!DOCTYPE html>', {
  52. pretendToBeVisual: true,
  53. runScripts: 'dangerously',
  54. url: config.testURL,
  55. virtualConsole: new (_jsdom().VirtualConsole)().sendTo(
  56. options.console || console
  57. ),
  58. ...config.testEnvironmentOptions
  59. });
  60. const global = (this.global = this.dom.window.document.defaultView);
  61. if (!global) {
  62. throw new Error('JSDOM did not return a Window object');
  63. } // In the `jsdom@16`, ArrayBuffer was not added to Window, ref: https://github.com/jsdom/jsdom/commit/3a4fd6258e6b13e9cf8341ddba60a06b9b5c7b5b
  64. // Install ArrayBuffer to Window to fix it. Make sure the test is passed, ref: https://github.com/facebook/jest/pull/7626
  65. global.ArrayBuffer = ArrayBuffer; // Node's error-message stack size is limited at 10, but it's pretty useful
  66. // to see more than that when a test fails.
  67. this.global.Error.stackTraceLimit = 100;
  68. (0, _jestUtil().installCommonGlobals)(global, config.globals); // Report uncaught errors.
  69. this.errorEventListener = event => {
  70. if (userErrorListenerCount === 0 && event.error) {
  71. process.emit('uncaughtException', event.error);
  72. }
  73. };
  74. global.addEventListener('error', this.errorEventListener); // However, don't report them as uncaught if the user listens to 'error' event.
  75. // In that case, we assume the might have custom error handling logic.
  76. const originalAddListener = global.addEventListener;
  77. const originalRemoveListener = global.removeEventListener;
  78. let userErrorListenerCount = 0;
  79. global.addEventListener = function (...args) {
  80. if (args[0] === 'error') {
  81. userErrorListenerCount++;
  82. }
  83. return originalAddListener.apply(this, args);
  84. };
  85. global.removeEventListener = function (...args) {
  86. if (args[0] === 'error') {
  87. userErrorListenerCount--;
  88. }
  89. return originalRemoveListener.apply(this, args);
  90. };
  91. this.moduleMocker = new (_jestMock().ModuleMocker)(global);
  92. const timerConfig = {
  93. idToRef: id => id,
  94. refToId: ref => ref
  95. };
  96. this.fakeTimers = new (_fakeTimers().LegacyFakeTimers)({
  97. config,
  98. global,
  99. moduleMocker: this.moduleMocker,
  100. timerConfig
  101. });
  102. this.fakeTimersModern = new (_fakeTimers().ModernFakeTimers)({
  103. config,
  104. global
  105. });
  106. }
  107. async setup() {}
  108. async teardown() {
  109. if (this.fakeTimers) {
  110. this.fakeTimers.dispose();
  111. }
  112. if (this.fakeTimersModern) {
  113. this.fakeTimersModern.dispose();
  114. }
  115. if (this.global) {
  116. if (this.errorEventListener) {
  117. this.global.removeEventListener('error', this.errorEventListener);
  118. } // Dispose "document" to prevent "load" event from triggering.
  119. Object.defineProperty(this.global, 'document', {
  120. value: null
  121. });
  122. this.global.close();
  123. }
  124. this.errorEventListener = null; // @ts-expect-error
  125. this.global = null;
  126. this.dom = null;
  127. this.fakeTimers = null;
  128. this.fakeTimersModern = null;
  129. }
  130. runScript(script) {
  131. if (this.dom) {
  132. return script.runInContext(this.dom.getInternalVMContext());
  133. }
  134. return null;
  135. }
  136. getVmContext() {
  137. if (this.dom) {
  138. return this.dom.getInternalVMContext();
  139. }
  140. return null;
  141. }
  142. }
  143. module.exports = JSDOMEnvironment;