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
This commit is contained in:
Isaac Daly 2024-11-17 21:47:19 +11:00
parent 46321e5bf2
commit 6f349b9414
No known key found for this signature in database
4 changed files with 74 additions and 1 deletions

View file

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

View file

@ -1857,6 +1857,7 @@ type TestDetailsAnnotation = {
export type TestDetails = {
tag?: string | string[];
annotation?: TestDetailsAnnotation | TestDetailsAnnotation[];
fixtures?: Fixtures;
}
type TestBody<TestArgs> = (args: TestArgs, testInfo: TestInfo) => Promise<void> | void;

View file

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

View file

@ -73,6 +73,7 @@ type TestDetailsAnnotation = {
export type TestDetails = {
tag?: string | string[];
annotation?: TestDetailsAnnotation | TestDetailsAnnotation[];
fixtures?: Fixtures;
}
type TestBody<TestArgs> = (args: TestArgs, testInfo: TestInfo) => Promise<void> | void;