diff --git a/packages/playwright/src/util.ts b/packages/playwright/src/util.ts index ca909c916e..271dd60c92 100644 --- a/packages/playwright/src/util.ts +++ b/packages/playwright/src/util.ts @@ -83,7 +83,9 @@ export type TestFileFilter = { export async function createFileFiltersFromArguments(args: string[], onlyChanged: boolean): Promise { if (onlyChanged) { const untrackedFiles = childProcess.execSync('git ls-files --others --exclude-standard', { encoding: 'utf-8' }).split('\n').filter(Boolean); - args = untrackedFiles; + const changedFiles = childProcess.execSync('git diff --name-only', { encoding: 'utf-8' }).split('\n').filter(Boolean); + + args = [...untrackedFiles, ...changedFiles]; } return args.map(arg => { diff --git a/tests/playwright-test/only-changed.spec.ts b/tests/playwright-test/only-changed.spec.ts index 6f746faf52..86a71abf11 100644 --- a/tests/playwright-test/only-changed.spec.ts +++ b/tests/playwright-test/only-changed.spec.ts @@ -19,7 +19,7 @@ import { execSync } from 'node:child_process'; import { writeFile } from 'node:fs/promises'; import { join } from 'node:path'; -test('should filter by file name', async ({ runInlineTest }) => { +test('should detect untracked files', async ({ runInlineTest }) => { const result = await runInlineTest({ 'a.spec.ts': ` import { test, expect } from '@playwright/test'; @@ -45,3 +45,32 @@ test('should filter by file name', async ({ runInlineTest }) => { expect(result.failed).toBe(1); expect(result.output).toContain('c.spec.ts'); }); + + +test('should detect changed files', async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'a.spec.ts': ` + import { test, expect } from '@playwright/test'; + test('fails', () => { expect(1).toBe(2); }); + `, + 'b.spec.ts': ` + import { test, expect } from '@playwright/test'; + test('fails', () => { expect(1).toBe(2); }); + `, + async [magicFileCreationSymbol](baseDir) { + execSync(`git init --initial-branch=main`, { cwd: baseDir }); + execSync(`git add .`, { cwd: baseDir }); + execSync(`git commit -m init`, { cwd: baseDir }); + + await writeFile(join(baseDir, 'b.spec.ts'), ` + import { test, expect } from '@playwright/test'; + test('fails', () => { expect(1).toBe(3); }); + `); + } + }, { 'only-changed': true }); + + expect(result.exitCode).toBe(1); + expect(result.failed).toBe(1); + expect(result.output).toContain('b.spec.ts'); +}); +