-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: support fixtures * docs: fixtures * chore: generate changeset * chore: improve typings
- Loading branch information
1 parent
5bb418b
commit 3a44870
Showing
12 changed files
with
287 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- | ||
"playwright-decorators": minor | ||
--- | ||
|
||
Add support for fixtures | ||
|
||
This release introduce a new method `extend<T>(customFixture)` that allows to create decorators (`afterAll`, `afterEach`, `test`, `beforeAll`, `beforeEach`) with access to custom fixtures. | ||
|
||
```ts | ||
import { test as base } from 'playwright'; | ||
import { suite, test, extend } from 'playwright-decorators'; | ||
|
||
// #1 Create fixture type | ||
type UserFixture = { | ||
user: { | ||
firstName: string; | ||
lastName: string; | ||
} | ||
} | ||
|
||
// #2 Create user fixture | ||
const withUser = base.extend<UserFixture>({ | ||
user: async ({}, use) => { | ||
await use({ | ||
firstName: 'John', | ||
lastName: 'Doe' | ||
}) | ||
} | ||
}) | ||
|
||
// #3 Generate afterAll, afterEach, test, beforeAll, beforeEach decorators with access to the user fixture | ||
const { | ||
afterAll, | ||
afterEach, | ||
test, | ||
beforeAll, | ||
beforeEach, | ||
} = extend<UserFixture>(withUser); | ||
|
||
// #4 Use decorators | ||
@suite() | ||
class MyTestSuite { | ||
@beforeAll() | ||
async beforeAll({ user }: TestArgs<UserFixture>) { // have access to user fixture | ||
// ... | ||
} | ||
|
||
@test() | ||
async test({ user }: TestArgs<UserFixture>) { // have access to user fixture | ||
// ... | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,31 @@ | ||
import playwright from '@playwright/test' | ||
import { decoratePlaywrightTest } from './helpers' | ||
import { TestMethod } from './common' | ||
import { TestMethod, TestType } from './common' | ||
|
||
export interface AfterAllDecoratorOptions<T> { | ||
/** | ||
* Custom playwright instance to use instead of standard one. | ||
* For example, provide result of `playwright.extend<T>(customFixture)` to ensure availability of custom fixture in the `afterAll` hook. | ||
*/ | ||
playwright?: TestType<T> | ||
} | ||
|
||
/** | ||
* Run method after all tests in the suite. | ||
* Target class should be marked by @suite decorator. | ||
*/ | ||
export const afterAll = () => | ||
function (originalMethod: TestMethod, context: ClassMethodDecoratorContext) { | ||
export const afterAll = <T = void>(options?: AfterAllDecoratorOptions<T>) => | ||
function (originalMethod: TestMethod<T>, context: ClassMethodDecoratorContext) { | ||
context.addInitializer(function () { | ||
const decoratedBeforeAll = decoratePlaywrightTest( | ||
const decoratedBeforeAll = decoratePlaywrightTest<T>( | ||
originalMethod, | ||
(originalMethod) => | ||
(...args) => | ||
originalMethod.call(this, ...args) | ||
) | ||
|
||
playwright.afterAll(decoratedBeforeAll) | ||
const { afterAll } = options?.playwright || (playwright as TestType<T>) | ||
|
||
afterAll(decoratedBeforeAll) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,31 @@ | ||
import playwright from '@playwright/test' | ||
import { decoratePlaywrightTest } from './helpers' | ||
import { TestMethod } from './common' | ||
import { TestMethod, TestType } from './common' | ||
|
||
export interface AfterEachDecoratorOptions<T> { | ||
/** | ||
* Custom playwright instance to use instead of standard one. | ||
* For example, provide result of `playwright.extend<T>(customFixture)` to ensure availability of custom fixture in the `afterEach` hook. | ||
*/ | ||
playwright?: TestType<T> | ||
} | ||
|
||
/** | ||
* Run method after each test in suite. | ||
* Target class should be marked by @suite decorator. | ||
*/ | ||
export const afterEach = () => | ||
function (originalMethod: TestMethod, context: ClassMethodDecoratorContext) { | ||
export const afterEach = <T = void>(options?: AfterEachDecoratorOptions<T>) => | ||
function (originalMethod: TestMethod<T>, context: ClassMethodDecoratorContext) { | ||
context.addInitializer(function () { | ||
const decoratedBeforeEach = decoratePlaywrightTest( | ||
const decoratedBeforeEach = decoratePlaywrightTest<T>( | ||
originalMethod, | ||
(originalMethod) => | ||
(...args) => | ||
originalMethod.call(this, ...args) | ||
) | ||
|
||
playwright.afterEach(decoratedBeforeEach) | ||
const { afterEach } = options?.playwright || (playwright as TestType<T>) | ||
|
||
afterEach(decoratedBeforeEach) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,31 @@ | ||
import playwright from '@playwright/test' | ||
import { decoratePlaywrightTest } from './helpers' | ||
import { TestMethod } from './common' | ||
import { TestMethod, TestType } from './common' | ||
|
||
export interface BeforeAllDecoratorOptions<T> { | ||
/** | ||
* Custom playwright instance to use instead of standard one. | ||
* For example, provide result of `playwright.extend<T>(customFixture)` to ensure availability of custom fixture in the `beforeAll` hook. | ||
*/ | ||
playwright?: TestType<T> | ||
} | ||
|
||
/** | ||
* Run method before all tests in the suite. | ||
* Target class should be marked by @suite decorator. | ||
*/ | ||
export const beforeAll = () => | ||
function (originalMethod: TestMethod, context: ClassMethodDecoratorContext) { | ||
export const beforeAll = <T = void>(options?: BeforeAllDecoratorOptions<T>) => | ||
function (originalMethod: TestMethod<T>, context: ClassMethodDecoratorContext) { | ||
context.addInitializer(function () { | ||
const decoratedBeforeAll = decoratePlaywrightTest( | ||
const decoratedBeforeAll = decoratePlaywrightTest<T>( | ||
originalMethod, | ||
(originalMethod) => | ||
(...args) => | ||
originalMethod.call(this, ...args) | ||
) | ||
|
||
playwright.beforeAll(decoratedBeforeAll) | ||
const { beforeAll } = options?.playwright || (playwright as TestType<T>) | ||
|
||
beforeAll(decoratedBeforeAll) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,31 @@ | ||
import playwright from '@playwright/test' | ||
import { decoratePlaywrightTest } from './helpers' | ||
import { TestMethod } from './common' | ||
import { TestMethod, TestType } from './common' | ||
|
||
export interface BeforeEachDecoratorOptions<T> { | ||
/** | ||
* Custom playwright instance to use instead of standard one. | ||
* For example, provide result of `playwright.extend<T>(customFixture)` to ensure availability of custom fixture in the `beforeEach` hook. | ||
*/ | ||
playwright?: TestType<T> | ||
} | ||
|
||
/** | ||
* Run method before each test in the suite. | ||
* Target class should be marked by @suite decorator. | ||
*/ | ||
export const beforeEach = () => | ||
function (originalMethod: TestMethod, context: ClassMethodDecoratorContext) { | ||
export const beforeEach = <T = void>(options?: BeforeEachDecoratorOptions<T>) => | ||
function (originalMethod: TestMethod<T>, context: ClassMethodDecoratorContext) { | ||
context.addInitializer(function () { | ||
const decoratedBeforeEach = decoratePlaywrightTest( | ||
const decoratedBeforeEach = decoratePlaywrightTest<T>( | ||
originalMethod, | ||
(originalMethod) => | ||
(...args) => | ||
originalMethod.call(this, ...args) | ||
) | ||
|
||
playwright.beforeEach(decoratedBeforeEach) | ||
const { beforeEach } = options?.playwright || (playwright as TestType<T>) | ||
|
||
beforeEach(decoratedBeforeEach) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { afterAll } from './afterAll.decorator' | ||
import { afterEach } from './afterEach.decorator' | ||
import { beforeAll } from './beforeAll.decorator' | ||
import { beforeEach } from './beforeEach.decorator' | ||
import { test } from './test.decorator' | ||
import { TestType } from './common' | ||
|
||
/** | ||
* Generates afterAll, afterEach, test, beforeAll, beforeEach decorators with access to custom fixture. | ||
* @param customPlaywright - method returned from playwright.extend<T> | ||
*/ | ||
export const extend = <T>(customPlaywright: TestType<T>) => { | ||
return { | ||
afterAll: (...options: Parameters<typeof afterAll>) => | ||
afterAll<T>({ ...options, playwright: customPlaywright }), | ||
afterEach: (...options: Parameters<typeof afterEach>) => | ||
afterEach<T>({ ...options, playwright: customPlaywright }), | ||
test: (...options: Parameters<typeof test>) => | ||
test<T>({ ...options, playwright: customPlaywright }), | ||
beforeAll: (...options: Parameters<typeof beforeAll>) => | ||
beforeAll<T>({ ...options, playwright: customPlaywright }), | ||
beforeEach: (...options: Parameters<typeof beforeEach>) => | ||
beforeEach<T>({ ...options, playwright: customPlaywright }) | ||
} | ||
} |
Oops, something went wrong.