index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. 'use strict';
  2. var _jestUtil;
  3. function _load_jestUtil() {
  4. return (_jestUtil = require('jest-util'));
  5. }
  6. var _jestMock;
  7. function _load_jestMock() {
  8. return (_jestMock = _interopRequireDefault(require('jest-mock')));
  9. }
  10. var _jsdom;
  11. function _load_jsdom() {
  12. return (_jsdom = require('jsdom'));
  13. }
  14. function _interopRequireDefault(obj) {
  15. return obj && obj.__esModule ? obj : {default: obj};
  16. }
  17. /**
  18. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  19. *
  20. * This source code is licensed under the MIT license found in the
  21. * LICENSE file in the root directory of this source tree.
  22. *
  23. */
  24. class JSDOMEnvironment {
  25. constructor(config) {
  26. let options =
  27. arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  28. this.dom = new (_jsdom || _load_jsdom()).JSDOM(
  29. '<!DOCTYPE html>',
  30. Object.assign(
  31. {
  32. pretendToBeVisual: true,
  33. runScripts: 'dangerously',
  34. url: config.testURL,
  35. virtualConsole: new (_jsdom || _load_jsdom()).VirtualConsole().sendTo(
  36. options.console || console
  37. )
  38. },
  39. config.testEnvironmentOptions
  40. )
  41. );
  42. const global = (this.global = this.dom.window.document.defaultView);
  43. // Node's error-message stack size is limited at 10, but it's pretty useful
  44. // to see more than that when a test fails.
  45. this.global.Error.stackTraceLimit = 100;
  46. (0, (_jestUtil || _load_jestUtil()).installCommonGlobals)(
  47. global,
  48. config.globals
  49. );
  50. // Report uncaught errors.
  51. this.errorEventListener = event => {
  52. if (userErrorListenerCount === 0 && event.error) {
  53. process.emit('uncaughtException', event.error);
  54. }
  55. };
  56. global.addEventListener('error', this.errorEventListener);
  57. // However, don't report them as uncaught if the user listens to 'error' event.
  58. // In that case, we assume the might have custom error handling logic.
  59. const originalAddListener = global.addEventListener;
  60. const originalRemoveListener = global.removeEventListener;
  61. let userErrorListenerCount = 0;
  62. global.addEventListener = function(name) {
  63. if (name === 'error') {
  64. userErrorListenerCount++;
  65. }
  66. return originalAddListener.apply(this, arguments);
  67. };
  68. global.removeEventListener = function(name) {
  69. if (name === 'error') {
  70. userErrorListenerCount--;
  71. }
  72. return originalRemoveListener.apply(this, arguments);
  73. };
  74. this.moduleMocker = new (
  75. _jestMock || _load_jestMock()
  76. ).default.ModuleMocker(global);
  77. const timerConfig = {
  78. idToRef: id => id,
  79. refToId: ref => ref
  80. };
  81. this.fakeTimers = new (_jestUtil || _load_jestUtil()).FakeTimers({
  82. config: config,
  83. global: global,
  84. moduleMocker: this.moduleMocker,
  85. timerConfig: timerConfig
  86. });
  87. }
  88. setup() {
  89. return Promise.resolve();
  90. }
  91. teardown() {
  92. if (this.fakeTimers) {
  93. this.fakeTimers.dispose();
  94. }
  95. if (this.global) {
  96. if (this.errorEventListener) {
  97. this.global.removeEventListener('error', this.errorEventListener);
  98. }
  99. // Dispose "document" to prevent "load" event from triggering.
  100. Object.defineProperty(this.global, 'document', {value: null});
  101. this.global.close();
  102. }
  103. this.errorEventListener = null;
  104. this.global = null;
  105. this.dom = null;
  106. this.fakeTimers = null;
  107. return Promise.resolve();
  108. }
  109. runScript(script) {
  110. if (this.dom) {
  111. return this.dom.runVMScript(script);
  112. }
  113. return null;
  114. }
  115. }
  116. module.exports = JSDOMEnvironment;