[jest/cli] stop iterating up at the repo_root and error if no config found (#126853) (#126868)

(cherry picked from commit f2150ebaf5)

Co-authored-by: Spencer <spencer@elastic.co>
This commit is contained in:
Kibana Machine 2022-03-03 20:02:01 -05:00 committed by GitHub
parent 7b93bffa8b
commit e99552e72b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -22,6 +22,7 @@ import { existsSync } from 'fs';
import { run } from 'jest';
import { buildArgv } from 'jest-cli/build/cli';
import { ToolingLog, getTimeReporter } from '@kbn/dev-utils';
import { REPO_ROOT } from '@kbn/utils';
import { map } from 'lodash';
// yarn test:jest src/core/server/saved_objects
@ -68,23 +69,47 @@ export function runJest(configName = 'jest.config.js') {
log.verbose('commonTestFiles:', commonTestFiles);
let configPath;
let devConfigPath;
// sets the working directory to the cwd or the common
// base directory of the provided test files
let wd = testFilesProvided ? commonTestFiles : cwd;
while (true) {
const dev = resolve(wd, devConfigName);
if (existsSync(dev)) {
configPath = dev;
break;
}
devConfigPath = resolve(wd, devConfigName);
configPath = resolve(wd, configName);
const actual = resolve(wd, configName);
if (existsSync(actual)) {
configPath = actual;
break;
}
while (!existsSync(configPath) && !existsSync(devConfigPath)) {
wd = resolve(wd, '..');
devConfigPath = resolve(wd, devConfigName);
configPath = resolve(wd, configName);
if (wd === REPO_ROOT) {
break;
}
const parent = resolve(wd, '..');
if (parent === wd) {
break;
}
wd = parent;
}
if (existsSync(devConfigPath)) {
configPath = devConfigPath;
if (!configPath) {
if (testFilesProvided) {
log.error(
`unable to find a ${configName} file in ${commonTestFiles} or any parent directory up to the root of the repo. This CLI can only run Jest tests which resolve to a single ${configName} file, and that file must exist in a parent directory of all the paths you pass.`
);
} else {
log.error(
`we no longer ship a root config file so you either need to pass a path to a test file, a folder where tests can be found, or a --config argument pointing to one of the many ${configName} files in the repository`
);
}
process.exit(1);
}
log.verbose(`no config provided, found ${configPath}`);