Skip to content

Commit

Permalink
fix: skip & tag decorators (#29)
Browse files Browse the repository at this point in the history
* fix: skip decorator do not pass reason

* fix: display annotation on skipped tests

* fix: rewrite error messages to be more detailed

* docs: refactor main example

* docs: fix tests badge

* chore: generate changeset
  • Loading branch information
SebastianSedzik committed Jan 1, 2024
1 parent 173256e commit 7398cc2
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 19 deletions.
8 changes: 8 additions & 0 deletions .changeset/funny-pugs-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'playwright-decorators': patch
---

- Display `reason` from `@skip` decorator.
- Show tags from `@skip` tests.
- Provide more detailed info in error messages.
- Cosmetic changes in readme file
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Tests
name: PR

on:
pull_request:
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
TypeScript's decorators for writing Playwright based tests.

[![npm version](https://badge.fury.io/js/playwright-decorators.svg)](https://www.npmjs.com/package/playwright-decorators)
[![package tests](https://github.com/SebastianSedzik/playwright-decorators/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/SebastianSedzik/playwright-decorators/actions/workflows/tests.yml)
[![package tests](https://github.com/SebastianSedzik/playwright-decorators/actions/workflows/master.yml/badge.svg?branch=master)](https://github.com/SebastianSedzik/playwright-decorators/actions/workflows/master.yml)

## 🌱 Installation
```sh
Expand All @@ -13,19 +13,19 @@ npm i playwright-decorators
## 🏗️ Usage
Declare tests using `@suite` and `@test` decorators
```ts
import { suite, test, slow, annotation } from 'playwright-decorators';
import { suite, test, slow, tag } from 'playwright-decorators';

@suite() // <-- Decorate class with @suite
class MyTestSuite {
@test()
async myTest({ page }) { // <-- Decorate test method with @test
@test() // <-- Decorate test method with @test
async myTest({ page }) {
// ...
}

@annotation(['team-x']) // <-- Add custom annotation to test
@slow('Processing a new user takes a long time') // <-- Mark test as "slow"
@tag(['team-x'])
@slow('Response from pasword reset service takes a long time')
@test()
async userCreation({ page }) {
async userShouldBeAbleToResetPassword({ page }) {
// ...
}

Expand Down
26 changes: 23 additions & 3 deletions lib/errors.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
export class NotSuiteDecoratedMethodError extends Error {
constructor(decoratorName: string ,method: any) {
super(`${decoratorName} decorator can be applied only to methods decorated by @suite. ${method?.name} is not decorated by @suite`);
super(`
The @${decoratorName} decorator can only be used on class that also have the @suite decorator.
Make sure ${method?.name} is marked with @suite, and that ${decoratorName} comes before @suite, like this:
@${decoratorName}
@suite()
${method?.name}() {}`);
}
}

export class NotTestDecoratedMethodError extends Error {
constructor(decoratorName: string, method: any) {
super(`${decoratorName} decorator can be applied only to methods decorated by @test. ${method?.name} is not decorated by @test`);
super(`
The @${decoratorName} decorator can only be used on methods that also have the @test decorator.
Make sure ${method?.name} is marked with @test, and that ${decoratorName} comes before @test, like this:
@${decoratorName}
@test()
${method?.name}() {}`
);
}
}

export class NotSuiteOrTestDecoratedMethodError extends Error {
constructor(decoratorName: string, method: any) {
super(`${decoratorName} decorator can be applied only to methods decorated by @test or classes decorated by @suite. ${method?.name} is not decorated by any of them`);
super(`
The @${decoratorName} decorator can only be used on classes/methods that also have the @suite or @test decorator.
Make sure ${method?.name} is marked with @suite or @test, and that ${decoratorName} comes before @suite or @test, like this:
@${decoratorName}
@suite() / @test()
${method?.name}() {}
`);
}
}
6 changes: 3 additions & 3 deletions lib/suite.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ export class SuiteDecorator implements SuiteDecoratorOptions {
return;
}

if (typeof this.skip === 'string') {
return playwright.slow(true, this.skip);
if (typeof this.slow === 'string') {
return playwright.slow(true, this.slow);
}

return playwright.slow();
}

Expand Down
10 changes: 5 additions & 5 deletions lib/test.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ export class TestDecorator implements TestDecoratorOptions {
if (this.slow === false) {
return;
}
if (typeof this.skip === 'string') {
return playwright.slow(true, this.skip);

if (typeof this.slow === 'string') {
return playwright.slow(true, this.slow);
}

return playwright.slow();
}

Expand Down Expand Up @@ -125,11 +125,11 @@ export class TestDecorator implements TestDecoratorOptions {
*/
run(executionContext: any) {
const decoratedTest: TestDecoratorFunction = (testFunction) => async (...args) => {
this.handleAnnotations();
this.handleSkip();
this.handleSlow();
this.handleFail();
this.handleFixme();
this.handleAnnotations();
await this.handleBeforeTestHooks();

// set correct executionContext (test class)
Expand Down

0 comments on commit 7398cc2

Please sign in to comment.