Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: warn when there are multiple configs #11922

Merged
merged 41 commits into from Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
b85b26b
chore: throw when there are multiple configs
jankaifer Oct 2, 2021
4dff740
fix: allowed package.json without 'jest' as valid config
jankaifer Oct 2, 2021
227f7d4
test: unit test for multiple configs error in resolver
jankaifer Oct 2, 2021
a678d28
Apply suggestions from code review
jankaifer Oct 4, 2021
0f81fe4
test: fixed integration test
jankaifer Oct 4, 2021
b6ff8b8
tests: fixed config issues in old tests
jankaifer Oct 4, 2021
27fdaff
feat: multiple jest.config.ext files are not allowed
jankaifer Oct 4, 2021
0d15a1f
test: added e2e test for --config override
jankaifer Oct 4, 2021
eccf953
fix: more explicit expression
jankaifer Oct 5, 2021
3d71585
fix: added more real-life package.json example
jankaifer Oct 5, 2021
4cca511
Merge branch 'jankaifer/warn-multiple-configs-found' of github.com:Ja…
jankaifer Oct 5, 2021
752b1f9
test: added snapshot tests for multiple errors
jankaifer Oct 5, 2021
02b8ef2
test: moved snapshots from unit tests to e2e tests
jankaifer Oct 5, 2021
cd7c9c9
test: fixed rootDir replacements in snapshots
jankaifer Oct 5, 2021
9e13ff2
Apply suggestions from code review
jankaifer Oct 5, 2021
f30ab95
added missing import
jankaifer Oct 5, 2021
b931029
Change from CR
jankaifer Oct 5, 2021
f9c515e
improved error message
jankaifer Oct 5, 2021
3b1c5b8
added test for --config error surpresion
jankaifer Oct 5, 2021
02338fd
added more real life package.json
jankaifer Oct 5, 2021
2ce0401
added more assertions for multiple config errors
jankaifer Oct 5, 2021
ac00510
improved formatting of error message
jankaifer Oct 5, 2021
82d3e44
fixed sample jest configs in e2e tests
jankaifer Oct 5, 2021
db8c90c
wip: changed from error to warning
jankaifer Oct 5, 2021
ab05377
fixed unit tests for warning
jankaifer Oct 5, 2021
df0f3a5
updated snapshots
jankaifer Oct 5, 2021
1f17acc
Update CHANGELOG.md
SimenB Oct 6, 2021
2d6efff
Update packages/jest-config/src/resolveConfigPath.ts
jankaifer Oct 6, 2021
3682947
Merge branch 'main' into jankaifer/warn-multiple-configs-found
SimenB Oct 6, 2021
7727e74
add slash import
SimenB Oct 6, 2021
9926edf
no flat in node 10
SimenB Oct 6, 2021
91655ce
rename tests
SimenB Oct 6, 2021
d60356f
slash path in e2e test as well
SimenB Oct 6, 2021
6a52634
skip duplicate warning
SimenB Oct 6, 2021
3505a2c
add copyright header
SimenB Oct 6, 2021
80e52de
make warning yellow
SimenB Oct 6, 2021
8abb1c1
specify key in pkg.json
SimenB Oct 6, 2021
746f353
use wrap
SimenB Oct 6, 2021
cae826c
removed hardcoded filename
jankaifer Oct 6, 2021
1de582b
made skipMultipleConfigWarning optional
jankaifer Oct 6, 2021
b57cbf5
Update packages/jest-config/src/resolveConfigPath.ts
SimenB Oct 6, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions e2e/Utils.ts
Expand Up @@ -176,20 +176,23 @@ interface JestPackageJson extends PackageJson {
}

const DEFAULT_PACKAGE_JSON: JestPackageJson = {
description: 'THIS IS AN AUTOGENERATED FILE AND SHOULD NOT BE ADDED TO GIT',
jest: {
testEnvironment: 'node',
},
};

export const createEmptyPackage = (
directory: Config.Path,
packageJson = DEFAULT_PACKAGE_JSON,
packageJson: PackageJson = DEFAULT_PACKAGE_JSON,
) => {
const packageJsonWithDefaults = {
...packageJson,
description: 'THIS IS AN AUTOGENERATED FILE AND SHOULD NOT BE ADDED TO GIT',
};
fs.mkdirSync(directory, {recursive: true});
fs.writeFileSync(
path.resolve(directory, 'package.json'),
JSON.stringify(packageJson, null, 2),
JSON.stringify(packageJsonWithDefaults, null, 2),
);
};

Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/dependencyClash.test.ts
Expand Up @@ -18,7 +18,7 @@ const hasteImplModulePath = path

