Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/playwright/src/common/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ export class FixturePool {
this._appendFixtureList({ fixtures: selectedOverrides, location: optionOverrides!.location }, !!disallowWorkerFixtures, true);
}

if (optionOverrides) {
for (const key of overrideKeys) {
let registration = this._registrations.get(key);
if (!registration)
continue;
while (!registration.option && registration.super)
registration = registration.super;
if (!registration.option)
this._addLoadError(`Fixture "${key}" cannot be overridden in the configuration "use" section. Only fixtures registered with { option: true } can be set in the config.`, optionOverrides.location);
}
}

this.digest = this.validate();
}

Expand Down
77 changes: 77 additions & 0 deletions tests/playwright-test/fixture-errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,83 @@ test('should throw for unknown fixture parameter', async ({ runInlineTest }) =>
expect(result.exitCode).toBe(1);
});

test('should allow config to set an option overridden by a fixture', async ({ runInlineTest }) => {
const result = await runInlineTest({
'playwright.config.ts': `
module.exports = { use: { baseURL: 'from-config' } };
`,
'a.spec.ts': `
import { test as base, expect } from '@playwright/test';
const test = base.extend({
baseURL: [async ({}, use) => use('from-fixture'), { scope: 'test' }],
});

test('works', async ({ baseURL }) => {
expect(baseURL).toBe('from-fixture');
});
`,
});
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(1);
});

test('should allow config to set an option overridden by multiple fixtures', async ({ runInlineTest }) => {
const result = await runInlineTest({
'playwright.config.ts': `
module.exports = { use: { baseURL: 'from-config' } };
`,
'a.spec.ts': `
import { test as base, expect } from '@playwright/test';
const test1 = base.extend({
baseURL: [async ({}, use) => use('first'), { scope: 'test' }],
});
const test2 = test1.extend({
baseURL: [async ({ baseURL }, use) => use(baseURL + '-second'), { scope: 'test' }],
});

test2('works', async ({ baseURL }) => {
expect(baseURL).toBe('first-second');
});
`,
});
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(1);
});

test('should throw when config overrides a non-option fixture', async ({ runInlineTest }) => {
const result = await runInlineTest({
'playwright.config.ts': `
module.exports = { use: { foo: 'from-config' } };
`,
'a.spec.ts': `
import { test as base } from '@playwright/test';
const test = base.extend({
foo: async ({}, use) => use('from-fixture'),
});

test('does not run', async ({ foo }) => {});
`,
});
expect(result.exitCode).toBe(1);
expect(result.output).toContain('Fixture "foo" cannot be overridden in the configuration "use" section. Only fixtures registered with { option: true } can be set in the config.');
});

test('should allow config to set an option and ignore unknown keys', async ({ runInlineTest }) => {
const result = await runInlineTest({
'playwright.config.ts': `
module.exports = { use: { headless: false, unknownThing: 'ignored' } };
`,
'a.spec.ts': `
import { test, expect } from '@playwright/test';
test('works', async ({ headless }) => {
expect(headless).toBe(false);
});
`,
});
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(1);
});

test('should throw when calling runTest twice', async ({ runInlineTest }) => {
const result = await runInlineTest({
'f.spec.ts': `
Expand Down