Skip to content

Commit

Permalink
fix auto config for react projects (#1002)
Browse files Browse the repository at this point in the history
  • Loading branch information
connectdotz committed Feb 6, 2023
1 parent fc1b6f7 commit 6468661
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
19 changes: 13 additions & 6 deletions src/helpers.ts
Expand Up @@ -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 --';
Expand Down Expand Up @@ -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}"`;
}
};

Expand Down
2 changes: 1 addition & 1 deletion tests/helpers.test.ts
Expand Up @@ -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'}
Expand Down

0 comments on commit 6468661

Please sign in to comment.