index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.run = run;
  6. function _os() {
  7. const data = _interopRequireDefault(require('os'));
  8. _os = function _os() {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _path() {
  14. const data = _interopRequireDefault(require('path'));
  15. _path = function _path() {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _chalk() {
  21. const data = _interopRequireDefault(require('chalk'));
  22. _chalk = function _chalk() {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _realpathNative() {
  28. const data = require('realpath-native');
  29. _realpathNative = function _realpathNative() {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _yargs() {
  35. const data = _interopRequireDefault(require('yargs'));
  36. _yargs = function _yargs() {
  37. return data;
  38. };
  39. return data;
  40. }
  41. function _console() {
  42. const data = require('@jest/console');
  43. _console = function _console() {
  44. return data;
  45. };
  46. return data;
  47. }
  48. function _jestUtil() {
  49. const data = require('jest-util');
  50. _jestUtil = function _jestUtil() {
  51. return data;
  52. };
  53. return data;
  54. }
  55. function _jestValidate() {
  56. const data = require('jest-validate');
  57. _jestValidate = function _jestValidate() {
  58. return data;
  59. };
  60. return data;
  61. }
  62. function _jestConfig() {
  63. const data = require('jest-config');
  64. _jestConfig = function _jestConfig() {
  65. return data;
  66. };
  67. return data;
  68. }
  69. var _version = require('../version');
  70. var args = _interopRequireWildcard(require('./args'));
  71. function _interopRequireWildcard(obj) {
  72. if (obj && obj.__esModule) {
  73. return obj;
  74. } else {
  75. var newObj = {};
  76. if (obj != null) {
  77. for (var key in obj) {
  78. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  79. var desc =
  80. Object.defineProperty && Object.getOwnPropertyDescriptor
  81. ? Object.getOwnPropertyDescriptor(obj, key)
  82. : {};
  83. if (desc.get || desc.set) {
  84. Object.defineProperty(newObj, key, desc);
  85. } else {
  86. newObj[key] = obj[key];
  87. }
  88. }
  89. }
  90. }
  91. newObj.default = obj;
  92. return newObj;
  93. }
  94. }
  95. function _interopRequireDefault(obj) {
  96. return obj && obj.__esModule ? obj : {default: obj};
  97. }
  98. function _objectSpread(target) {
  99. for (var i = 1; i < arguments.length; i++) {
  100. var source = arguments[i] != null ? arguments[i] : {};
  101. var ownKeys = Object.keys(source);
  102. if (typeof Object.getOwnPropertySymbols === 'function') {
  103. ownKeys = ownKeys.concat(
  104. Object.getOwnPropertySymbols(source).filter(function(sym) {
  105. return Object.getOwnPropertyDescriptor(source, sym).enumerable;
  106. })
  107. );
  108. }
  109. ownKeys.forEach(function(key) {
  110. _defineProperty(target, key, source[key]);
  111. });
  112. }
  113. return target;
  114. }
  115. function _defineProperty(obj, key, value) {
  116. if (key in obj) {
  117. Object.defineProperty(obj, key, {
  118. value: value,
  119. enumerable: true,
  120. configurable: true,
  121. writable: true
  122. });
  123. } else {
  124. obj[key] = value;
  125. }
  126. return obj;
  127. }
  128. function run(cliArgv, cliInfo) {
  129. const realFs = require('fs');
  130. const fs = require('graceful-fs');
  131. fs.gracefulify(realFs);
  132. let argv;
  133. if (cliArgv) {
  134. argv = cliArgv;
  135. } else {
  136. argv = _yargs()
  137. .default.usage(args.usage)
  138. .help(false)
  139. .version(false)
  140. .options(args.options).argv; // @ts-ignore: fix this at some point
  141. (0, _jestValidate().validateCLIOptions)(
  142. argv,
  143. _objectSpread({}, args.options, {
  144. deprecationEntries: _jestConfig().deprecationEntries
  145. })
  146. );
  147. }
  148. if (argv.help) {
  149. _yargs().default.showHelp();
  150. process.on('exit', () => (process.exitCode = 1));
  151. return;
  152. }
  153. if (argv.version) {
  154. console.log(`v${_version.VERSION}\n`);
  155. return;
  156. }
  157. if (!argv._.length) {
  158. console.log('Please provide a path to a script. (See --help for details)');
  159. process.on('exit', () => (process.exitCode = 1));
  160. return;
  161. }
  162. const root = (0, _realpathNative().sync)(process.cwd());
  163. const filePath = _path().default.resolve(root, argv._[0]);
  164. if (argv.debug) {
  165. const info = cliInfo ? ', ' + cliInfo.join(', ') : '';
  166. console.log(`Using Jest Runtime v${_version.VERSION}${info}`);
  167. } // TODO: Figure this out
  168. // @ts-ignore: this might not have the correct arguments
  169. const options = (0, _jestConfig().readConfig)(argv, root);
  170. const globalConfig = options.globalConfig; // Always disable automocking in scripts.
  171. const config = _objectSpread({}, options.projectConfig, {
  172. automock: false,
  173. unmockedModulePathPatterns: null
  174. }); // Break circular dependency
  175. const Runtime = require('..');
  176. Runtime.createContext(config, {
  177. maxWorkers: Math.max(_os().default.cpus().length - 1, 1),
  178. watchman: globalConfig.watchman
  179. })
  180. .then(hasteMap => {
  181. const Environment = require(config.testEnvironment);
  182. const environment = new Environment(config);
  183. (0, _jestUtil().setGlobal)(
  184. environment.global,
  185. 'console',
  186. new (_console()).CustomConsole(process.stdout, process.stderr)
  187. );
  188. (0, _jestUtil().setGlobal)(
  189. environment.global,
  190. 'jestProjectConfig',
  191. config
  192. );
  193. (0, _jestUtil().setGlobal)(
  194. environment.global,
  195. 'jestGlobalConfig',
  196. globalConfig
  197. );
  198. const runtime = new Runtime(config, environment, hasteMap.resolver);
  199. runtime.requireModule(filePath);
  200. })
  201. .catch(e => {
  202. console.error(_chalk().default.red(e.stack || e));
  203. process.on('exit', () => (process.exitCode = 1));
  204. });
  205. }