index.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.run = run;
  6. function _os() {
  7. const data = require('os');
  8. _os = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function path() {
  14. const data = _interopRequireWildcard(require('path'));
  15. path = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _chalk() {
  21. const data = _interopRequireDefault(require('chalk'));
  22. _chalk = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _yargs() {
  28. const data = _interopRequireDefault(require('yargs'));
  29. _yargs = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _console() {
  35. const data = require('@jest/console');
  36. _console = function () {
  37. return data;
  38. };
  39. return data;
  40. }
  41. function _jestConfig() {
  42. const data = require('jest-config');
  43. _jestConfig = function () {
  44. return data;
  45. };
  46. return data;
  47. }
  48. function _jestUtil() {
  49. const data = require('jest-util');
  50. _jestUtil = function () {
  51. return data;
  52. };
  53. return data;
  54. }
  55. function _jestValidate() {
  56. const data = require('jest-validate');
  57. _jestValidate = function () {
  58. return data;
  59. };
  60. return data;
  61. }
  62. function _version() {
  63. const data = require('../version');
  64. _version = function () {
  65. return data;
  66. };
  67. return data;
  68. }
  69. var args = _interopRequireWildcard(require('./args'));
  70. function _interopRequireDefault(obj) {
  71. return obj && obj.__esModule ? obj : {default: obj};
  72. }
  73. function _getRequireWildcardCache() {
  74. if (typeof WeakMap !== 'function') return null;
  75. var cache = new WeakMap();
  76. _getRequireWildcardCache = function () {
  77. return cache;
  78. };
  79. return cache;
  80. }
  81. function _interopRequireWildcard(obj) {
  82. if (obj && obj.__esModule) {
  83. return obj;
  84. }
  85. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  86. return {default: obj};
  87. }
  88. var cache = _getRequireWildcardCache();
  89. if (cache && cache.has(obj)) {
  90. return cache.get(obj);
  91. }
  92. var newObj = {};
  93. var hasPropertyDescriptor =
  94. Object.defineProperty && Object.getOwnPropertyDescriptor;
  95. for (var key in obj) {
  96. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  97. var desc = hasPropertyDescriptor
  98. ? Object.getOwnPropertyDescriptor(obj, key)
  99. : null;
  100. if (desc && (desc.get || desc.set)) {
  101. Object.defineProperty(newObj, key, desc);
  102. } else {
  103. newObj[key] = obj[key];
  104. }
  105. }
  106. }
  107. newObj.default = obj;
  108. if (cache) {
  109. cache.set(obj, newObj);
  110. }
  111. return newObj;
  112. }
  113. /**
  114. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  115. *
  116. * This source code is licensed under the MIT license found in the
  117. * LICENSE file in the root directory of this source tree.
  118. */
  119. async function run(cliArgv, cliInfo) {
  120. let argv;
  121. if (cliArgv) {
  122. argv = cliArgv;
  123. } else {
  124. argv = _yargs()
  125. .default.usage(args.usage)
  126. .help(false)
  127. .version(false)
  128. .options(args.options).argv;
  129. (0, _jestValidate().validateCLIOptions)(argv, {
  130. ...args.options,
  131. deprecationEntries: _jestConfig().deprecationEntries
  132. });
  133. }
  134. if (argv.help) {
  135. _yargs().default.showHelp();
  136. process.on('exit', () => (process.exitCode = 1));
  137. return;
  138. }
  139. if (argv.version) {
  140. console.log(`v${_version().VERSION}\n`);
  141. return;
  142. }
  143. if (!argv._.length) {
  144. console.log('Please provide a path to a script. (See --help for details)');
  145. process.on('exit', () => (process.exitCode = 1));
  146. return;
  147. }
  148. const root = (0, _jestUtil().tryRealpath)(process.cwd());
  149. const filePath = path().resolve(root, argv._[0]);
  150. if (argv.debug) {
  151. const info = cliInfo ? ', ' + cliInfo.join(', ') : '';
  152. console.log(`Using Jest Runtime v${_version().VERSION}${info}`);
  153. }
  154. const options = await (0, _jestConfig().readConfig)(argv, root);
  155. const globalConfig = options.globalConfig; // Always disable automocking in scripts.
  156. const config = {...options.projectConfig, automock: false};
  157. const Runtime = require('..');
  158. try {
  159. var _runtime$unstable_sho2;
  160. const hasteMap = await Runtime.createContext(config, {
  161. maxWorkers: Math.max((0, _os().cpus)().length - 1, 1),
  162. watchman: globalConfig.watchman
  163. });
  164. const Environment = require(config.testEnvironment);
  165. const environment = new Environment(config);
  166. (0, _jestUtil().setGlobal)(
  167. environment.global,
  168. 'console',
  169. new (_console().CustomConsole)(process.stdout, process.stderr)
  170. );
  171. (0, _jestUtil().setGlobal)(environment.global, 'jestProjectConfig', config);
  172. (0, _jestUtil().setGlobal)(
  173. environment.global,
  174. 'jestGlobalConfig',
  175. globalConfig
  176. );
  177. const runtime = new Runtime(
  178. config,
  179. environment,
  180. hasteMap.resolver,
  181. undefined,
  182. undefined,
  183. filePath
  184. );
  185. for (const path of config.setupFiles) {
  186. var _runtime$unstable_sho;
  187. // TODO: remove ? in Jest 26
  188. const esm =
  189. (_runtime$unstable_sho = runtime.unstable_shouldLoadAsEsm) === null ||
  190. _runtime$unstable_sho === void 0
  191. ? void 0
  192. : _runtime$unstable_sho.call(runtime, path);
  193. if (esm) {
  194. await runtime.unstable_importModule(path);
  195. } else {
  196. runtime.requireModule(path);
  197. }
  198. } // TODO: remove ? in Jest 26
  199. const esm =
  200. (_runtime$unstable_sho2 = runtime.unstable_shouldLoadAsEsm) === null ||
  201. _runtime$unstable_sho2 === void 0
  202. ? void 0
  203. : _runtime$unstable_sho2.call(runtime, filePath);
  204. if (esm) {
  205. await runtime.unstable_importModule(filePath);
  206. } else {
  207. runtime.requireModule(filePath);
  208. }
  209. } catch (e) {
  210. console.error(_chalk().default.red(e.stack || e));
  211. process.on('exit', () => (process.exitCode = 1));
  212. }
  213. }