Skip to content

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
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand All @@ -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
Copy link
Owner

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)

--standaloneMode Use explicit require calls instead of globals
Copy link
Owner

Choose a reason for hiding this comment

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

Use explicit require calls instead of globals -> Use explicit require calls to Jest instead of globals (not recommended unless you run the tests in a browser)

--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.
Expand Down
99 changes: 68 additions & 31 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: ['_'],
Expand All @@ -38,6 +44,21 @@ Options:
type: 'boolean',
alias: 'd',
},
parser: {
type: 'string',
},
transformer: {
type: 'string',
},
skipImportDetection: {
type: 'boolean',
},
standaloneMode: {
type: 'boolean',
},
mochaAssertion: {
type: 'string',
},
},
}
)
Expand Down Expand Up @@ -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(
Expand All @@ -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,
},
Expand All @@ -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`,
Expand All @@ -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)',
Expand All @@ -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',
Expand All @@ -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')
}
Expand Down