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

Support dashed args #7497

Merged
merged 5 commits into from
Dec 13, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

### Fixes

- `[jest-cli]` Support dashed args ([#7497](https://github.com/facebook/jest/pull/7497))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this below the breaking changes, preferably at the bottom of this section?

- `[jest-cli]` [**BREAKING**] Do not use `text-summary` coverage reporter by default if other reporters are configured ([#7058](https://github.com/facebook/jest/pull/7058))
- `[jest-mock]` [**BREAKING**] Fix bugs with mock/spy result tracking of recursive functions ([#6381](https://github.com/facebook/jest/pull/6381))
- `[jest-haste-map]` [**BREAKING**] Recover files correctly after haste name collisions are fixed ([#7329](https://github.com/facebook/jest/pull/7329))
Expand Down
15 changes: 15 additions & 0 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ you can use:
npm test -- -u -t="ColorPicker"
```

## Camelcase & dashed args support

Jest supports both camelcase and dashed arg formats. Which means the following examples will have equal result:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Jest supports both camelcase and dashed arg formats. Which means the following examples will have equal result:
Jest supports both camelcase and dashed arg formats. The following examples will have equal result:


```bash
jest --collect-coverage
jest --collectCoverage
```

They can also be mixed:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
They can also be mixed:
Arguments can also be mixed:


```bash
jest --update-snapshot --detectOpenHandles
```

## Options

_Note: CLI options take precedence over values from the [Configuration](Configuration.md)._
Expand Down
63 changes: 63 additions & 0 deletions e2e/__tests__/supports-dashed-args.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Copyright (c) 2018-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';

import path from 'path';
import runJest from '../runJest';

const consoleDir = path.resolve(__dirname, '../console');
const eachDir = path.resolve(__dirname, '../each');

expect.addSnapshotSerializer({
print: value => value,
test: received => typeof received === 'string',
});

test('works with passing tests', () => {
const result = runJest(eachDir, [
'success.test.js',
'--runInBand',
'--collect-coverage',
'--coverageReporters',
'text-summary',
'--clear-mocks',
'--useStderr',
]);
if (result.status !== 0) {
console.error(result.stderr);
}
expect(result.status).toBe(0);
});

test('throws error for unknown dashed & camelcase args', () => {
const result = runJest(consoleDir, [
'success.test.js',
'--runInBand',
'--collect-coverage',
'--coverageReporters',
'text-summary',
'--clear-mocks',
'--doesNotExist',
'--also-does-not-exist',
'--useStderr',
]);
expect(result.stderr).toMatchInlineSnapshot(`
● Unrecognized CLI Parameters:

Following options were not recognized:
["doesNotExist", "also-does-not-exist"]

CLI Options Documentation:
https://jestjs.io/docs/en/cli.html


`);
expect(result.status).toBe(1);
});
6 changes: 5 additions & 1 deletion packages/jest-cli/src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ const readResultsAndExit = (
};

const buildArgv = (maybeArgv: ?Argv, project: ?Path) => {
const argv: Argv = yargs(maybeArgv || process.argv.slice(2))
const rawArgv: Argv | string[] = maybeArgv || process.argv.slice(2);
const argv: Argv = yargs(rawArgv)
.usage(args.usage)
.alias('help', 'h')
.options(args.options)
Expand All @@ -185,6 +186,9 @@ const buildArgv = (maybeArgv: ?Argv, project: ?Path) => {
validateCLIOptions(
argv,
Object.assign({}, args.options, {deprecationEntries}),
Array.isArray(rawArgv)
? rawArgv.map(rawArgv => rawArgv.replace(/^--?/, ''))
: Object.keys(rawArgv),
);

return argv;
Expand Down
19 changes: 15 additions & 4 deletions packages/jest-validate/src/validateCLIOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,27 @@ const logDeprecatedOptions = (
});
};

export default function validateCLIOptions(argv: Argv, options: Object) {
export default function validateCLIOptions(
argv: Argv,
options: Object,
rawArgv: string[] = [],
) {
const yargsSpecialOptions = ['$0', '_', 'help', 'h'];
const deprecationEntries = options.deprecationEntries || {};
const allowedOptions = Object.keys(options).reduce(
(acc, option) => acc.add(option).add(options[option].alias || option),
new Set(yargsSpecialOptions),
);
const unrecognizedOptions = Object.keys(argv).filter(
arg => !allowedOptions.has(arg),
);
const unrecognizedOptions = Object.keys(argv).filter(arg => {
const camelCased = arg.replace(/-([^-])/g, (a, b) => b.toUpperCase());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add camelize dep?

if (
!allowedOptions.has(camelCased) &&
(!rawArgv.length || rawArgv.includes(arg))
) {
return true;
}
return false;
}, []);

if (unrecognizedOptions.length) {
throw createCLIValidationError(unrecognizedOptions, allowedOptions);
Expand Down