index.js 3.2 KB

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