Skip to content

Commit

Permalink
Warn on Deprecated Options in CLI args
Browse files Browse the repository at this point in the history
  • Loading branch information
Aftabnack committed Feb 15, 2018
1 parent a66b88f commit f620a4c
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### Features
* `[docs]` Add MongoDB guide ([#5571](https://github.com/facebook/jest/pull/5571))
* `[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
21 changes: 21 additions & 0 deletions integration-tests/__tests__/deprecated-cli-options.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* 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, stdout, status} = runJest(dir);
expect(status).toBe(0);
expect(stderr).toMatch(/Test Suites: 1 passed, 1 total/);
});
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": {}
}
2 changes: 1 addition & 1 deletion packages/jest-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"jest-runtime": "^22.3.0",
"jest-snapshot": "^22.2.0",
"jest-util": "^22.3.0",
"jest-validate": "^22.3.0",
"jest-validate": "^22.2.2",
"jest-worker": "^22.2.2",
"micromatch": "^2.3.11",
"node-notifier": "^5.2.1",
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-validate": "^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-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"jest-regex-util": "^22.1.0",
"jest-resolve": "^22.3.0",
"jest-util": "^22.3.0",
"jest-validate": "^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
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
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
51 changes: 45 additions & 6 deletions packages/jest-validate/src/validate_cli_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
import type {Argv} from 'types/Argv';

import chalk from 'chalk';
import {
createDidYouMeanMessage,
format,
logValidationWarning,
ValidationError,
} from './utils';
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 @@ -48,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 @@ -62,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),
);

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

return true;
}

0 comments on commit f620a4c

Please sign in to comment.