Skip to content

Commit

Permalink
bring test back, and simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Nov 27, 2023
1 parent 00167af commit 886420c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
70 changes: 69 additions & 1 deletion packages/jest-config/src/__tests__/resolveConfigPath.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,74 @@ describe.each(JEST_CONFIG_EXT_ORDER.slice(0))(
);
});

test(`directory path with "${extension}"`, () => {
const relativePackageJsonPath = 'a/b/c/package.json';
const absolutePackageJsonPath = path.resolve(
DIR,
relativePackageJsonPath,
);
const relativeJestConfigPath = `a/b/c/jest.config${extension}`;
const absoluteJestConfigPath = path.resolve(DIR, relativeJestConfigPath);

// no configs yet. should throw
writeFiles(DIR, {[`a/b/c/some_random_file${extension}`]: ''});

expect(() =>
// absolute
resolveConfigPath(path.dirname(absoluteJestConfigPath), DIR),
).toThrow(ERROR_PATTERN);

expect(() =>
// relative
resolveConfigPath(path.dirname(relativeJestConfigPath), DIR),
).toThrow(ERROR_PATTERN);

writeFiles(DIR, {[relativePackageJsonPath]: ''});

// absolute
expect(
resolveConfigPath(path.dirname(absolutePackageJsonPath), DIR),
).toBe(absolutePackageJsonPath);

// relative
expect(
resolveConfigPath(path.dirname(relativePackageJsonPath), DIR),
).toBe(absolutePackageJsonPath);

// jest.config.js takes precedence
writeFiles(DIR, {[relativeJestConfigPath]: ''});

// absolute
expect(
resolveConfigPath(path.dirname(absolutePackageJsonPath), DIR),
).toBe(absoluteJestConfigPath);

// relative
expect(
resolveConfigPath(path.dirname(relativePackageJsonPath), DIR),
).toBe(absoluteJestConfigPath);

// jest.config.js and package.json with 'jest' cannot be used together
writeFiles(DIR, {[relativePackageJsonPath]: JSON.stringify({jest: {}})});

// absolute
expect(() =>
resolveConfigPath(path.dirname(absolutePackageJsonPath), DIR),
).toThrow(MULTIPLE_CONFIGS_ERROR_PATTERN);

// relative
expect(() =>
resolveConfigPath(path.dirname(relativePackageJsonPath), DIR),
).toThrow(MULTIPLE_CONFIGS_ERROR_PATTERN);

expect(() => {
resolveConfigPath(
path.join(path.dirname(relativePackageJsonPath), 'j/x/b/m/'),
DIR,
);
}).toThrow(NO_ROOT_DIR_ERROR_PATTERN);
});

test('file path from "jest" key', () => {
const anyFileName = `anyJestConfigfile${extension}`;
const relativePackageJsonPath = 'a/b/c/package.json';
Expand Down Expand Up @@ -107,7 +175,7 @@ describe.each(JEST_CONFIG_EXT_ORDER.slice(0))(
expect(() =>
resolveConfigPath(path.dirname(absolutePackageJsonPath), DIR),
).toThrow(
'Jest expects the string configuration to point to a file, but it does not.',
/Jest expects the string configuration to point to a file, but .* not\./,
);
});
},
Expand Down
9 changes: 4 additions & 5 deletions packages/jest-config/src/resolveConfigPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,19 @@ const resolveConfigPathByTraversing = (
const packageJson = findPackageJson(pathToResolve);

if (packageJson) {
const packagePath = path.dirname(packageJson);
const jestKey = getPackageJsonJestKey(packageJson);

if (jestKey) {
if (typeof jestKey === 'string') {
const absolutePath = path.isAbsolute(jestKey)
? jestKey
: path.resolve(packagePath, jestKey);
: path.resolve(pathToResolve, jestKey);

if (!isFile(absolutePath)) {
throw new ValidationError(
`${BULLET}Validation Error`,
` Configuration in ${chalk.bold(packageJson)} is not valid. ` +
'Jest expects the string configuration to point to a file, but it does not. ' +
`Jest expects the string configuration to point to a file, but ${absolutePath} is not. ` +
`Please check your Jest configuration in ${chalk.bold(
packageJson,
)}.`,
Expand All @@ -109,8 +108,8 @@ const resolveConfigPathByTraversing = (
throw new ValidationError(...makeMultipleConfigsErrorMessage(configFiles));
}

if (configFiles.length > 0) {
return configFiles[0];
if (configFiles.length > 0 || packageJson) {
return configFiles[0] ?? packageJson;
}

// This is the system root.
Expand Down

0 comments on commit 886420c

Please sign in to comment.