Skip to content

Commit

Permalink
Print deprecation warnings on CLI flags (#5536)
Browse files Browse the repository at this point in the history
* Moving validate_cli_options from jest-util to jest-validate

* Warn on Deprecated Options in CLI args

* Added an integration test to test cli deprecations
  • Loading branch information
Aftabnack authored and cpojer committed Feb 16, 2018
1 parent 966aab6 commit ba8a2d5
Show file tree
Hide file tree
Showing 19 changed files with 94 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
([#5177](https://github.com/facebook/jest/pull/5177))
* `[babel-jest]` Add option to return sourcemap from the transformer separately
from source. ([#5177](https://github.com/facebook/jest/pull/5177))
* `[jest-validate]` Add ability to log deprecation warnings for CLI flags.
([#5536](https://github.com/facebook/jest/pull/5536))

## jest 22.3.0

Expand Down
29 changes: 29 additions & 0 deletions integration-tests/__tests__/deprecated_cli_options.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. 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.
*
* @flow
*/

'use strict';

const path = require('path');
const runJest = require('../runJest');

const dir = path.resolve(__dirname, '../deprecated-cli-options');

it('Prints deprecation warnings for CLI flags', () => {
const {stderr, status} = runJest(dir, ['--mapCoverage']);
expect(status).toBe(0);
expect(stderr).toMatch(/Test Suites: 1 passed, 1 total/);
expect(stderr).toMatch(`● Deprecation Warning:
Option "mapCoverage" has been removed, as it's no longer necessary.
Please update your configuration.
CLI Options Documentation:
https://facebook.github.io/jest/docs/en/cli.html`);
});
3 changes: 3 additions & 0 deletions integration-tests/deprecated-cli-options/__tests__/dummy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test('Dummy', () => {
expect(2).toBe(2);
});
3 changes: 3 additions & 0 deletions integration-tests/deprecated-cli-options/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"jest": {}
}

This file was deleted.

1 change: 1 addition & 0 deletions packages/jest-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"jest-runtime": "^22.3.0",
"jest-snapshot": "^22.2.0",
"jest-util": "^22.3.0",
"jest-validate": "^22.2.2",
"jest-worker": "^22.2.2",
"micromatch": "^2.3.11",
"node-notifier": "^5.2.1",
Expand Down
8 changes: 2 additions & 6 deletions packages/jest-cli/src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ import type {AggregatedResult} from 'types/TestResult';
import type {Argv} from 'types/Argv';
import type {GlobalConfig, Path, ProjectConfig} from 'types/Config';

import {
Console,
clearLine,
createDirectory,
validateCLIOptions,
} from 'jest-util';
import {Console, clearLine, createDirectory} from 'jest-util';
import {validateCLIOptions} from 'jest-validate';
import {readConfig} from 'jest-config';
import {version as VERSION} from '../../package.json';
import * as args from './args';
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import readConfigFileAndSetRootDir from './read_config_file_and_set_root_dir';

export {getTestEnvironment, isJSONString} from './utils';
export {default as normalize} from './normalize';
export {default as deprecationEntries} from './deprecated';

export function readConfig(
argv: Argv,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-repl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"main": "build/index.js",
"dependencies": {
"jest-runtime": "^22.3.0",
"jest-util": "^22.3.0",
"jest-validate": "^22.2.2",
"repl": "^0.1.3",
"yargs": "^10.0.3"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-repl/src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import path from 'path';

import Runtime from 'jest-runtime';
import yargs from 'yargs';
import {validateCLIOptions} from 'jest-util';
import {validateCLIOptions} from 'jest-validate';
import {version as VERSION} from '../../package.json';
import * as args from './args';

Expand Down
1 change: 1 addition & 0 deletions packages/jest-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"jest-regex-util": "^22.1.0",
"jest-resolve": "^22.3.0",
"jest-util": "^22.3.0",
"jest-validate": "^22.2.2",
"json-stable-stringify": "^1.0.1",
"micromatch": "^2.3.11",
"realpath-native": "^1.0.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-runtime/src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import chalk from 'chalk';
import os from 'os';
import path from 'path';
import yargs from 'yargs';
import {Console, setGlobal, validateCLIOptions} from 'jest-util';
import {Console, setGlobal} from 'jest-util';
import {validateCLIOptions} from 'jest-validate';
import {readConfig} from 'jest-config';
// eslint-disable-next-line import/default
import Runtime from '../';
Expand Down
1 change: 0 additions & 1 deletion packages/jest-util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"graceful-fs": "^4.1.11",
"is-ci": "^1.0.10",
"jest-message-util": "^22.2.0",
"jest-validate": "^22.2.2",
"mkdirp": "^0.5.1"
},
"devDependencies": {
Expand Down
2 changes: 0 additions & 2 deletions packages/jest-util/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import installCommonGlobals from './install_common_globals';
import NullConsole from './null_console';
import isInteractive from './is_interative';
import setGlobal from './set_global';
import validateCLIOptions from './validate_cli_options';
import deepCyclicCopy from './deep_cyclic_copy';

const createDirectory = (path: string) => {
Expand All @@ -47,5 +46,4 @@ module.exports = {
installCommonGlobals,
isInteractive,
setGlobal,
validateCLIOptions,
};
1 change: 1 addition & 0 deletions packages/jest-validate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"main": "build/index.js",
"dependencies": {
"chalk": "^2.0.1",
"jest-config": "^22.2.2",
"jest-get-type": "^22.1.0",
"leven": "^2.1.0",
"pretty-format": "^22.1.0"
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-validate/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import {
ValidationError,
} from './utils';
import validate from './validate';
import validateCLIOptions from './validate_cli_options';

module.exports = {
ValidationError,
createDidYouMeanMessage,
format,
logValidationWarning,
validate,
validateCLIOptions,
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@
import type {Argv} from 'types/Argv';

import chalk from 'chalk';
import {ValidationError, format, createDidYouMeanMessage} from 'jest-validate';
import {deprecationEntries} from 'jest-config';
import {createDidYouMeanMessage, format, ValidationError} from './utils';
import {deprecationWarning} from './deprecated';
import defaultConfig from './default_config';

const BULLET: string = chalk.bold('\u25cf');
export const DOCUMENTATION_NOTE = ` ${chalk.bold('CLI Options Documentation:')}
https://facebook.github.io/jest/docs/en/cli.html
`;

const createCLIValidationError = (
unrecognizedOptions: Array<string>,
Expand Down Expand Up @@ -43,6 +49,23 @@ const createCLIValidationError = (
return new ValidationError(title, message, comment);
};

const logDeprecatedOptions = (
deprecatedOptions: Array<string>,
deprecationEntries: Object,
argv: Argv,
) => {
deprecatedOptions.forEach(opt => {
deprecationWarning(
argv,
opt,
deprecationEntries,
Object.assign({}, defaultConfig, {
comment: DOCUMENTATION_NOTE,
}),
);
});
};

export default function validateCLIOptions(argv: Argv, options: Object) {
const yargsSpecialOptions = ['$0', '_', 'help', 'h'];
const allowedOptions = Object.keys(options).reduce(
Expand All @@ -57,5 +80,26 @@ export default function validateCLIOptions(argv: Argv, options: Object) {
throw createCLIValidationError(unrecognizedOptions, allowedOptions);
}

const CLIDeprecations = Object.keys(deprecationEntries).reduce(
(acc, entry) => {
if (options[entry]) {
acc[entry] = deprecationEntries[entry];
if (options[entry].alias) {
acc[options[entry].alias] = deprecationEntries[entry];
}
}
return acc;
},
{},
);
const deprecations = new Set(Object.keys(CLIDeprecations));
const deprecatedOptions = Object.keys(argv).filter(
arg => deprecations.has(arg) && argv[arg] != null,
);

if (deprecatedOptions.length) {
logDeprecatedOptions(deprecatedOptions, CLIDeprecations, argv);
}

return true;
}

0 comments on commit ba8a2d5

Please sign in to comment.