-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Refactor setting config from argv #3424
Conversation
Codecov Report
@@ Coverage Diff @@
## master #3424 +/- ##
==========================================
+ Coverage 63.8% 64.38% +0.57%
==========================================
Files 179 179
Lines 6648 6620 -28
Branches 5 5
==========================================
+ Hits 4242 4262 +20
+ Misses 2404 2356 -48
Partials 2 2
Continue to review full report at Codecov.
|
I tested this branch, and it doesn't work for me (yes, I ran $ yarn jest-coverage -- --testPathPattern eslint --coverage-reporters html
yarn jest-coverage v0.23.3
$ yarn run jest --silent -- --coverage --testPathPattern eslint --coverage-reporters html
/Users/simen/Development/jest/packages/jest-util/build/validateCLIOptions.js:62
throw createCLIValidationError(unrecognizedOptions, allowedOptions);
^
● Unrecognized CLI Parameters:
Following options were not recognized:
["coverage-reporters", "coverageReporters"]
CLI Options Documentation:
http://facebook.github.io/jest/docs/cli.html
error Command failed with exit code 1.
error Command failed with exit code 1. If I'm misunderstanding the issue this is supposed to fix I can open up a separate issue for it 😄 |
I'm still planning more testing for this tomorrow, forgot to mention that. Right now I just wanted to have feature parity. Thanks for looking into it! |
@thymikee this is super awesome, I didn't think we'd get this done before Jest 20. I really appreciate you taking this on and will give a more thorough review tomorrow. One thing that I like about all of the rewrites we did for Jest 20 is that the entire codebase became a lot more approachable and everything makes much more sense. We now have a clear flow from: DefaultOptions + User specified options + Argv -> InitialOptions -> ProjectConfig & GlobalConfig That's beautiful. |
packages/jest-cli/src/cli/args.js
Outdated
@@ -51,11 +51,12 @@ const docs = 'Documentation: https://facebook.github.io/jest/'; | |||
const options = { | |||
bail: { | |||
alias: 'b', | |||
default: undefined, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the default in yargs if you don't specify this line in this file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For booleans it's false
, otherwise it's undefined
@@ -9,9 +9,18 @@ | |||
*/ | |||
'use strict'; | |||
|
|||
import type {Argv} from 'types/Config'; | |||
type Options = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be an exact type?
@@ -9,9 +9,18 @@ | |||
*/ | |||
'use strict'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that you renamed this file!
packages/jest-config/src/index.js
Outdated
@@ -40,7 +39,7 @@ async function readConfig( | |||
const parseConfig = argv => { | |||
if (argv.config && typeof argv.config === 'string') { | |||
// If the passed in value looks like JSON, treat it as an object. | |||
if (argv.config[0] === '{' && argv.config[argv.config.length - 1] === '}') { | |||
if (argv.config.startsWith('{') && argv.config.endsWith('}')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh! Didn't realize this was part of node 4.
let configFromArgv; | ||
const argvToConfig = Object.keys(argv) | ||
.filter(key => argv[key] !== undefined && specialArgs.indexOf(key) === -1) | ||
.reduce((acc: Object, key) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we call this "options" instead of "acc"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yas!
.reduce((acc: Object, key) => { | ||
switch (key) { | ||
case 'coverage': | ||
acc['collectCoverage'] = argv[key]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer to not use string keys. Did you do this to make flow happy? If so, you could put the keys in the default value that you pass to the reduce call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, pardon moi, I should just use dot notation 👍
types/Config.js
Outdated
@@ -181,3 +183,74 @@ export type ProjectConfig = {| | |||
transformIgnorePatterns: Array<Glob>, | |||
unmockedModulePathPatterns: ?Array<string>, | |||
|}; | |||
|
|||
export type Argv = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mind reordering this file in terms of how it is flowing through Jest? Something like this:
Argv
DefaultOptions
InitialOptions
GlobalConfig
ProjectConfig
I think that makes it a bit more readable. We could also consider having a separate types/Options
file for Argv and DefaultOptions/InitialOptions.
It does work now! Should support kebab in addition to camel cased, though 😄 $ yarn jest-coverage -- --testPathPattern eslint --coverage-reporters html
yarn jest-coverage v0.23.3
$ yarn run jest --silent -- --coverage --testPathPattern eslint --coverage-reporters html
/Users/simen/Development/jest/packages/jest-util/build/validateCLIOptions.js:63
throw createCLIValidationError(unrecognizedOptions, allowedOptions);
^
● Unrecognized CLI Parameter:
Unrecognized option "coverage-reporters". Did you mean "coverageReporters"?
CLI Options Documentation:
http://facebook.github.io/jest/docs/cli.html
error Command failed with exit code 1.
error Command failed with exit code 1. Especially since I got this before:
|
*/ | ||
'use strict'; | ||
|
||
export type Argv = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
such a beautiful type.
packages/jest-config/src/utils.js
Outdated
@@ -59,27 +59,29 @@ const _replaceRootDirInPath = (rootDir: string, filePath: Path): string => { | |||
); | |||
}; | |||
|
|||
const _replaceRootDirInObject = (rootDir: string, config: any): Object => { | |||
if (config instanceof RegExp) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually feel like we may be able to remove this in a follow-up PR. It can't be a regex at this point, can it?
return options; | ||
}, {}); | ||
|
||
if (isJSON(argv.config)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this needs to be moved to the top of this function, it needs to happen earlier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed the order of merging the returned object, --watch
will now take precedence over --config="{"watch": false}"
YES. Great work @thymikee. |
* Rename setState to updateArgv * Add Argv type * Remove unnecessary argv.help check * Refactor setting config from argv * Rename acc to options * Use dot notation instead of string keys * Make Options of updateArgv an exact type * Move Argv type to it's own file * Update showConfig snapshot with 'projects' * Add missing options to args * Update setFromArgv and Argv types * Extract and reuse isJSON * Update updateArgv.js * Update validateCLIOptions.js * Move regex check out of _replaceRootDirInObject * Make explicit flags override those from --config * Rename isJSON -> isJSONString * Add reporters to args
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
This PR aims to refactor how we translate config from CLI.
Notes:
undefined
, so yargs doesn't default them tofalse
(defaults are handled indefaults.js
so I putwatchman
andcache
there)setFromArgv
is now more generic, translating any value (with some exceptions) to new config before normalizing (makes some weird hacks around config/argv unnecessary).setState
withinjest-cli
toupdateArgv
because that's what it does.normalize
are a bit loosened now (they didn't really work fully, which we need to update in a followup)Fixes #3403.
Test plan
jest