-
Notifications
You must be signed in to change notification settings - Fork 80
Add the ability to bypass prompts by passing command line arguments #189
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,9 @@ To use the interactive CLI run | |
|
||
$ npx jest-codemods | ||
|
||
If you do not have `npx` installed, you can install the `jest-codemods` command globally by running `npm install -g jest-codemods`. | ||
If you do not have `npx` installed, you can install the `jest-codemods` command globally by running `npm install -g jest-codemods`. | ||
|
||
Command line arguments can be used to bypass prompts. Invalid values for arguments will be ignored. | ||
|
||
For more options | ||
``` | ||
|
@@ -50,10 +52,16 @@ $ npx jest-codemods --help | |
|
||
Examples: npx jest-codemods src | ||
npx jest-codemods src/**/*.test.js | ||
npx jest-codemods src/**/*.test.js --parser=babel --skipImportDetection=0 --standaloneMode | ||
|
||
Options: | ||
-f, --force Bypass Git safety checks and force codemods to run | ||
-d, --dry Dry run (no changes are made to files) | ||
-f, --force Bypass Git safety checks and force codemods to run | ||
-d, --dry Dry run (no changes are made to files) | ||
--parser The parser to use (babel, flow, ts, tsx) | ||
--transformer The transformer to use (ava, chai-assert, chai-should, expect-js, expect, jasmine-globals, jasmine-this, mocha, should, tape) | ||
--skipImportDetection Keep using the global object for assertions | ||
--standaloneMode Use explicit require calls instead of globals | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
--mochaAssertion Use assertion transformations with Mocha (chai-assert, chai-should, expect-js, expect, should) | ||
``` | ||
|
||
To transform all test files in a directory run `jest-codemods .` in your terminal. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,10 +15,16 @@ Usage: npx jest-codemods <path> [options] | |
|
||
Examples: npx jest-codemods src | ||
npx jest-codemods src/**/*.test.js | ||
npx jest-codemods src/**/*.test.js --parser=babel | ||
|
||
Options: | ||
-f, --force Bypass Git safety checks and force codemods to run | ||
-d, --dry Dry run (no changes are made to files)`, | ||
-f, --force Bypass Git safety checks and force codemods to run | ||
-d, --dry Dry run (no changes are made to files) | ||
--parser The parser to use (babel, flow, ts, tsx) | ||
--transformer The transformer to use (ava, chai-assert, chai-should, expect-js, expect, jasmine-globals, jasmine-this, mocha, should, tape) | ||
--skipImportDetection Keep using the global object for assertions | ||
--standaloneMode Use explicit require calls instead of globals | ||
--mochaAssertion Use assertion transformations with Mocha (chai-assert, chai-should, expect-js, expect, should)`, | ||
{ | ||
boolean: ['force', 'dry'], | ||
string: ['_'], | ||
|
@@ -38,6 +44,21 @@ Options: | |
type: 'boolean', | ||
alias: 'd', | ||
}, | ||
parser: { | ||
type: 'string', | ||
}, | ||
transformer: { | ||
type: 'string', | ||
}, | ||
skipImportDetection: { | ||
type: 'boolean', | ||
}, | ||
standaloneMode: { | ||
type: 'boolean', | ||
}, | ||
mochaAssertion: { | ||
type: 'string', | ||
}, | ||
}, | ||
} | ||
) | ||
|
@@ -140,6 +161,33 @@ const PARSER_INQUIRER_CHOICES: { name: string; value: Parser }[] = [ | |
}, | ||
] | ||
|
||
const MOCHA_ASSERTION_INQUIRER_CHOICES = [ | ||
{ | ||
name: 'Chai: Assert Syntax', | ||
value: TRANSFORMER_CHAI_ASSERT, | ||
}, | ||
{ | ||
name: 'Chai: Should/Expect BDD Syntax', | ||
value: TRANSFORMER_CHAI_SHOULD, | ||
}, | ||
{ | ||
name: 'Expect.js (by Automattic)', | ||
value: TRANSFORMER_EXPECT_JS, | ||
}, | ||
{ | ||
name: '[email protected] (by mjackson)', | ||
value: TRANSFORMER_EXPECT_1, | ||
}, | ||
{ | ||
name: 'Should.js', | ||
value: TRANSFORMER_SHOULD, | ||
}, | ||
{ | ||
name: 'None', | ||
value: null, | ||
}, | ||
] | ||
|
||
function supportFailure(supportedItems) { | ||
console.log(`\nCurrently, jest-codemods only has support for ${supportedItems}.`) | ||
console.log( | ||
|
@@ -152,19 +200,31 @@ function expandFilePathsIfNeeded(filesBeforeExpansion) { | |
return shouldExpandFiles ? globby.sync(filesBeforeExpansion) : filesBeforeExpansion | ||
} | ||
|
||
const isValidParser = PARSER_INQUIRER_CHOICES.map(c => c.value).includes(cli.flags.parser) | ||
|
||
const isValidTransformer = TRANSFORMER_INQUIRER_CHOICES.map(c => c.value).includes( | ||
cli.flags.transformer | ||
) | ||
|
||
const isValidMochaAssertion = MOCHA_ASSERTION_INQUIRER_CHOICES.map(c => c.value).includes( | ||
cli.flags.mochaAssertion | ||
) | ||
|
||
inquirer | ||
.prompt([ | ||
{ | ||
type: 'list', | ||
name: 'parser', | ||
message: 'Which parser do you want to use?', | ||
when: () => !isValidParser, | ||
pageSize: PARSER_INQUIRER_CHOICES.length, | ||
choices: PARSER_INQUIRER_CHOICES, | ||
}, | ||
{ | ||
type: 'list', | ||
name: 'transformer', | ||
message: 'Which test library would you like to migrate from?', | ||
when: () => !isValidTransformer, | ||
pageSize: TRANSFORMER_INQUIRER_CHOICES.length, | ||
choices: TRANSFORMER_INQUIRER_CHOICES, | ||
}, | ||
|
@@ -173,6 +233,7 @@ inquirer | |
type: 'list', | ||
message: | ||
'Are you using the global object for assertions (i.e. without requiring them)', | ||
when: () => cli.flags.skipImportDetection === undefined, | ||
choices: [ | ||
{ | ||
name: `No, I use import/require statements for my current assertion library`, | ||
|
@@ -188,6 +249,7 @@ inquirer | |
name: 'standaloneMode', | ||
type: 'list', | ||
message: 'Will you be using Jest on Node.js as your test runner?', | ||
when: () => cli.flags.standaloneMode === undefined, | ||
choices: [ | ||
{ | ||
name: 'Yes, use the globals provided by Jest (recommended)', | ||
|
@@ -203,33 +265,9 @@ inquirer | |
type: 'list', | ||
name: 'mochaAssertion', | ||
message: 'Would you like to include assertion transformations with Mocha?', | ||
when: ({ transformer }) => TRANSFORMER_MOCHA === transformer, | ||
choices: [ | ||
{ | ||
name: 'Chai: Assert Syntax', | ||
value: TRANSFORMER_CHAI_ASSERT, | ||
}, | ||
{ | ||
name: 'Chai: Should/Expect BDD Syntax', | ||
value: TRANSFORMER_CHAI_SHOULD, | ||
}, | ||
{ | ||
name: 'Expect.js (by Automattic)', | ||
value: TRANSFORMER_EXPECT_JS, | ||
}, | ||
{ | ||
name: '[email protected] (by mjackson)', | ||
value: TRANSFORMER_EXPECT_1, | ||
}, | ||
{ | ||
name: 'Should.js', | ||
value: TRANSFORMER_SHOULD, | ||
}, | ||
{ | ||
name: 'None', | ||
value: null, | ||
}, | ||
], | ||
when: ({ transformer }) => | ||
TRANSFORMER_MOCHA === transformer && !isValidMochaAssertion, | ||
choices: MOCHA_ASSERTION_INQUIRER_CHOICES, | ||
}, | ||
{ | ||
type: 'input', | ||
|
@@ -252,8 +290,7 @@ inquirer | |
mochaAssertion, | ||
skipImportDetection, | ||
standaloneMode, | ||
} = answers | ||
|
||
} = Object.assign({}, cli.flags, answers) | ||
if (transformer === 'other') { | ||
return supportFailure('AVA, Chai, Expect.js, [email protected], Mocha, Should.js and Tape') | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Use if your current assertion library is not required, but globally available (might result in some false positive transformations)