docs(fixtures): explain an option array value edge case (#32261)

Closes #32033.
This commit is contained in:
Dmitry Gozman 2024-08-22 05:47:19 -07:00 committed by GitHub
parent f74c6d77db
commit dc4a8e48eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -454,10 +454,6 @@ test('example test', async ({ slowFixture }) => {
## Fixtures-options
:::note
Overriding custom fixtures in the config file has changed in version 1.18. [Learn more](./release-notes#breaking-change-custom-config-options).
:::
Playwright Test supports running multiple test projects that can be separately configured. You can use "option" fixtures to make your configuration options declarative and type-checked. Learn more about [parametrizing tests](./test-parameterize.md).
Below we'll create a `defaultItem` option in addition to the `todoPage` fixture from other examples. This option will be set in configuration file. Note the tuple syntax and `{ option: true }` argument.
@ -555,6 +551,30 @@ export default defineConfig<MyOptions>({
});
```
**Array as an option value**
If the value of your option is an array, for example `[{ name: 'Alice' }, { name: 'Bob' }]`, you'll need to wrap it into an extra array when providing the value. This is best illustrated with an example.
```js
type Person = { name: string };
const test = base.extend<{ persons: Person[] }>({
// Declare the option, default value is an empty array.
persons: [[], { option: true }],
});
// Option value is an array of persons.
const actualPersons = [{ name: 'Alice' }, { name: 'Bob' }];
test.use({
// CORRECT: Wrap the value into an array and pass the scope.
persons: [actualPersons, { scope: 'test' }],
});
test.use({
// WRONG: passing an array value directly will not work.
persons: actualPersons,
});
```
## Execution order
Each fixture has a setup and teardown phase separated by the `await use()` call in the fixture. Setup is executed before the fixture is used by the test/hook, and teardown is executed when the fixture will not be used by the test/hook anymore.