index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. 'use strict';
  2. var _vm;
  3. function _load_vm() {
  4. return (_vm = _interopRequireDefault(require('vm')));
  5. }
  6. var _jestUtil;
  7. function _load_jestUtil() {
  8. return (_jestUtil = require('jest-util'));
  9. }
  10. var _jestMock;
  11. function _load_jestMock() {
  12. return (_jestMock = _interopRequireDefault(require('jest-mock')));
  13. }
  14. function _interopRequireDefault(obj) {
  15. return obj && obj.__esModule ? obj : {default: obj};
  16. }
  17. class NodeEnvironment {
  18. constructor(config) {
  19. this.context = (_vm || _load_vm()).default.createContext();
  20. const global = (this.global = (_vm || _load_vm()).default.runInContext(
  21. 'this',
  22. Object.assign(this.context, config.testEnvironmentOptions)
  23. ));
  24. global.global = global;
  25. global.clearInterval = clearInterval;
  26. global.clearTimeout = clearTimeout;
  27. global.setInterval = setInterval;
  28. global.setTimeout = setTimeout;
  29. // URL and URLSearchParams are global in Node >= 10
  30. if (typeof URL !== 'undefined' && typeof URLSearchParams !== 'undefined') {
  31. /* global URL, URLSearchParams */
  32. global.URL = URL;
  33. global.URLSearchParams = URLSearchParams;
  34. }
  35. (0, (_jestUtil || _load_jestUtil()).installCommonGlobals)(
  36. global,
  37. config.globals
  38. );
  39. this.moduleMocker = new (
  40. _jestMock || _load_jestMock()
  41. ).default.ModuleMocker(global);
  42. const timerIdToRef = id => ({
  43. id: id,
  44. ref: function() {
  45. return this;
  46. },
  47. unref: function() {
  48. return this;
  49. }
  50. });
  51. const timerRefToId = timer => (timer && timer.id) || null;
  52. const timerConfig = {
  53. idToRef: timerIdToRef,
  54. refToId: timerRefToId
  55. };
  56. this.fakeTimers = new (_jestUtil || _load_jestUtil()).FakeTimers({
  57. config: config,
  58. global: global,
  59. moduleMocker: this.moduleMocker,
  60. timerConfig: timerConfig
  61. });
  62. }
  63. setup() {
  64. return Promise.resolve();
  65. }
  66. teardown() {
  67. if (this.fakeTimers) {
  68. this.fakeTimers.dispose();
  69. }
  70. this.context = null;
  71. this.fakeTimers = null;
  72. return Promise.resolve();
  73. }
  74. // Disabling rule as return type depends on script's return type.
  75. /* eslint-disable flowtype/no-weak-types */
  76. runScript(script) {
  77. /* eslint-enable flowtype/no-weak-types */
  78. if (this.context) {
  79. return script.runInContext(this.context);
  80. }
  81. return null;
  82. }
  83. }
  84. /**
  85. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  86. *
  87. * This source code is licensed under the MIT license found in the
  88. * LICENSE file in the root directory of this source tree.
  89. *
  90. *
  91. */
  92. module.exports = NodeEnvironment;