diff --git a/packages/runner/src/utils/collect.ts b/packages/runner/src/utils/collect.ts index 3f80a4d2d99c..f016db68b581 100644 --- a/packages/runner/src/utils/collect.ts +++ b/packages/runner/src/utils/collect.ts @@ -20,9 +20,17 @@ export function interpretTaskModes( const traverseSuite = (suite: Suite, parentIsOnly?: boolean, parentMatchedWithLocation?: boolean) => { const suiteIsOnly = parentIsOnly || suite.mode === 'only' + // Check if any tasks in this suite have `.only` - if so, only those should run + const hasSomeTasksOnly = onlyMode && suite.tasks.some( + t => t.mode === 'only' || (t.type === 'suite' && someTasksAreOnly(t)), + ) + suite.tasks.forEach((t) => { // Check if either the parent suite or the task itself are marked as included - const includeTask = suiteIsOnly || t.mode === 'only' + // If there are tasks with `.only` in this suite, only include those (not all tasks from describe.only) + const includeTask = hasSomeTasksOnly + ? (t.mode === 'only' || (t.type === 'suite' && someTasksAreOnly(t))) + : (suiteIsOnly || t.mode === 'only') if (onlyMode) { if (t.type === 'suite' && (includeTask || someTasksAreOnly(t))) { // Don't skip this suite diff --git a/test/core/test/nested-only.test.ts b/test/core/test/nested-only.test.ts new file mode 100644 index 000000000000..038c22021594 --- /dev/null +++ b/test/core/test/nested-only.test.ts @@ -0,0 +1,19 @@ +import { describe, expect, it } from 'vitest' + +describe('nested only behavior', () => { + describe.only('describe.only with nested test.only', () => { + it.only('should be the only test that runs', () => { + expect(true).toBe(true) + }) + + it('should NOT run because the previous test has test.only', () => { + throw new Error('This test should not run') + }) + }) + + describe('another suite', () => { + it('should not run - outside describe.only', () => { + throw new Error('This test should not run') + }) + }) +})