diff --git a/src/helpers.ts b/src/helpers.ts index aa75f4af..b04e8999 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -66,13 +66,22 @@ export function isCreateReactAppTestCommand(testCommand?: string | null): boolea createReactAppBinaryNames.some((binary) => testCommand.includes(`${binary} test`)) ); } +export function hasReactBinary(rootPath: string): boolean { + return createReactAppBinaryNames.some((binary) => getLocalPathForExecutable(rootPath, binary)); +} function checkPackageTestScript(rootPath: string): string | undefined { const testCommand = getTestCommand(rootPath); if (!testCommand) { return; } - if (isCreateReactAppTestCommand(testCommand) || testCommand.includes('jest')) { + if ( + isCreateReactAppTestCommand(testCommand) || + testCommand.includes('jest') || + // for react apps, even if we don't recognize the test script pattern, still better to use the test script + // than running the binary with hard coded parameters outselves. + hasReactBinary(rootPath) + ) { const pm = getPM(rootPath) ?? 'npm'; if (pm === 'npm') { return 'npm test --'; @@ -109,11 +118,9 @@ export const getDefaultJestCommand = (rootPath = ''): string | undefined => { return pmScript; } - for (const binary of [...createReactAppBinaryNames, 'jest']) { - const cmd = getLocalPathForExecutable(rootPath, binary); - if (cmd) { - return `"${cmd}"`; - } + const cmd = getLocalPathForExecutable(rootPath, 'jest'); + if (cmd) { + return `"${cmd}"`; } }; diff --git a/tests/helpers.test.ts b/tests/helpers.test.ts index 83d840c6..bc7f7877 100644 --- a/tests/helpers.test.ts +++ b/tests/helpers.test.ts @@ -326,7 +326,7 @@ describe('getDefaultJestCommand', () => { ${1} | ${'react-scripts test'} | ${'yarn.lock'} | ${'react-scripts'} | ${'yarn test'} ${2} | ${'react-scripts test'} | ${'package-lock.json'} | ${'react-scripts'} | ${'npm test --'} ${3} | ${'some other test'} | ${'yarn.lock'} | ${'jest'} | ${'binary'} - ${4} | ${'some other test'} | ${'package-lock.json'} | ${'react-native-scripts'} | ${'binary'} + ${4} | ${'some other test'} | ${'package-lock.json'} | ${'react-native-scripts'} | ${'npm test --'} ${5} | ${'some other test'} | ${'package-lock.json'} | ${undefined} | ${undefined} ${6} | ${'jest'} | ${'package-lock.json'} | ${undefined} | ${'npm test --'} ${7} | ${undefined} | ${'package-lock.json'} | ${'jest'} | ${'binary'}