Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(test runner): --list should ignore '.only' annotations (#20868)

Dmitry Gozman6d03211439a5
diff --git a/packages/playwright-test/src/runner/loadUtils.ts b/packages/playwright-test/src/runner/loadUtils.ts
index fb14806f9c..4dc78faa64 100644
--- a/packages/playwright-test/src/runner/loadUtils.ts
+++ b/packages/playwright-test/src/runner/loadUtils.ts
@@ -27,7 +27,7 @@ import { requireOrImport } from '../common/transform';
import { buildFileSuiteForProject, filterByFocusedLine, filterByTestIds, filterOnly, filterTestsRemoveEmptySuites } from '../common/suiteUtils';
import { filterForShard } from './testGroups';
-export async function loadAllTests(mode: 'out-of-process' | 'in-process', config: FullConfigInternal, projectsToIgnore: Set<FullProjectInternal>, fileMatcher: Matcher, errors: TestError[]): Promise<Suite> {
+export async function loadAllTests(mode: 'out-of-process' | 'in-process', config: FullConfigInternal, projectsToIgnore: Set<FullProjectInternal>, fileMatcher: Matcher, errors: TestError[], shouldFilterOnly: boolean): Promise<Suite> {
const projects = filterProjects(config.projects, config._internal.cliProjectFilter);
let filesToRunByProject = new Map<FullProjectInternal, string[]>();
@@ -118,7 +118,8 @@ export async function loadAllTests(mode: 'out-of-process' | 'in-process', config
}
// Filter only for leaf projects.
- filterOnly(rootSuite);
+ if (shouldFilterOnly)
+ filterOnly(rootSuite);
// Prepend the projects that are dependencies.
for (const project of dependencyProjects) {
diff --git a/packages/playwright-test/src/runner/tasks.ts b/packages/playwright-test/src/runner/tasks.ts
index 3cee8d1ddd..3b93d0e229 100644
--- a/packages/playwright-test/src/runner/tasks.ts
+++ b/packages/playwright-test/src/runner/tasks.ts
@@ -53,7 +53,7 @@ export type TaskRunnerState = {
export function createTaskRunner(config: FullConfigInternal, reporter: Multiplexer): TaskRunner<TaskRunnerState> {
const taskRunner = new TaskRunner<TaskRunnerState>(reporter, config.globalTimeout);
addGlobalSetupTasks(taskRunner, config);
- taskRunner.addTask('load tests', createLoadTask('in-process'));
+ taskRunner.addTask('load tests', createLoadTask('in-process', true));
addRunTasks(taskRunner, config);
return taskRunner;
}
@@ -66,7 +66,7 @@ export function createTaskRunnerForWatchSetup(config: FullConfigInternal, report
export function createTaskRunnerForWatch(config: FullConfigInternal, reporter: Multiplexer, projectsToIgnore?: Set<FullProjectInternal>, additionalFileMatcher?: Matcher): TaskRunner<TaskRunnerState> {
const taskRunner = new TaskRunner<TaskRunnerState>(reporter, 0);
- taskRunner.addTask('load tests', createLoadTask('out-of-process', projectsToIgnore, additionalFileMatcher));
+ taskRunner.addTask('load tests', createLoadTask('out-of-process', true, projectsToIgnore, additionalFileMatcher));
addRunTasks(taskRunner, config);
return taskRunner;
}
@@ -94,7 +94,7 @@ function addRunTasks(taskRunner: TaskRunner<TaskRunnerState>, config: FullConfig
export function createTaskRunnerForList(config: FullConfigInternal, reporter: Multiplexer): TaskRunner<TaskRunnerState> {
const taskRunner = new TaskRunner<TaskRunnerState>(reporter, config.globalTimeout);
- taskRunner.addTask('load tests', createLoadTask('in-process'));
+ taskRunner.addTask('load tests', createLoadTask('in-process', false));
taskRunner.addTask('report begin', async ({ reporter, rootSuite }) => {
reporter.onBegin?.(config, rootSuite!);
return () => reporter.onEnd();
@@ -155,12 +155,12 @@ function createRemoveOutputDirsTask(): Task<TaskRunnerState> {
};
}
-function createLoadTask(mode: 'out-of-process' | 'in-process', projectsToIgnore = new Set<FullProjectInternal>(), additionalFileMatcher?: Matcher): Task<TaskRunnerState> {
+function createLoadTask(mode: 'out-of-process' | 'in-process', shouldFilterOnly: boolean, projectsToIgnore = new Set<FullProjectInternal>(), additionalFileMatcher?: Matcher): Task<TaskRunnerState> {
return async (context, errors) => {
const { config } = context;
const cliMatcher = config._internal.cliArgs.length ? createFileMatcherFromArguments(config._internal.cliArgs) : () => true;
const fileMatcher = (value: string) => cliMatcher(value) && (additionalFileMatcher ? additionalFileMatcher(value) : true);
- context.rootSuite = await loadAllTests(mode, config, projectsToIgnore, fileMatcher, errors);
+ context.rootSuite = await loadAllTests(mode, config, projectsToIgnore, fileMatcher, errors, shouldFilterOnly);
// Fail when no tests.
if (!context.rootSuite.allTests().length && !config._internal.passWithNoTests && !config.shard)
throw new Error(`No tests found`);
diff --git a/tests/playwright-test/list-mode.spec.ts b/tests/playwright-test/list-mode.spec.ts
index a6bf279b9d..63d4d33521 100644
--- a/tests/playwright-test/list-mode.spec.ts
+++ b/tests/playwright-test/list-mode.spec.ts
@@ -151,3 +151,24 @@ test('should report errors', async ({ runInlineTest }) => {
expect(result.exitCode).toBe(1);
expect(result.output).toContain('> 6 | oh = 2;');
});
+
+test('should ignore .only', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'a.test.js': `
+ const { test } = pwt;
+ test('example1', async ({}) => {
+ expect(1 + 1).toBe(2);
+ });
+ test.only('example2', async ({}) => {
+ expect(1 + 1).toBe(2);
+ });
+ `
+ }, { 'list': true });
+ expect(result.exitCode).toBe(0);
+ expect(result.output).toContain([
+ `Listing tests:`,
+ ` a.test.js:6:7 › example1`,
+ ` a.test.js:9:12 › example2`,
+ `Total: 2 tests in 1 file`
+ ].join('\n'));
+});