From 9cfdc62404b0b5ac8f511579abd79a669c59b4e9 Mon Sep 17 00:00:00 2001 From: Stephen Kilbourn Date: Fri, 30 Sep 2022 12:03:13 -0500 Subject: [PATCH] allow custom reports (#1) * allow custom reports --- README.md | 51 ++++++++++++++++++++++++++++++++++++++++++-- package.json | 2 +- playwright.config.ts | 3 ++- src/index.ts | 46 +++++++++++++++++++++++++-------------- 4 files changed, 82 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index c335a95..23dfd23 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,53 @@ Modify your `playwright.config.ts` file to include the reporter: The default output location will be to your root as `summary.txt` Including the `outputFile` parameter allows you to specify a custom report location. -## Customizing Outputs +## Default Output 📜 -Coming Soon +If you do not pass an outputFile option, then the summary will be generated to a `summary.txt` file in the following format: + +```txt +Total Tests in Suite: 30, +Total Tests Completed: 30, +Tests Passed: 30, +Tests Failed: 0, +Flaky Tests: 0, +Test Skipped: 0, +Test run was failure free? true, +Duration in ms: 234, +Average Test Duration:0, +Formatted Duration: 00:01 (mm:ss), +Formatted Avg Test Duration: 00:01 (mm:ss) +``` + +## Customizing Outputs 👨‍💻 + +To add a custom report leveraging your stats, create a function in the format: + +```typescript + stats, +) => ` +I completed ${stats.totalCompleted} Total Tests in ${stats.formattedDuration}.`; +``` + +and then modify your `playwright.config.ts` file with the following: + +```typescript +import type { PlaywrightTestConfig } from '@playwright/test'; +import { devices } from '@playwright/test'; + +const customReport = require('./customReport'); // Your custom report path and preferred name + + +const config: PlaywrightTestConfig = { + ... + reporter: [ + ['@skilbourn/playwright-report-summary', { outputFile: 'custom-summary.txt', inputTemplate: customReport }]] + ], + +``` + +this will generate a file such as : + +```txt +I completed 30 Total Tests in 00:01 (mm:ss). +``` diff --git a/package.json b/package.json index 662a1a1..ca05812 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@skilbourn/playwright-report-summary", - "version": "0.1.0", + "version": "0.1.1", "description": "generate a customizable text summary of your playwright test results", "main": "dist/src/index.js", "types": "dist/src/index.d.ts", diff --git a/playwright.config.ts b/playwright.config.ts index 2f0881a..5c3133a 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -4,6 +4,7 @@ import { devices } from '@playwright/test'; /** * See https://playwright.dev/docs/test-configuration. */ + const config: PlaywrightTestConfig = { testDir: './tests', /* Maximum time one test can run for. */ @@ -24,7 +25,7 @@ const config: PlaywrightTestConfig = { /* Opt out of parallel tests on CI. */ workers: process.env.CI ? 1 : undefined, /* Reporter to use. See https://playwright.dev/docs/test-reporters */ - reporter: [['html'], ['./src/index.ts', { outputFile: 'summary.txt' }]], + reporter: [['html'], ['./src/index.ts']], /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ use: { actionTimeout: 0, diff --git a/src/index.ts b/src/index.ts index 4bfef3c..31b9a9a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -41,8 +41,11 @@ class PlaywrightReportSummary implements Reporter { private outputFile: string; - constructor(options: { outputFile?: string } = {}) { + private inputTemplate: Function; + + constructor(options: { outputFile?: string; inputTemplate?: Function } = {}) { this.outputFile = options.outputFile; + this.inputTemplate = options.inputTemplate; } onBegin(config, suite) { @@ -65,24 +68,35 @@ class PlaywrightReportSummary implements Reporter { const avgTestTime = this.stats.duration / (this.stats.totalCompleted || 1); this.stats.formattedDuration = millisToMinuteSeconds(this.stats.duration); this.stats.formattedAvgTestDuration = millisToMinuteSeconds(avgTestTime); - outputReport(this.stats, this.outputFile); + outputReport(this.stats, this.inputTemplate, this.outputFile); } } -function outputReport(stats: Stats, outputFile = 'results.txt') { - const reportString = defaultReport( - stats.testsInSuite, - stats.totalCompleted, - stats.expectedResults, - stats.unexpectedResults, - stats.flakyTests, - stats.testMarkedSkipped, - stats.failureFree, - stats.duration, - stats.avgTestDuration, - stats.formattedDuration, - stats.formattedAvgTestDuration, - ); +function outputReport( + stats: Stats, + inputTemplate?: Function, + outputFile: string = 'results.txt', +) { + let reportString: string; + + if (typeof inputTemplate === 'undefined') { + reportString = defaultReport( + stats.testsInSuite, + stats.totalCompleted, + stats.expectedResults, + stats.unexpectedResults, + stats.flakyTests, + stats.testMarkedSkipped, + stats.failureFree, + stats.duration, + stats.avgTestDuration, + stats.formattedDuration, + stats.formattedAvgTestDuration, + ); + } else { + reportString = inputTemplate(stats); + } + fs.mkdirSync(path.dirname(outputFile), { recursive: true }); fs.writeFileSync(outputFile, reportString); }