Skip to content

Commit

Permalink
feat: add @retries decorator (#58)
Browse files Browse the repository at this point in the history
* feat: add `@retry` decorator

* chore: add changesets

* docs: fix typos

* refactor: rename `retry` decorator to `retries`

* refactor: rename `retry` decorator to `retries`
  • Loading branch information
SebastianSedzik committed Jan 31, 2024
1 parent 86ff2f5 commit 2962b6e
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 1 deletion.
20 changes: 20 additions & 0 deletions .changeset/ten-snails-buy.md
Original file line number Diff line number Diff line change
@@ -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
// ...
}
}
```
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
13 changes: 13 additions & 0 deletions lib/retries.decorator.ts
Original file line number Diff line number Diff line change
@@ -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 })
})
})
2 changes: 1 addition & 1 deletion playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
14 changes: 14 additions & 0 deletions tests/retries.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
})

0 comments on commit 2962b6e

Please sign in to comment.