index.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.descriptions = exports.defaults = exports.replaceRootDirInPath = exports.deprecationEntries = exports.normalize = exports.isJSONString = exports.getTestEnvironment = undefined;
  6. var _utils;
  7. function _load_utils() {
  8. return (_utils = require('./utils'));
  9. }
  10. Object.defineProperty(exports, 'getTestEnvironment', {
  11. enumerable: true,
  12. get: function() {
  13. return (_utils || _load_utils()).getTestEnvironment;
  14. }
  15. });
  16. Object.defineProperty(exports, 'isJSONString', {
  17. enumerable: true,
  18. get: function() {
  19. return (_utils || _load_utils()).isJSONString;
  20. }
  21. });
  22. var _normalize2;
  23. function _load_normalize() {
  24. return (_normalize2 = require('./normalize'));
  25. }
  26. Object.defineProperty(exports, 'normalize', {
  27. enumerable: true,
  28. get: function() {
  29. return _interopRequireDefault(_normalize2 || _load_normalize()).default;
  30. }
  31. });
  32. var _Deprecated;
  33. function _load_Deprecated() {
  34. return (_Deprecated = require('./Deprecated'));
  35. }
  36. Object.defineProperty(exports, 'deprecationEntries', {
  37. enumerable: true,
  38. get: function() {
  39. return _interopRequireDefault(_Deprecated || _load_Deprecated()).default;
  40. }
  41. });
  42. Object.defineProperty(exports, 'replaceRootDirInPath', {
  43. enumerable: true,
  44. get: function() {
  45. return (_utils || _load_utils()).replaceRootDirInPath;
  46. }
  47. });
  48. var _Defaults;
  49. function _load_Defaults() {
  50. return (_Defaults = require('./Defaults'));
  51. }
  52. Object.defineProperty(exports, 'defaults', {
  53. enumerable: true,
  54. get: function() {
  55. return _interopRequireDefault(_Defaults || _load_Defaults()).default;
  56. }
  57. });
  58. var _Descriptions;
  59. function _load_Descriptions() {
  60. return (_Descriptions = require('./Descriptions'));
  61. }
  62. Object.defineProperty(exports, 'descriptions', {
  63. enumerable: true,
  64. get: function() {
  65. return _interopRequireDefault(_Descriptions || _load_Descriptions())
  66. .default;
  67. }
  68. });
  69. exports.readConfig = readConfig;
  70. var _path;
  71. function _load_path() {
  72. return (_path = _interopRequireDefault(require('path')));
  73. }
  74. var _normalize3;
  75. function _load_normalize2() {
  76. return (_normalize3 = _interopRequireDefault(require('./normalize')));
  77. }
  78. var _resolveConfigPath;
  79. function _load_resolveConfigPath() {
  80. return (_resolveConfigPath = _interopRequireDefault(
  81. require('./resolveConfigPath')
  82. ));
  83. }
  84. var _readConfigFileAndSetRootDir;
  85. function _load_readConfigFileAndSetRootDir() {
  86. return (_readConfigFileAndSetRootDir = _interopRequireDefault(
  87. require('./readConfigFileAndSetRootDir')
  88. ));
  89. }
  90. function _interopRequireDefault(obj) {
  91. return obj && obj.__esModule ? obj : {default: obj};
  92. }
  93. function readConfig(
  94. argv,
  95. packageRootOrConfig,
  96. // Whether it needs to look into `--config` arg passed to CLI.
  97. // It only used to read initial config. If the initial config contains
  98. // `project` property, we don't want to read `--config` value and rather
  99. skipArgvConfigOption,
  100. parentConfigPath
  101. ) {
  102. let rawOptions;
  103. let configPath = null;
  104. if (typeof packageRootOrConfig !== 'string') {
  105. if (parentConfigPath) {
  106. const parentConfigDirname = (_path || _load_path()).default.dirname(
  107. parentConfigPath
  108. );
  109. rawOptions = packageRootOrConfig;
  110. rawOptions.rootDir = rawOptions.rootDir
  111. ? (0, (_utils || _load_utils()).replaceRootDirInPath)(
  112. parentConfigDirname,
  113. rawOptions.rootDir
  114. )
  115. : parentConfigDirname;
  116. } else {
  117. throw new Error(
  118. 'Jest: Cannot use configuration as an object without a file path.'
  119. );
  120. }
  121. } else if ((0, (_utils || _load_utils()).isJSONString)(argv.config)) {
  122. // A JSON string was passed to `--config` argument and we can parse it
  123. // and use as is.
  124. let config;
  125. try {
  126. config = JSON.parse(argv.config);
  127. } catch (e) {
  128. throw new Error(
  129. 'There was an error while parsing the `--config` argument as a JSON string.'
  130. );
  131. }
  132. // NOTE: we might need to resolve this dir to an absolute path in the future
  133. config.rootDir = config.rootDir || packageRootOrConfig;
  134. rawOptions = config;
  135. // A string passed to `--config`, which is either a direct path to the config
  136. // or a path to directory containing `package.json` or `jest.config.js`
  137. } else if (!skipArgvConfigOption && typeof argv.config == 'string') {
  138. configPath = (0, (_resolveConfigPath || _load_resolveConfigPath()).default)(
  139. argv.config,
  140. process.cwd()
  141. );
  142. rawOptions = (0,
  143. (_readConfigFileAndSetRootDir || _load_readConfigFileAndSetRootDir())
  144. .default)(configPath);
  145. } else {
  146. // Otherwise just try to find config in the current rootDir.
  147. configPath = (0, (_resolveConfigPath || _load_resolveConfigPath()).default)(
  148. packageRootOrConfig,
  149. process.cwd()
  150. );
  151. rawOptions = (0,
  152. (_readConfigFileAndSetRootDir || _load_readConfigFileAndSetRootDir())
  153. .default)(configPath);
  154. }
  155. var _normalize = (0, (_normalize3 || _load_normalize2()).default)(
  156. rawOptions,
  157. argv
  158. );
  159. const options = _normalize.options,
  160. hasDeprecationWarnings = _normalize.hasDeprecationWarnings;
  161. var _getConfigs = getConfigs(options);
  162. const globalConfig = _getConfigs.globalConfig,
  163. projectConfig = _getConfigs.projectConfig;
  164. return {
  165. configPath: configPath,
  166. globalConfig: globalConfig,
  167. hasDeprecationWarnings: hasDeprecationWarnings,
  168. projectConfig: projectConfig
  169. };
  170. }
  171. const getConfigs = options => ({
  172. globalConfig: Object.freeze({
  173. bail: options.bail,
  174. changedFilesWithAncestor: options.changedFilesWithAncestor,
  175. changedSince: options.changedSince,
  176. collectCoverage: options.collectCoverage,
  177. collectCoverageFrom: options.collectCoverageFrom,
  178. collectCoverageOnlyFrom: options.collectCoverageOnlyFrom,
  179. coverageDirectory: options.coverageDirectory,
  180. coverageReporters: options.coverageReporters,
  181. coverageThreshold: options.coverageThreshold,
  182. detectLeaks: options.detectLeaks,
  183. detectOpenHandles: options.detectOpenHandles,
  184. enabledTestsMap: options.enabledTestsMap,
  185. errorOnDeprecated: options.errorOnDeprecated,
  186. expand: options.expand,
  187. filter: options.filter,
  188. findRelatedTests: options.findRelatedTests,
  189. forceExit: options.forceExit,
  190. globalSetup: options.globalSetup,
  191. globalTeardown: options.globalTeardown,
  192. json: options.json,
  193. lastCommit: options.lastCommit,
  194. listTests: options.listTests,
  195. logHeapUsage: options.logHeapUsage,
  196. maxWorkers: options.maxWorkers,
  197. noSCM: undefined,
  198. noStackTrace: options.noStackTrace,
  199. nonFlagArgs: options.nonFlagArgs,
  200. notify: options.notify,
  201. notifyMode: options.notifyMode,
  202. onlyChanged: options.onlyChanged,
  203. onlyFailures: options.onlyFailures,
  204. outputFile: options.outputFile,
  205. passWithNoTests: options.passWithNoTests,
  206. projects: options.projects,
  207. replname: options.replname,
  208. reporters: options.reporters,
  209. rootDir: options.rootDir,
  210. runTestsByPath: options.runTestsByPath,
  211. silent: options.silent,
  212. skipFilter: options.skipFilter,
  213. testFailureExitCode: options.testFailureExitCode,
  214. testNamePattern: options.testNamePattern,
  215. testPathPattern: options.testPathPattern,
  216. testResultsProcessor: options.testResultsProcessor,
  217. updateSnapshot: options.updateSnapshot,
  218. useStderr: options.useStderr,
  219. verbose: options.verbose,
  220. watch: options.watch,
  221. watchAll: options.watchAll,
  222. watchPlugins: options.watchPlugins,
  223. watchman: options.watchman
  224. }),
  225. projectConfig: Object.freeze({
  226. automock: options.automock,
  227. browser: options.browser,
  228. cache: options.cache,
  229. cacheDirectory: options.cacheDirectory,
  230. clearMocks: options.clearMocks,
  231. coveragePathIgnorePatterns: options.coveragePathIgnorePatterns,
  232. cwd: options.cwd,
  233. detectLeaks: options.detectLeaks,
  234. detectOpenHandles: options.detectOpenHandles,
  235. displayName: options.displayName,
  236. errorOnDeprecated: options.errorOnDeprecated,
  237. filter: options.filter,
  238. forceCoverageMatch: options.forceCoverageMatch,
  239. globals: options.globals,
  240. haste: options.haste,
  241. moduleDirectories: options.moduleDirectories,
  242. moduleFileExtensions: options.moduleFileExtensions,
  243. moduleLoader: options.moduleLoader,
  244. moduleNameMapper: options.moduleNameMapper,
  245. modulePathIgnorePatterns: options.modulePathIgnorePatterns,
  246. modulePaths: options.modulePaths,
  247. name: options.name,
  248. prettierPath: options.prettierPath,
  249. resetMocks: options.resetMocks,
  250. resetModules: options.resetModules,
  251. resolver: options.resolver,
  252. restoreMocks: options.restoreMocks,
  253. rootDir: options.rootDir,
  254. roots: options.roots,
  255. runner: options.runner,
  256. setupFiles: options.setupFiles,
  257. setupTestFrameworkScriptFile: options.setupTestFrameworkScriptFile,
  258. skipFilter: options.skipFilter,
  259. skipNodeResolution: options.skipNodeResolution,
  260. snapshotSerializers: options.snapshotSerializers,
  261. testEnvironment: options.testEnvironment,
  262. testEnvironmentOptions: options.testEnvironmentOptions,
  263. testLocationInResults: options.testLocationInResults,
  264. testMatch: options.testMatch,
  265. testPathIgnorePatterns: options.testPathIgnorePatterns,
  266. testRegex: options.testRegex,
  267. testRunner: options.testRunner,
  268. testURL: options.testURL,
  269. timers: options.timers,
  270. transform: options.transform,
  271. transformIgnorePatterns: options.transformIgnorePatterns,
  272. unmockedModulePathPatterns: options.unmockedModulePathPatterns,
  273. watchPathIgnorePatterns: options.watchPathIgnorePatterns
  274. })
  275. });