mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
fix(runner): clarify where test.use() can be called (#9486)
This commit is contained in:
parent
7a187d9994
commit
235cd10a43
@ -954,7 +954,7 @@ Step body.
|
|||||||
|
|
||||||
## method: Test.use
|
## method: Test.use
|
||||||
|
|
||||||
Specifies options or fixtures to use in a single test file or a [`method: Test.describe`] group. Most useful to set an option, for example set `locale` to configure `context` fixture.
|
Specifies options or fixtures to use in a single test file or a [`method: Test.describe`] group. Most useful to set an option, for example set `locale` to configure `context` fixture. `test.use` can be called either in the global scope or inside `test.describe`, it's is an error to call it within `beforeEach` or `beforeAll`.
|
||||||
|
|
||||||
```js js-flavor=js
|
```js js-flavor=js
|
||||||
const { test, expect } = require('@playwright/test');
|
const { test, expect } = require('@playwright/test');
|
||||||
|
|||||||
@ -178,7 +178,7 @@ export class TestTypeImpl {
|
|||||||
private _use(location: Location, fixtures: Fixtures) {
|
private _use(location: Location, fixtures: Fixtures) {
|
||||||
const suite = currentlyLoadingFileSuite();
|
const suite = currentlyLoadingFileSuite();
|
||||||
if (!suite)
|
if (!suite)
|
||||||
throw errorWithLocation(location, `test.use() can only be called in a test file`);
|
throw errorWithLocation(location, `test.use() can only be called in a test file and can only be nested in test.describe()`);
|
||||||
suite._use.push({ fixtures, location });
|
suite._use.push({ fixtures, location });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
3
packages/playwright-test/types/test.d.ts
vendored
3
packages/playwright-test/types/test.d.ts
vendored
@ -2219,7 +2219,8 @@ export interface TestType<TestArgs extends KeyValue, WorkerArgs extends KeyValue
|
|||||||
/**
|
/**
|
||||||
* Specifies options or fixtures to use in a single test file or a
|
* Specifies options or fixtures to use in a single test file or a
|
||||||
* [test.describe(title, callback)](https://playwright.dev/docs/api/class-test#test-describe) group. Most useful to set an
|
* [test.describe(title, callback)](https://playwright.dev/docs/api/class-test#test-describe) group. Most useful to set an
|
||||||
* option, for example set `locale` to configure `context` fixture.
|
* option, for example set `locale` to configure `context` fixture. `test.use` can be called either in the global scope or
|
||||||
|
* inside `test.describe`, it's is an error to call it within `beforeEach` or `beforeAll`.
|
||||||
*
|
*
|
||||||
* ```ts
|
* ```ts
|
||||||
* import { test, expect } from '@playwright/test';
|
* import { test, expect } from '@playwright/test';
|
||||||
|
|||||||
@ -163,3 +163,19 @@ test('should use options from the config', async ({ runInlineTest }) => {
|
|||||||
expect(result.exitCode).toBe(0);
|
expect(result.exitCode).toBe(0);
|
||||||
expect(result.passed).toBe(2);
|
expect(result.passed).toBe(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('test.use() should throw if called from beforeAll ', async ({ runInlineTest }) => {
|
||||||
|
const result = await runInlineTest({
|
||||||
|
'a.test.ts': `
|
||||||
|
const test = pwt.test;
|
||||||
|
test.beforeAll(() => {
|
||||||
|
test.use({});
|
||||||
|
});
|
||||||
|
test('should work', async () => {
|
||||||
|
});
|
||||||
|
`,
|
||||||
|
});
|
||||||
|
expect(result.exitCode).toBe(1);
|
||||||
|
expect(result.output).toContain('test.use() can only be called in a test file and can only be nested in test.describe()');
|
||||||
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user