index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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)(
  52. typeof config.testEnvironmentOptions.html === 'string'
  53. ? config.testEnvironmentOptions.html
  54. : '<!DOCTYPE html>',
  55. {
  56. pretendToBeVisual: true,
  57. resources:
  58. typeof config.testEnvironmentOptions.userAgent === 'string'
  59. ? new (_jsdom().ResourceLoader)({
  60. userAgent: config.testEnvironmentOptions.userAgent
  61. })
  62. : undefined,
  63. runScripts: 'dangerously',
  64. url: config.testURL,
  65. virtualConsole: new (_jsdom().VirtualConsole)().sendTo(
  66. (options === null || options === void 0 ? void 0 : options.console) ||
  67. console
  68. ),
  69. ...config.testEnvironmentOptions
  70. }
  71. );
  72. const global = (this.global = this.dom.window.document.defaultView);
  73. if (!global) {
  74. throw new Error('JSDOM did not return a Window object');
  75. } // for "universal" code (code should use `globalThis`)
  76. global.global = global; // Node's error-message stack size is limited at 10, but it's pretty useful
  77. // to see more than that when a test fails.
  78. this.global.Error.stackTraceLimit = 100;
  79. (0, _jestUtil().installCommonGlobals)(global, config.globals); // TODO: remove this ASAP, but it currently causes tests to run really slow
  80. global.Buffer = Buffer; // Report uncaught errors.
  81. this.errorEventListener = event => {
  82. if (userErrorListenerCount === 0 && event.error) {
  83. process.emit('uncaughtException', event.error);
  84. }
  85. };
  86. global.addEventListener('error', this.errorEventListener); // However, don't report them as uncaught if the user listens to 'error' event.
  87. // In that case, we assume the might have custom error handling logic.
  88. const originalAddListener = global.addEventListener;
  89. const originalRemoveListener = global.removeEventListener;
  90. let userErrorListenerCount = 0;
  91. global.addEventListener = function (...args) {
  92. if (args[0] === 'error') {
  93. userErrorListenerCount++;
  94. }
  95. return originalAddListener.apply(this, args);
  96. };
  97. global.removeEventListener = function (...args) {
  98. if (args[0] === 'error') {
  99. userErrorListenerCount--;
  100. }
  101. return originalRemoveListener.apply(this, args);
  102. };
  103. this.moduleMocker = new (_jestMock().ModuleMocker)(global);
  104. const timerConfig = {
  105. idToRef: id => id,
  106. refToId: ref => ref
  107. };
  108. this.fakeTimers = new (_fakeTimers().LegacyFakeTimers)({
  109. config,
  110. global: global,
  111. moduleMocker: this.moduleMocker,
  112. timerConfig
  113. });
  114. this.fakeTimersModern = new (_fakeTimers().ModernFakeTimers)({
  115. config,
  116. global: global
  117. });
  118. }
  119. async setup() {}
  120. async teardown() {
  121. if (this.fakeTimers) {
  122. this.fakeTimers.dispose();
  123. }
  124. if (this.fakeTimersModern) {
  125. this.fakeTimersModern.dispose();
  126. }
  127. if (this.global) {
  128. if (this.errorEventListener) {
  129. this.global.removeEventListener('error', this.errorEventListener);
  130. }
  131. this.global.close(); // Dispose "document" to prevent "load" event from triggering.
  132. // Note that this.global.close() will trigger the CustomElement::disconnectedCallback
  133. // Do not reset the document before CustomElement disconnectedCallback function has finished running,
  134. // document should be accessible within disconnectedCallback.
  135. Object.defineProperty(this.global, 'document', {
  136. value: null
  137. });
  138. }
  139. this.errorEventListener = null; // @ts-expect-error
  140. this.global = null;
  141. this.dom = null;
  142. this.fakeTimers = null;
  143. this.fakeTimersModern = null;
  144. }
  145. getVmContext() {
  146. if (this.dom) {
  147. return this.dom.getInternalVMContext();
  148. }
  149. return null;
  150. }
  151. }
  152. module.exports = JSDOMEnvironment;