Skip to content

Commit db6c3bc

Browse files
authored
fix: fix test.scoped inheritance (#7814)
1 parent 773b10e commit db6c3bc

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

packages/runner/src/suite.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ function createSuiteCollector(
295295
mode: RunMode,
296296
each?: boolean,
297297
suiteOptions?: TestOptions,
298+
parentCollectorFixtures?: FixtureItem[],
298299
) {
299300
const tasks: (Test | Suite | SuiteCollector)[] = []
300301

@@ -395,7 +396,7 @@ function createSuiteCollector(
395396
test.type = 'test'
396397
})
397398

398-
let collectorFixtures: FixtureItem[] | undefined
399+
let collectorFixtures = parentCollectorFixtures
399400

400401
const collector: SuiteCollector = {
401402
type: 'collector',
@@ -555,6 +556,7 @@ function createSuite() {
555556
mode,
556557
this.each,
557558
options,
559+
currentSuite?.fixtures(),
558560
)
559561
}
560562

@@ -768,14 +770,15 @@ export function createTaskCollector(
768770
) {
769771
const collector = getCurrentSuite()
770772
const scopedFixtures = collector.fixtures()
773+
const context = { ...this }
771774
if (scopedFixtures) {
772-
this.fixtures = mergeScopedFixtures(
773-
this.fixtures || [],
775+
context.fixtures = mergeScopedFixtures(
776+
context.fixtures || [],
774777
scopedFixtures,
775778
)
776779
}
777780
collector.test.fn.call(
778-
this,
781+
context,
779782
formatName(name),
780783
optionsOrFn as TestOptions,
781784
optionsOrTest as TestFunction,

test/core/test/test-extend.test.ts

+36
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,39 @@ describe('scoping variables to suite', () => {
451451
})
452452
})
453453
})
454+
455+
describe('test.scoped repro #7793', () => {
456+
const extendedTest = test.extend<{ foo: boolean }>({
457+
foo: false,
458+
})
459+
460+
describe('top level', () => {
461+
extendedTest.scoped({ foo: true })
462+
463+
describe('second level', () => {
464+
extendedTest('foo is true', ({ foo }) => {
465+
expect(foo).toBe(true)
466+
})
467+
})
468+
})
469+
})
470+
471+
describe('test.scoped repro #7813', () => {
472+
const extendedTest = test.extend<{ foo?: boolean }>({
473+
foo: false,
474+
})
475+
476+
describe('foo is scoped to true', () => {
477+
extendedTest.scoped({ foo: true })
478+
479+
extendedTest('foo is true', ({ foo }) => {
480+
expect(foo).toBe(true)
481+
})
482+
})
483+
484+
describe('foo is left as default of false', () => {
485+
extendedTest('foo is false', ({ foo }) => {
486+
expect(foo).toBe(false)
487+
})
488+
})
489+
})

0 commit comments

Comments
 (0)