index.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. 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. exports.readConfig = readConfig;
  43. var _path;
  44. function _load_path() {
  45. return _path = _interopRequireDefault(require('path'));
  46. }
  47. var _normalize3;
  48. function _load_normalize2() {
  49. return _normalize3 = _interopRequireDefault(require('./normalize'));
  50. }
  51. var _resolve_config_path;
  52. function _load_resolve_config_path() {
  53. return _resolve_config_path = _interopRequireDefault(require('./resolve_config_path'));
  54. }
  55. var _read_config_file_and_set_root_dir;
  56. function _load_read_config_file_and_set_root_dir() {
  57. return _read_config_file_and_set_root_dir = _interopRequireDefault(require('./read_config_file_and_set_root_dir'));
  58. }
  59. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  60. function readConfig(argv, packageRootOrConfig,
  61. // Whether it needs to look into `--config` arg passed to CLI.
  62. // It only used to read initial config. If the initial config contains
  63. // `project` property, we don't want to read `--config` value and rather
  64. skipArgvConfigOption, parentConfigPath) {
  65. let rawOptions;
  66. let configPath = null;
  67. if (typeof packageRootOrConfig !== 'string') {
  68. if (parentConfigPath) {
  69. rawOptions = packageRootOrConfig;
  70. rawOptions.rootDir = (_path || _load_path()).default.dirname(parentConfigPath);
  71. } else {
  72. throw new Error('Jest: Cannot use configuration as an object without a file path.');
  73. }
  74. } else if ((0, (_utils || _load_utils()).isJSONString)(argv.config)) {
  75. // A JSON string was passed to `--config` argument and we can parse it
  76. // and use as is.
  77. let config;
  78. try {
  79. config = JSON.parse(argv.config);
  80. } catch (e) {
  81. throw new Error('There was an error while parsing the `--config` argument as a JSON string.');
  82. }
  83. // NOTE: we might need to resolve this dir to an absolute path in the future
  84. config.rootDir = config.rootDir || packageRootOrConfig;
  85. rawOptions = config;
  86. // A string passed to `--config`, which is either a direct path to the config
  87. // or a path to directory containing `package.json` or `jest.conf.js`
  88. } else if (!skipArgvConfigOption && typeof argv.config == 'string') {
  89. configPath = (0, (_resolve_config_path || _load_resolve_config_path()).default)(argv.config, process.cwd());
  90. rawOptions = (0, (_read_config_file_and_set_root_dir || _load_read_config_file_and_set_root_dir()).default)(configPath);
  91. } else {
  92. // Otherwise just try to find config in the current rootDir.
  93. configPath = (0, (_resolve_config_path || _load_resolve_config_path()).default)(packageRootOrConfig, process.cwd());
  94. rawOptions = (0, (_read_config_file_and_set_root_dir || _load_read_config_file_and_set_root_dir()).default)(configPath);
  95. }
  96. var _normalize = (0, (_normalize3 || _load_normalize2()).default)(rawOptions, argv);
  97. const options = _normalize.options,
  98. hasDeprecationWarnings = _normalize.hasDeprecationWarnings;
  99. var _getConfigs = getConfigs(options);
  100. const globalConfig = _getConfigs.globalConfig,
  101. projectConfig = _getConfigs.projectConfig;
  102. return {
  103. configPath,
  104. globalConfig,
  105. hasDeprecationWarnings,
  106. projectConfig
  107. };
  108. }
  109. const getConfigs = options => {
  110. return {
  111. globalConfig: Object.freeze({
  112. bail: options.bail,
  113. changedFilesWithAncestor: options.changedFilesWithAncestor,
  114. changedSince: options.changedSince,
  115. collectCoverage: options.collectCoverage,
  116. collectCoverageFrom: options.collectCoverageFrom,
  117. collectCoverageOnlyFrom: options.collectCoverageOnlyFrom,
  118. coverageDirectory: options.coverageDirectory,
  119. coverageReporters: options.coverageReporters,
  120. coverageThreshold: options.coverageThreshold,
  121. detectLeaks: options.detectLeaks,
  122. enabledTestsMap: options.enabledTestsMap,
  123. expand: options.expand,
  124. findRelatedTests: options.findRelatedTests,
  125. forceExit: options.forceExit,
  126. globalSetup: options.globalSetup,
  127. globalTeardown: options.globalTeardown,
  128. json: options.json,
  129. lastCommit: options.lastCommit,
  130. listTests: options.listTests,
  131. logHeapUsage: options.logHeapUsage,
  132. maxWorkers: options.maxWorkers,
  133. noSCM: undefined,
  134. noStackTrace: options.noStackTrace,
  135. nonFlagArgs: options.nonFlagArgs,
  136. notify: options.notify,
  137. notifyMode: options.notifyMode,
  138. onlyChanged: options.onlyChanged,
  139. onlyFailures: options.onlyFailures,
  140. outputFile: options.outputFile,
  141. passWithNoTests: options.passWithNoTests,
  142. projects: options.projects,
  143. replname: options.replname,
  144. reporters: options.reporters,
  145. rootDir: options.rootDir,
  146. runTestsByPath: options.runTestsByPath,
  147. silent: options.silent,
  148. testFailureExitCode: options.testFailureExitCode,
  149. testNamePattern: options.testNamePattern,
  150. testPathPattern: options.testPathPattern,
  151. testResultsProcessor: options.testResultsProcessor,
  152. updateSnapshot: options.updateSnapshot,
  153. useStderr: options.useStderr,
  154. verbose: options.verbose,
  155. watch: options.watch,
  156. watchAll: options.watchAll,
  157. watchPlugins: options.watchPlugins,
  158. watchman: options.watchman
  159. }),
  160. projectConfig: Object.freeze({
  161. automock: options.automock,
  162. browser: options.browser,
  163. cache: options.cache,
  164. cacheDirectory: options.cacheDirectory,
  165. clearMocks: options.clearMocks,
  166. coveragePathIgnorePatterns: options.coveragePathIgnorePatterns,
  167. cwd: options.cwd,
  168. detectLeaks: options.detectLeaks,
  169. displayName: options.displayName,
  170. forceCoverageMatch: options.forceCoverageMatch,
  171. globals: options.globals,
  172. haste: options.haste,
  173. moduleDirectories: options.moduleDirectories,
  174. moduleFileExtensions: options.moduleFileExtensions,
  175. moduleLoader: options.moduleLoader,
  176. moduleNameMapper: options.moduleNameMapper,
  177. modulePathIgnorePatterns: options.modulePathIgnorePatterns,
  178. modulePaths: options.modulePaths,
  179. name: options.name,
  180. resetMocks: options.resetMocks,
  181. resetModules: options.resetModules,
  182. resolver: options.resolver,
  183. restoreMocks: options.restoreMocks,
  184. rootDir: options.rootDir,
  185. roots: options.roots,
  186. runner: options.runner,
  187. setupFiles: options.setupFiles,
  188. setupTestFrameworkScriptFile: options.setupTestFrameworkScriptFile,
  189. skipNodeResolution: options.skipNodeResolution,
  190. snapshotSerializers: options.snapshotSerializers,
  191. testEnvironment: options.testEnvironment,
  192. testEnvironmentOptions: options.testEnvironmentOptions,
  193. testLocationInResults: options.testLocationInResults,
  194. testMatch: options.testMatch,
  195. testPathIgnorePatterns: options.testPathIgnorePatterns,
  196. testRegex: options.testRegex,
  197. testRunner: options.testRunner,
  198. testURL: options.testURL,
  199. timers: options.timers,
  200. transform: options.transform,
  201. transformIgnorePatterns: options.transformIgnorePatterns,
  202. unmockedModulePathPatterns: options.unmockedModulePathPatterns,
  203. watchPathIgnorePatterns: options.watchPathIgnorePatterns
  204. })
  205. };
  206. };