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

Show warning in cli in case of multiple configs #10213

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions e2e/__tests__/__snapshots__/console.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,57 @@ Snapshots: 0 total
Time: <<REPLACED>>
`;

exports[`does not warn of multiple configs when --config is passed 1`] = `
console.log
This is a log message.

at Object.log (__tests__/console.test.js:10:11)

console.info
This is an info message.

at Object.info (__tests__/console.test.js:12:11)

console.warn
This is a warning message.

12 | console.info('This is an info message.');
13 |
> 14 | console.warn('This is a warning message.');
| ^
15 |
16 | console.error('This is an error message.');
17 | });

at Object.warn (__tests__/console.test.js:14:11)

console.error
This is an error message.

14 | console.warn('This is a warning message.');
15 |
> 16 | console.error('This is an error message.');
| ^
17 | });
18 |

at Object.error (__tests__/console.test.js:16:11)

`;

exports[`does not warn of multiple configs when --config is passed 2`] = `
PASS __tests__/console.test.js
✓ works just fine
`;

exports[`does not warn of multiple configs when --config is passed 3`] = `
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites.
`;

exports[`respects --noStackTrace 1`] = `
console.log
This is a log message.
Expand Down Expand Up @@ -231,3 +282,53 @@ Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites.
`;

exports[`warns of multiple configs 1`] = ``;

exports[`warns of multiple configs 2`] = `
PASS __tests__/console.test.js
● Console

console.log
This is a log message.

at Object.log (__tests__/console.test.js:10:11)

console.info
This is an info message.

at Object.info (__tests__/console.test.js:12:11)

console.warn
This is a warning message.

12 | console.info('This is an info message.');
13 |
> 14 | console.warn('This is a warning message.');
| ^
15 |
16 | console.error('This is an error message.');
17 | });

at Object.warn (__tests__/console.test.js:14:11)

console.error
This is an error message.

14 | console.warn('This is a warning message.');
15 |
> 16 | console.error('This is an error message.');
| ^
17 | });
18 |

at Object.error (__tests__/console.test.js:16:11)
`;

exports[`warns of multiple configs 3`] = `
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites.
`;
26 changes: 26 additions & 0 deletions e2e/__tests__/console.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,29 @@ test('does not error out when using winston', () => {
expect(wrap(rest)).toMatchSnapshot();
expect(wrap(summary)).toMatchSnapshot();
});

test('warns of multiple configs', () => {
const {stderr, stdout, exitCode} = runJest('console');
const {summary, rest} = extractSummary(stderr);
console.log('in last test');
console.log(summary);
console.log(rest);
expect(exitCode).toBe(0);
expect(wrap(stdout)).toMatchSnapshot();
expect(wrap(rest)).toMatchSnapshot();
expect(wrap(summary)).toMatchSnapshot();
});

test('does not warn of multiple configs when --config is passed', () => {
const {stderr, stdout, exitCode} = runJest('console', [
'--config=' + JSON.stringify({testEnvironment: 'node'}),
]);
const {summary, rest} = extractSummary(stderr);
console.log('in last with --config test');
console.log(summary);
console.log(rest);
expect(exitCode).toBe(0);
expect(wrap(stdout)).toMatchSnapshot();
expect(wrap(rest)).toMatchSnapshot();
expect(wrap(summary)).toMatchSnapshot();
});
11 changes: 11 additions & 0 deletions e2e/jest-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

/* -------------------------------------------------------------------------- */
/* Empty file just for testing purposes. */
/* -------------------------------------------------------------------------- */
1 change: 1 addition & 0 deletions packages/jest-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export {getTestEnvironment, isJSONString} from './utils';
export {default as normalize} from './normalize';
export {default as deprecationEntries} from './Deprecated';
export {replaceRootDirInPath} from './utils';
export {checkMultipleConfigs} from './resolveConfigPath';
export {default as defaults} from './Defaults';
export {default as descriptions} from './Descriptions';
import * as constants from './constants';
Expand Down
24 changes: 24 additions & 0 deletions packages/jest-config/src/resolveConfigPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,30 @@ const resolveConfigPathByTraversing = (
);
};

export const checkMultipleConfigs = (pathToResolve: Config.Path): boolean => {
const jestConfig = JEST_CONFIG_EXT_ORDER.map(ext =>
path.resolve(pathToResolve, getConfigFilename(ext)),
).find(isFile);

if (!jestConfig) {
return false;
}

const packageJson = path.resolve(pathToResolve, PACKAGE_JSON);

if (!isFile(packageJson)) {
return false;
}

const configObject = require(packageJson);

if (!configObject.jest) {
return false;
}

return true;
};

const makeResolutionErrorMessage = (
initialPath: Config.Path,
cwd: Config.Path,
Expand Down
23 changes: 22 additions & 1 deletion packages/jest-core/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {Config} from '@jest/types';
import type {AggregatedResult} from '@jest/test-result';
import {CustomConsole} from '@jest/console';
import {createDirectory, preRunMessage} from 'jest-util';
import {readConfigs} from 'jest-config';
import {checkMultipleConfigs, readConfigs} from 'jest-config';
import Runtime = require('jest-runtime');
import type {ChangedFilesPromise} from 'jest-changed-files';
import HasteMap = require('jest-haste-map');
Expand Down Expand Up @@ -48,6 +48,27 @@ export async function runCLI(
const outputStream =
argv.json || argv.useStderr ? process.stderr : process.stdout;

if (!argv.config) {
projects.forEach(project => {
const hasMultipleConfigs = checkMultipleConfigs(project);
if (hasMultipleConfigs) {
console.warn(
chalk.yellow(
chalk.bold(`Multiple configurations found:

Jest will use 'jest.config.js' for configuration, but Jest also
found a configuration in 'package.json'. Delete the 'jest' key
in that file to silence this warning, or delete the 'jest.config.js' file
to use the configuration from 'package.json'.

Configuration Documentation:
https://jestjs.io/docs/en/configuration.html`),
),
);
}
});
}

const {globalConfig, configs, hasDeprecationWarnings} = await readConfigs(
argv,
projects,
Expand Down