installCommonGlobals.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = installCommonGlobals;
  6. function fs() {
  7. const data = _interopRequireWildcard(require('graceful-fs'));
  8. fs = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. var _createProcessObject = _interopRequireDefault(
  14. require('./createProcessObject')
  15. );
  16. var _deepCyclicCopy = _interopRequireDefault(require('./deepCyclicCopy'));
  17. function _interopRequireDefault(obj) {
  18. return obj && obj.__esModule ? obj : {default: obj};
  19. }
  20. function _getRequireWildcardCache(nodeInterop) {
  21. if (typeof WeakMap !== 'function') return null;
  22. var cacheBabelInterop = new WeakMap();
  23. var cacheNodeInterop = new WeakMap();
  24. return (_getRequireWildcardCache = function (nodeInterop) {
  25. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  26. })(nodeInterop);
  27. }
  28. function _interopRequireWildcard(obj, nodeInterop) {
  29. if (!nodeInterop && obj && obj.__esModule) {
  30. return obj;
  31. }
  32. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  33. return {default: obj};
  34. }
  35. var cache = _getRequireWildcardCache(nodeInterop);
  36. if (cache && cache.has(obj)) {
  37. return cache.get(obj);
  38. }
  39. var newObj = {};
  40. var hasPropertyDescriptor =
  41. Object.defineProperty && Object.getOwnPropertyDescriptor;
  42. for (var key in obj) {
  43. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  44. var desc = hasPropertyDescriptor
  45. ? Object.getOwnPropertyDescriptor(obj, key)
  46. : null;
  47. if (desc && (desc.get || desc.set)) {
  48. Object.defineProperty(newObj, key, desc);
  49. } else {
  50. newObj[key] = obj[key];
  51. }
  52. }
  53. }
  54. newObj.default = obj;
  55. if (cache) {
  56. cache.set(obj, newObj);
  57. }
  58. return newObj;
  59. }
  60. /**
  61. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  62. *
  63. * This source code is licensed under the MIT license found in the
  64. * LICENSE file in the root directory of this source tree.
  65. */
  66. const DTRACE = Object.keys(global).filter(key => key.startsWith('DTRACE'));
  67. function installCommonGlobals(globalObject, globals) {
  68. globalObject.process = (0, _createProcessObject.default)();
  69. const symbol = globalObject.Symbol; // Keep a reference to some globals that Jest needs
  70. Object.defineProperties(globalObject, {
  71. [symbol.for('jest-native-promise')]: {
  72. enumerable: false,
  73. value: Promise,
  74. writable: false
  75. },
  76. [symbol.for('jest-native-now')]: {
  77. enumerable: false,
  78. value: globalObject.Date.now.bind(globalObject.Date),
  79. writable: false
  80. },
  81. [symbol.for('jest-native-read-file')]: {
  82. enumerable: false,
  83. value: fs().readFileSync.bind(fs()),
  84. writable: false
  85. },
  86. [symbol.for('jest-native-write-file')]: {
  87. enumerable: false,
  88. value: fs().writeFileSync.bind(fs()),
  89. writable: false
  90. },
  91. [symbol.for('jest-native-exists-file')]: {
  92. enumerable: false,
  93. value: fs().existsSync.bind(fs()),
  94. writable: false
  95. },
  96. 'jest-symbol-do-not-touch': {
  97. enumerable: false,
  98. value: symbol,
  99. writable: false
  100. }
  101. }); // Forward some APIs.
  102. DTRACE.forEach(dtrace => {
  103. // @ts-expect-error: no index
  104. globalObject[dtrace] = function (...args) {
  105. // @ts-expect-error: no index
  106. return global[dtrace].apply(this, args);
  107. };
  108. });
  109. return Object.assign(globalObject, (0, _deepCyclicCopy.default)(globals));
  110. }