From 6f349b9414277f0197432b45047a86c48bbba6ac Mon Sep 17 00:00:00 2001 From: Isaac Daly Date: Sun, 17 Nov 2024 21:47:19 +1100 Subject: [PATCH] feat(test): allow fixtures to be specified for single tests This patch adds `fixtures` to the `TestDetails` for `test`. Each test with `fixtures` will be wrapped by a `Suite`. Fixes #27138 --- packages/playwright/src/common/testType.ts | 12 +++- packages/playwright/types/test.d.ts | 1 + tests/playwright-test/test-fixtures.spec.ts | 61 +++++++++++++++++++++ utils/generate_types/overrides-test.d.ts | 1 + 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 tests/playwright-test/test-fixtures.spec.ts diff --git a/packages/playwright/src/common/testType.ts b/packages/playwright/src/common/testType.ts index d3c2f1c23a..370281c98d 100644 --- a/packages/playwright/src/common/testType.ts +++ b/packages/playwright/src/common/testType.ts @@ -104,7 +104,17 @@ export class TestTypeImpl { test._requireFile = suite._requireFile; test._staticAnnotations.push(...validatedDetails.annotations); test._tags.push(...validatedDetails.tags); - suite._addTest(test); + + if (typeof details.fixtures === 'object') { + const containingSubSuite = new Suite(title, 'describe'); + containingSubSuite._requireFile = suite._requireFile; + containingSubSuite.location = location; + containingSubSuite._use.push({ fixtures: details.fixtures, location }); + containingSubSuite._addTest(test); + suite._addSuite(containingSubSuite); + } else { + suite._addTest(test); + } if (type === 'only' || type === 'fail.only') test._only = true; diff --git a/packages/playwright/types/test.d.ts b/packages/playwright/types/test.d.ts index b3d66a7f6d..ea52e60d0f 100644 --- a/packages/playwright/types/test.d.ts +++ b/packages/playwright/types/test.d.ts @@ -1857,6 +1857,7 @@ type TestDetailsAnnotation = { export type TestDetails = { tag?: string | string[]; annotation?: TestDetailsAnnotation | TestDetailsAnnotation[]; + fixtures?: Fixtures; } type TestBody = (args: TestArgs, testInfo: TestInfo) => Promise | void; diff --git a/tests/playwright-test/test-fixtures.spec.ts b/tests/playwright-test/test-fixtures.spec.ts new file mode 100644 index 0000000000..596127971d --- /dev/null +++ b/tests/playwright-test/test-fixtures.spec.ts @@ -0,0 +1,61 @@ +/** + * Copyright (c) Microsoft Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { test, expect } from './playwright-test-fixtures'; + +test('test() should support fixture in test details', async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'a.test.ts': ` + import { test as base, expect } from '@playwright/test'; + const test = base.extend({ + input: 'input', + foo: async ({ input }, use) => { + await use(input); + } + }); + + test('test', { fixtures: { input: 'asd' } }, ({ foo }) => { + expect(foo).toBe('asd'); + }); + ` + }); + expect(result.exitCode).toBe(0); + expect(result.passed).toBe(1); +}); + +test('fixtures from the first test should not leak into the second test', async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'a.test.ts': ` + import { test as base, expect } from '@playwright/test'; + const test = base.extend({ + input: 'defaultInput', + foo: async ({ input }, use) => { + await use(input); + } + }); + + test('test1', { fixtures: { input: 'asd' } }, ({ foo }) => { + expect(foo).toBe('asd'); + }); + + test('test2', ({ foo }) => { + expect(foo).toBe('defaultInput'); + }); + ` + }); + expect(result.exitCode).toBe(0); + expect(result.passed).toBe(2); +}); diff --git a/utils/generate_types/overrides-test.d.ts b/utils/generate_types/overrides-test.d.ts index fc0d90a7db..817e7d75b6 100644 --- a/utils/generate_types/overrides-test.d.ts +++ b/utils/generate_types/overrides-test.d.ts @@ -73,6 +73,7 @@ type TestDetailsAnnotation = { export type TestDetails = { tag?: string | string[]; annotation?: TestDetailsAnnotation | TestDetailsAnnotation[]; + fixtures?: Fixtures; } type TestBody = (args: TestArgs, testInfo: TestInfo) => Promise | void;