Skip to content

Commit

Permalink
Merge 55e2af5 into 25394c8
Browse files Browse the repository at this point in the history
  • Loading branch information
stephtr authored Apr 28, 2020
2 parents 25394c8 + 55e2af5 commit 0554968
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Please add your own contribution below inside the Master section
Bug-fixes within the same version aren't needed
## Master
* improve create-react-app detection logic - stephtr
-->

### 3.2.0
Expand Down
17 changes: 16 additions & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,22 @@ export function getTestCommand(rootPath: string): string | undefined | null {
* Checks if the supplied test command could have been generated by create-react-app
*/
export function isCreateReactAppTestCommand(testCommand: string): boolean {
return testCommand && createReactAppBinaryNames.some(binary => testCommand.indexOf(binary + ' test') === 0)
if (!testCommand) {
return false
}
// Sometimes the `create-react` command is prepended with `cross-env`
// setting an environment variable (most often `CI=true`)
// in that case, let's fetch the real command name
if (testCommand.startsWith('cross-env ')) {
// do a quick & dirty argument parsing, for simplicity not caring about quotes
const args = testCommand.split(' ')
// look for the first argument (beside the first `"cross-env"`) not containing a equal sign...
const exeIndex = args.findIndex((argument, index) => index > 0 && !argument.includes('='))
// ...and remove everything in front of that
args.splice(0, exeIndex)
testCommand = args.join(' ')
}
return createReactAppBinaryNames.some(binary => testCommand.startsWith(binary + ' test'))
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,18 @@ describe('ModuleHelpers', () => {
expect(isCreateReactAppTestCommand('react-scripts test --env=jsdom')).toBe(true)
})

it('should return true for CRA with cross-env', () => {
expect(isCreateReactAppTestCommand('cross-env CI=true react-scripts test --env=jsdom')).toBe(true)
})

it('should return false for other scripts', () => {
expect(isCreateReactAppTestCommand(undefined)).toBe(false)
expect(isCreateReactAppTestCommand('custom-script')).toBe(false)
})

it('should return false for other scripts with cross-env', () => {
expect(isCreateReactAppTestCommand('cross-env CI=true custom-script')).toBe(false)
})
})

describe('pathToJest', () => {
Expand Down

0 comments on commit 0554968

Please sign in to comment.