diff --git a/.changeset/ten-snails-buy.md b/.changeset/ten-snails-buy.md new file mode 100644 index 0000000..387055b --- /dev/null +++ b/.changeset/ten-snails-buy.md @@ -0,0 +1,20 @@ +--- +"playwright-decorators": minor +--- + +Add `@retries` decorator + +Set the maximum number of retry attempts given to failed `@tests` in the `@suite` + +```ts +import { suite, test, retries } from 'playwright-decorators'; + +@retries(3) // <-- Decorate suite with @retries() +@suite() +class MyTestSuite { + @test() + async test() { // <- This test may be retried up to 3 times if it fails + // ... + } +} +``` diff --git a/README.md b/README.md index 4c11735..1980424 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ class MyTestSuite { - [Run only selected test(s) or suite(s): `@only`](#run-only-selected-tests-or-suites-only) - [Run test(s) or suite(s) with certain tag(s): `@tag`](#run-tests-or-suites-with-certain-tags-tagtags-string) - [Add custom annotation to test(s): `@annotate`](#add-custom-annotation-to-tests-annotatetype-string-description-string) +- [Change or set retries for test(s): `@retries`](#change-or-set-retries-for-tests-retriesretries-number) - [Run test(s) or suite(s) in debug mode: `@debug`](#run-tests-or-suites-in-debug-mode-debug) - [Run test(s) or suite(s) in preview mode: `@preview`](#run-tests-or-suites-in-preview-mode-preview) - [Create custom decorator: `createSuiteDecorator`, `createTestDecorator`, `createSuiteAndTestDecorator`](#custom-decorators) @@ -366,6 +367,25 @@ class MyTestSuite { - `type` (required) - type of annotation, for example 'skip' or 'fail'. - `description` (optional) - description of annotation. +### Change or set retries for test(s): `@retries(retries: number)` +Set the maximum number of retry attempts given to failed `@tests` in the `@suite` + +```ts +import { suite, test, retries } from 'playwright-decorators'; + +@retries(3) // <-- Decorate suite with @retries() +@suite() +class MyTestSuite { + @test() + async test() { // <- This test may be retried up to 3 times if it fails + // ... + } +} +``` + +#### Options +- `retries` (required) - the max number of retries for each test. + ### Run test(s) or suite(s) in debug mode: `@debug()` Runs a `@test`(s) or `@suite`(s) in debug mode. diff --git a/lib/index.ts b/lib/index.ts index b810c16..d70812e 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -20,6 +20,7 @@ export { fixme } from './fixme.decorator' export { only } from './only.decorator' export { annotation } from './annotation.decorator' export { tag } from './tag.decorator' +export { retries } from './retries.decorator' // helpers export { debug } from './debug.decorator' diff --git a/lib/retries.decorator.ts b/lib/retries.decorator.ts new file mode 100644 index 0000000..7e79c9b --- /dev/null +++ b/lib/retries.decorator.ts @@ -0,0 +1,13 @@ +import playwright from '@playwright/test' +import { createSuiteDecorator } from '../lib' + +/** + * Set the maximum number of retry attempts given to failed @tests in the @suite + * @param retries the number of retries for each @test. + */ +export const retries = (retries: number) => + createSuiteDecorator('retry', ({ suite }) => { + suite.initialized(() => { + playwright.describe.configure({ retries }) + }) + }) diff --git a/playwright.config.ts b/playwright.config.ts index f79d688..1876b82 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -4,7 +4,7 @@ export default defineConfig({ testDir: './tests', fullyParallel: true, forbidOnly: !!process.env.CI, - retries: process.env.CI ? 2 : 0, + retries: 0, workers: 1, reporter: 'html', use: { diff --git a/tests/retries.spec.ts b/tests/retries.spec.ts new file mode 100644 index 0000000..5da775b --- /dev/null +++ b/tests/retries.spec.ts @@ -0,0 +1,14 @@ +import { suite, test, retries, TestInfo } from '../lib' +import playwright, { expect } from '@playwright/test' + +playwright.describe('@retries decorator', () => { + @retries(3) + @suite() + class RetriesSuite { + @test() + // eslint-disable-next-line no-empty-pattern + 'Should retry test 3 times'({}, testInfo: TestInfo) { + expect(testInfo.retry + 1).toEqual(3) + } + } +})