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

This commit is contained in:
Dmitry Gozman 2023-02-13 11:13:30 -08:00 committed by GitHub
parent fdcd7b549d
commit 6d03211439
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 7 deletions

View File

@ -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) {

View File

@ -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`);

View File

@ -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'));
});