diff --git a/packages/runner/src/suite.ts b/packages/runner/src/suite.ts index 2b53016623f0..ab0d2945cb0d 100644 --- a/packages/runner/src/suite.ts +++ b/packages/runner/src/suite.ts @@ -295,6 +295,7 @@ function createSuiteCollector( mode: RunMode, each?: boolean, suiteOptions?: TestOptions, + parentCollectorFixtures?: FixtureItem[], ) { const tasks: (Test | Suite | SuiteCollector)[] = [] @@ -395,7 +396,7 @@ function createSuiteCollector( test.type = 'test' }) - let collectorFixtures: FixtureItem[] | undefined + let collectorFixtures = parentCollectorFixtures const collector: SuiteCollector = { type: 'collector', @@ -555,6 +556,7 @@ function createSuite() { mode, this.each, options, + currentSuite?.fixtures(), ) } @@ -768,14 +770,15 @@ export function createTaskCollector( ) { const collector = getCurrentSuite() const scopedFixtures = collector.fixtures() + const context = { ...this } if (scopedFixtures) { - this.fixtures = mergeScopedFixtures( - this.fixtures || [], + context.fixtures = mergeScopedFixtures( + context.fixtures || [], scopedFixtures, ) } collector.test.fn.call( - this, + context, formatName(name), optionsOrFn as TestOptions, optionsOrTest as TestFunction, diff --git a/test/core/test/test-extend.test.ts b/test/core/test/test-extend.test.ts index 2413eb521e1e..52dd01f547cb 100644 --- a/test/core/test/test-extend.test.ts +++ b/test/core/test/test-extend.test.ts @@ -451,3 +451,39 @@ describe('scoping variables to suite', () => { }) }) }) + +describe('test.scoped repro #7793', () => { + const extendedTest = test.extend<{ foo: boolean }>({ + foo: false, + }) + + describe('top level', () => { + extendedTest.scoped({ foo: true }) + + describe('second level', () => { + extendedTest('foo is true', ({ foo }) => { + expect(foo).toBe(true) + }) + }) + }) +}) + +describe('test.scoped repro #7813', () => { + const extendedTest = test.extend<{ foo?: boolean }>({ + foo: false, + }) + + describe('foo is scoped to true', () => { + extendedTest.scoped({ foo: true }) + + extendedTest('foo is true', ({ foo }) => { + expect(foo).toBe(true) + }) + }) + + describe('foo is left as default of false', () => { + extendedTest('foo is false', ({ foo }) => { + expect(foo).toBe(false) + }) + }) +})