beforeEach(() => {
cleanup(tempDir);
createEmptyPackage(tempDir);
createEmptyPackage(tempDir, {});
SimenB marked this conversation as resolved.
Show resolved Hide resolved
});

// This test case is checking that when having both
Expand Down
4 changes: 1 addition & 3 deletions e2e/esm-config/cjs/package.json
@@ -1,3 +1 @@
{
"jest": {}
SimenB marked this conversation as resolved.
Show resolved Hide resolved
}
{}
4 changes: 1 addition & 3 deletions e2e/esm-config/mjs/package.json
@@ -1,3 +1 @@
{
"jest": {}
}
{}
SimenB marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 15 additions & 0 deletions packages/jest-config/src/__tests__/resolveConfigPath.test.ts
Expand Up @@ -14,6 +14,7 @@ import resolveConfigPath from '../resolveConfigPath';
const DIR = path.resolve(tmpdir(), 'resolve_config_path_test');
const ERROR_PATTERN = /Could not find a config file based on provided values/;
const NO_ROOT_DIR_ERROR_PATTERN = /Can't find a root directory/;
const MULTIPLE_CONFIGS_ERROR_PATTERN = /Multiple configurations found/;

beforeEach(() => cleanup(DIR));
afterEach(() => cleanup(DIR));
Expand Down Expand Up @@ -92,6 +93,20 @@ describe.each(JEST_CONFIG_EXT_ORDER.slice(0))(
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),
).toThrowError(MULTIPLE_CONFIGS_ERROR_PATTERN);

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

expect(() => {
resolveConfigPath(
path.join(path.dirname(relativePackageJsonPath), 'j/x/b/m/'),
Expand Down
47 changes: 41 additions & 6 deletions packages/jest-config/src/resolveConfigPath.ts
Expand Up @@ -60,15 +60,17 @@ const resolveConfigPathByTraversing = (
const jestConfig = JEST_CONFIG_EXT_ORDER.map(ext =>
path.resolve(pathToResolve, getConfigFilename(ext)),
).find(isFile);
SimenB marked this conversation as resolved.
Show resolved Hide resolved
if (jestConfig) {
return jestConfig;
}

const packageJson = path.resolve(pathToResolve, PACKAGE_JSON);
if (isFile(packageJson)) {
return packageJson;
const packageJson = findPackageJson(pathToResolve);

// If user has jestConfig and 'jest' in package.json it's probably a mistake.
if (jestConfig && packageJson && hasPackageJsonJestKey(packageJson)) {
throw new Error(makeMultipleConfigsErrorMessage(jestConfig, packageJson));
}

const configPath = jestConfig ?? packageJson;
if (configPath) return configPath;

// This is the system root.
// We tried everything, config is nowhere to be found ¯\_(ツ)_/¯
if (pathToResolve === path.dirname(pathToResolve)) {
Expand All @@ -83,6 +85,25 @@ const resolveConfigPathByTraversing = (
);
};

const findPackageJson = (pathToResolve: Config.Path) => {
const packagePath = path.resolve(pathToResolve, PACKAGE_JSON);
if (isFile(packagePath)) {
return packagePath;
}

return undefined;
};

const hasPackageJsonJestKey = (packagePath: Config.Path) => {
const content = fs.readFileSync(packagePath, 'utf8');
try {
return 'jest' in JSON.parse(content);
} catch {
// If package is not a valid JSON
return false;
}
};

const makeResolutionErrorMessage = (
initialPath: Config.Path,
cwd: Config.Path,
Expand All @@ -95,3 +116,17 @@ const makeResolutionErrorMessage = (
`traverse directory tree up, until it finds one of those files in exact order: ${JEST_CONFIG_EXT_ORDER.map(
ext => `"${getConfigFilename(ext)}"`,
).join(' or ')}.`;

const makeMultipleConfigsErrorMessage = (
jestConfigPath: Config.Path,
packagePath: Config.Path,
) =>
'● Multiple configurations found:\n\n' +
`Jest will use \`${jestConfigPath}\` for configuration, but Jest also\n` +
`found configuration in \`${packagePath}\`. Delete the \`"jest"\` key\n` +
`in that file to silence this warning, or delete the \`${path.basename(
jestConfigPath,
)}\` file\n` +
`to use the configuration from \`${path.basename(packagePath)}\`.\n\n` +
'Configuration Documentation:\n' +
'https://jestjs.io/docs/configuration.html\n';