From 0924baf19541a03fd37cdb0a2bf13486b397483a Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 12 Apr 2020 17:46:03 +0200 Subject: [PATCH] fix: `--only-changed` should include staged files (#9799) --- CHANGELOG.md | 1 + e2e/__tests__/jestChangedFiles.test.ts | 16 ++++++-- packages/jest-changed-files/src/git.ts | 57 +++++++++++++++----------- 3 files changed, 47 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa2445e8f8b4..0c5588c37208 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Fixes - `[expect]` Restore support for passing functions to `toHaveLength` matcher ([#9796](https://github.com/facebook/jest/pull/9796)) +- `[jest-changed-files]` `--only-changed` should include staged files ([#9799](https://github.com/facebook/jest/pull/9799)) ### Chore & Maintenance diff --git a/e2e/__tests__/jestChangedFiles.test.ts b/e2e/__tests__/jestChangedFiles.test.ts index 66bcb05ff395..5b8e3014cd7b 100644 --- a/e2e/__tests__/jestChangedFiles.test.ts +++ b/e2e/__tests__/jestChangedFiles.test.ts @@ -32,7 +32,7 @@ if (!hgIsInstalled) { console.warn('Mercurial (hg) is not installed - skipping some tests'); } -testIfHg('gets hg SCM roots and dedups them', async () => { +testIfHg('gets hg SCM roots and dedupes them', async () => { writeFiles(DIR, { 'first-repo/file1.txt': 'file1', 'first-repo/nested-dir/file2.txt': 'file2', @@ -67,7 +67,7 @@ testIfHg('gets hg SCM roots and dedups them', async () => { expect(hgRepos[1]).toMatch(/\/jest-changed-files-test-dir\/second-repo\/?$/); }); -test('gets git SCM roots and dedups them', async () => { +test('gets git SCM roots and dedupes them', async () => { writeFiles(DIR, { 'first-repo/file1.txt': 'file1', 'first-repo/nested-dir/file2.txt': 'file2', @@ -101,7 +101,7 @@ test('gets git SCM roots and dedups them', async () => { expect(gitRepos[1]).toMatch(/\/jest-changed-files-test-dir\/second-repo\/?$/); }); -testIfHg('gets mixed git and hg SCM roots and dedups them', async () => { +testIfHg('gets mixed git and hg SCM roots and dedupes them', async () => { writeFiles(DIR, { 'first-repo/file1.txt': 'file1', 'first-repo/nested-dir/file2.txt': 'file2', @@ -186,6 +186,16 @@ test('gets changed files for git', async () => { .sort(), ).toEqual(['file1.txt']); + run(`${GIT} add -A`, DIR); + + // staged files should be included + ({changedFiles: files} = await getChangedFilesForRoots(roots, {})); + expect( + Array.from(files) + .map(filePath => path.basename(filePath)) + .sort(), + ).toEqual(['file1.txt']); + run(`${GIT} commit --no-gpg-sign -am "test2"`, DIR); writeFiles(DIR, { diff --git a/packages/jest-changed-files/src/git.ts b/packages/jest-changed-files/src/git.ts index 70ab9ced6cec..919eb19be98d 100644 --- a/packages/jest-changed-files/src/git.ts +++ b/packages/jest-changed-files/src/git.ts @@ -50,36 +50,45 @@ const adapter: SCMAdapter = { ), cwd, ); - } else if (changedSince) { - const committed = await findChangedFilesUsingCommand( - [ - 'log', - '--name-only', - '--pretty=format:', - 'HEAD', - `^${changedSince}`, - ].concat(includePaths), - cwd, - ); - const staged = await findChangedFilesUsingCommand( - ['diff', '--cached', '--name-only'].concat(includePaths), - cwd, - ); - const unstaged = await findChangedFilesUsingCommand( - ['ls-files', '--other', '--modified', '--exclude-standard'].concat( - includePaths, + } + if (changedSince) { + const [committed, staged, unstaged] = await Promise.all([ + findChangedFilesUsingCommand( + [ + 'log', + '--name-only', + '--pretty=format:', + 'HEAD', + `^${changedSince}`, + ].concat(includePaths), + cwd, ), - cwd, - ); + findChangedFilesUsingCommand( + ['diff', '--cached', '--name-only'].concat(includePaths), + cwd, + ), + findChangedFilesUsingCommand( + ['ls-files', '--other', '--modified', '--exclude-standard'].concat( + includePaths, + ), + cwd, + ), + ]); return [...committed, ...staged, ...unstaged]; - } else { - return findChangedFilesUsingCommand( + } + const [staged, unstaged] = await Promise.all([ + findChangedFilesUsingCommand( + ['diff', '--cached', '--name-only'].concat(includePaths), + cwd, + ), + findChangedFilesUsingCommand( ['ls-files', '--other', '--modified', '--exclude-standard'].concat( includePaths, ), cwd, - ); - } + ), + ]); + return [...staged, ...unstaged]; }, getRoot: async cwd => {