Skip to content

Commit 7cf7aec

Browse files
authored
cherry-pick(#32094): fix(test runner): run project dependencies of --only-changed test files (#32172)
Closes #32070. We were applying `additionalFileMatcher` not just to `filteredProjectSuites`, but also to `projectSuites`. `projectSuites` is where we take dependency projects from, though - so `--only-changed` led to empty dependency projects, resulting in the reported bug. The fix is to only apply `additionalFileMatcher` on `filteredProjectSuites`.
1 parent d78ae01 commit 7cf7aec

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

packages/playwright/src/runner/loadUtils.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,10 @@ export async function createRootSuite(testRun: TestRun, errors: TestError[], sho
136136

137137
// Filter file suites for all projects.
138138
for (const [project, fileSuites] of testRun.projectSuites) {
139-
const filteredFileSuites = additionalFileMatcher ? fileSuites.filter(fileSuite => additionalFileMatcher(fileSuite.location!.file)) : fileSuites;
140-
const projectSuite = createProjectSuite(project, filteredFileSuites);
139+
const projectSuite = createProjectSuite(project, fileSuites);
141140
projectSuites.set(project, projectSuite);
142-
const filteredProjectSuite = filterProjectSuite(projectSuite, { cliFileFilters, cliTitleMatcher, testIdMatcher: config.testIdMatcher });
141+
142+
const filteredProjectSuite = filterProjectSuite(projectSuite, { cliFileFilters, cliTitleMatcher, testIdMatcher: config.testIdMatcher, additionalFileMatcher });
143143
filteredProjectSuites.set(project, filteredProjectSuite);
144144
}
145145
}
@@ -200,8 +200,8 @@ export async function createRootSuite(testRun: TestRun, errors: TestError[], sho
200200
const projectClosure = new Map(buildProjectsClosure(rootSuite.suites.map(suite => suite._fullProject!)));
201201

202202
// Clone file suites for dependency projects.
203-
for (const project of projectClosure.keys()) {
204-
if (projectClosure.get(project) === 'dependency')
203+
for (const [project, level] of projectClosure.entries()) {
204+
if (level === 'dependency')
205205
rootSuite._prependSuite(buildProjectSuite(project, projectSuites.get(project)!));
206206
}
207207
}
@@ -225,9 +225,9 @@ function createProjectSuite(project: FullProjectInternal, fileSuites: Suite[]):
225225
return projectSuite;
226226
}
227227

228-
function filterProjectSuite(projectSuite: Suite, options: { cliFileFilters: TestFileFilter[], cliTitleMatcher?: Matcher, testIdMatcher?: Matcher }): Suite {
228+
function filterProjectSuite(projectSuite: Suite, options: { cliFileFilters: TestFileFilter[], cliTitleMatcher?: Matcher, testIdMatcher?: Matcher, additionalFileMatcher?: Matcher }): Suite {
229229
// Fast path.
230-
if (!options.cliFileFilters.length && !options.cliTitleMatcher && !options.testIdMatcher)
230+
if (!options.cliFileFilters.length && !options.cliTitleMatcher && !options.testIdMatcher && !options.additionalFileMatcher)
231231
return projectSuite;
232232

233233
const result = projectSuite._deepClone();
@@ -238,6 +238,8 @@ function filterProjectSuite(projectSuite: Suite, options: { cliFileFilters: Test
238238
filterTestsRemoveEmptySuites(result, (test: TestCase) => {
239239
if (options.cliTitleMatcher && !options.cliTitleMatcher(test._grepTitle()))
240240
return false;
241+
if (options.additionalFileMatcher && !options.additionalFileMatcher(test.location.file))
242+
return false;
241243
return true;
242244
});
243245
return result;

tests/playwright-test/only-changed.spec.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,4 +364,53 @@ test('UI mode is not supported', async ({ runInlineTest }) => {
364364
const result = await runInlineTest({}, { 'only-changed': true, 'ui': true });
365365
expect(result.exitCode).toBe(1);
366366
expect(result.output).toContain('--only-changed is not supported in UI mode');
367+
});
368+
369+
test('should run project dependencies of changed tests', {
370+
annotation: {
371+
type: 'issue',
372+
description: 'https://github.com/microsoft/playwright/issues/32070',
373+
},
374+
}, async ({ runInlineTest, git, writeFiles }) => {
375+
await writeFiles({
376+
'playwright.config.ts': `
377+
module.exports = {
378+
projects: [
379+
{ name: 'setup', testMatch: 'setup.spec.ts', },
380+
{ name: 'main', dependencies: ['setup'] },
381+
],
382+
};
383+
`,
384+
'setup.spec.ts': `
385+
import { test, expect } from '@playwright/test';
386+
387+
test('setup test', async ({ page }) => {
388+
console.log('setup test is executed')
389+
});
390+
`,
391+
'a.spec.ts': `
392+
import { test, expect } from '@playwright/test';
393+
test('fails', () => { expect(1).toBe(2); });
394+
`,
395+
'b.spec.ts': `
396+
import { test, expect } from '@playwright/test';
397+
test('fails', () => { expect(1).toBe(2); });
398+
`,
399+
});
400+
401+
git(`add .`);
402+
git(`commit -m init`);
403+
404+
const result = await runInlineTest({
405+
'c.spec.ts': `
406+
import { test, expect } from '@playwright/test';
407+
test('fails', () => { expect(1).toBe(2); });
408+
`
409+
}, { 'only-changed': true });
410+
411+
expect(result.exitCode).toBe(1);
412+
expect(result.failed).toBe(1);
413+
expect(result.passed).toBe(1);
414+
415+
expect(result.output).toContain('setup test is executed');
367416
});

0 commit comments

Comments
 (0)