MCPcopy
hub / github.com/jestjs/jest / resolveConfigPathByTraversing

Function resolveConfigPathByTraversing

packages/jest-config/src/resolveConfigPath.ts:67–128  ·  view source on GitHub ↗
(
  pathToResolve: string,
  initialPath: string,
  cwd: string,
  skipMultipleConfigError: boolean,
)

Source from the content-addressed store, hash-verified

65}
66
67const resolveConfigPathByTraversing = (
68 pathToResolve: string,
69 initialPath: string,
70 cwd: string,
71 skipMultipleConfigError: boolean,
72): string => {
73 const configFiles = JEST_CONFIG_EXT_ORDER.map(ext =>
74 path.resolve(pathToResolve, getConfigFilename(ext)),
75 ).filter(isFile);
76
77 const packageJson = findPackageJson(pathToResolve);
78
79 if (packageJson) {
80 const jestKey = getPackageJsonJestKey(packageJson);
81
82 if (jestKey) {
83 if (typeof jestKey === 'string') {
84 const absolutePath = path.isAbsolute(jestKey)
85 ? jestKey
86 : path.resolve(pathToResolve, jestKey);
87
88 if (!isFile(absolutePath)) {
89 throw new ValidationError(
90 `${BULLET}Validation Error`,
91 ` Configuration in ${chalk.bold(packageJson)} is not valid. ` +
92 `Jest expects the string configuration to point to a file, but ${absolutePath} is not. ` +
93 `Please check your Jest configuration in ${chalk.bold(
94 packageJson,
95 )}.`,
96 DOCUMENTATION_NOTE,
97 );
98 }
99
100 configFiles.push(absolutePath);
101 } else {
102 configFiles.push(packageJson);
103 }
104 }
105 }
106
107 if (!skipMultipleConfigError && configFiles.length > 1) {
108 throw new ValidationError(...makeMultipleConfigsErrorMessage(configFiles));
109 }
110
111 if (configFiles.length > 0 || packageJson) {
112 return configFiles[0] ?? packageJson;
113 }
114
115 // This is the system root.
116 // We tried everything, config is nowhere to be found ¯\_(ツ)_/¯
117 if (pathToResolve === path.dirname(pathToResolve)) {
118 throw new Error(makeResolutionErrorMessage(initialPath, cwd));
119 }
120
121 // go up a level and try it again
122 return resolveConfigPathByTraversing(
123 path.dirname(pathToResolve),
124 initialPath,

Callers 1

resolveConfigPathFunction · 0.85

Calls 7

findPackageJsonFunction · 0.85
getPackageJsonJestKeyFunction · 0.85
getConfigFilenameFunction · 0.70
isFileFunction · 0.70
resolveMethod · 0.45

Tested by

no test coverage detected