From 07388c7b4cb869060a2827a6ef7ac632deafb534 Mon Sep 17 00:00:00 2001 From: Matthew Thomas Date: Tue, 6 Aug 2024 19:12:15 +0100 Subject: [PATCH] fix: handle no assertions --- README.md | 7 +++- package-lock.json | 4 +-- package.json | 2 +- src/generate-report.ts | 77 +++++++++++++++++++++++++++++++----------- types/ctrf.d.ts | 10 ++++-- 5 files changed, 73 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index a1bc522..50f9873 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,12 @@ newman run ./postman_collection.json -r ctrf-json \ --reporter-ctrf-json-os-release 18.04 \ --reporter-ctrf-json-os-version 5.4.0 \ --reporter-ctrf-json-build-name MyApp \ ---reporter-ctrf-json-build-number 100 +--reporter-ctrf-json-build-number 100 \ +--reporter-ctrf-json-build-url https://ctrf.io \ +--reporter-ctrf-json-repository-name ctrf \ +--reporter-ctrf-json-repository-url https://github.com/ctrf-io/newman-reporter-ctrf-json \ +--reporter-ctrf-json-branch-name main \ +--reporter-ctrf-json-test-environment staging ``` ## Test Object Properties diff --git a/package-lock.json b/package-lock.json index 382be66..66b9b09 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "newman-reporter-ctrf-json", - "version": "0.0.1", + "version": "0.0.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "newman-reporter-ctrf-json", - "version": "0.0.1", + "version": "0.0.4", "license": "ISC", "devDependencies": { "@types/newman": "^5.3.5", diff --git a/package.json b/package.json index cd5c3e9..6730e25 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "newman-reporter-ctrf-json", - "version": "0.0.3", + "version": "0.0.4", "description": "", "main": "dist/index.js", "scripts": { diff --git a/src/generate-report.ts b/src/generate-report.ts index 16814d5..b86d992 100644 --- a/src/generate-report.ts +++ b/src/generate-report.ts @@ -20,6 +20,11 @@ interface ReporterConfigOptions { ctrfJsonOsVersion?: string ctrfJsonBuildName?: string ctrfJsonBuildNumber?: string + ctrfJsonBuildUrl?: string + ctrfJsonRepositoryName?: string + ctrfJsonRepositoryUrl?: string + ctrfJsonBranchName?: string + ctrfJsonTestEnvironment?: string } class GenerateCtrfReport { @@ -50,6 +55,14 @@ class GenerateCtrfReport { ctrfJsonOsVersion: reporterOptions?.ctrfJsonOsVersion ?? undefined, ctrfJsonBuildName: reporterOptions?.ctrfJsonBuildName ?? undefined, ctrfJsonBuildNumber: reporterOptions?.ctrfJsonBuildNumber ?? undefined, + ctrfJsonBuildUrl: reporterOptions?.ctrfJsonBuildUrl ?? undefined, + ctrfJsonRepositoryName: + reporterOptions?.ctrfJsonRepositoryName ?? undefined, + ctrfJsonRepositoryUrl: + reporterOptions?.ctrfJsonRepositoryUrl ?? undefined, + ctrfJsonBranchName: reporterOptions?.ctrfJsonBranchName ?? undefined, + ctrfJsonTestEnvironment: + reporterOptions?.ctrfJsonTestEnvironment ?? undefined, } this.ctrfReport = { @@ -109,26 +122,29 @@ class GenerateCtrfReport { this.setFilename(this.reporterConfigOptions.ctrfJsonOutputFile) } summary.run.executions.forEach((execution) => { - execution.assertions.forEach((assertion) => { - this.ctrfReport.results.summary.tests += 1 - - const testResult = { - name: assertion.assertion, - status: - assertion.error != null - ? ('failed' as CtrfTestState) - : ('passed' as CtrfTestState), - duration: execution.response.responseTime, - } - - if (assertion.error != null) { - this.ctrfReport.results.summary.failed += 1 - } else { - this.ctrfReport.results.summary.passed += 1 - } - - this.ctrfReport.results.tests.push(testResult) - }) + // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions + if (execution.assertions) { + execution.assertions.forEach((assertion) => { + this.ctrfReport.results.summary.tests += 1 + + const testResult = { + name: assertion.assertion, + status: + assertion.error != null + ? ('failed' as CtrfTestState) + : ('passed' as CtrfTestState), + duration: execution.response.responseTime, + } + + if (assertion.error != null) { + this.ctrfReport.results.summary.failed += 1 + } else { + this.ctrfReport.results.summary.passed += 1 + } + + this.ctrfReport.results.tests.push(testResult) + }) + } }) this.ctrfReport.results.summary.stop = Date.now() this.writeReportToFile(this.ctrfReport) @@ -166,6 +182,27 @@ class GenerateCtrfReport { this.ctrfEnvironment.buildNumber = reporterConfigOptions.ctrfJsonBuildNumber } + if (reporterConfigOptions.ctrfJsonBuildUrl !== undefined) { + this.ctrfEnvironment.buildUrl = reporterConfigOptions.ctrfJsonBuildUrl + } + if (reporterConfigOptions.ctrfJsonBuildUrl !== undefined) { + this.ctrfEnvironment.buildUrl = reporterConfigOptions.ctrfJsonBuildUrl + } + if (reporterConfigOptions.ctrfJsonRepositoryName !== undefined) { + this.ctrfEnvironment.repositoryName = + reporterConfigOptions.ctrfJsonRepositoryName + } + if (reporterConfigOptions.ctrfJsonRepositoryUrl !== undefined) { + this.ctrfEnvironment.repositoryUrl = + reporterConfigOptions.ctrfJsonRepositoryUrl + } + if (reporterConfigOptions.ctrfJsonBranchName !== undefined) { + this.ctrfEnvironment.branchName = reporterConfigOptions.ctrfJsonBranchName + } + if (reporterConfigOptions.ctrfJsonTestEnvironment !== undefined) { + this.ctrfEnvironment.testEnvironment = + reporterConfigOptions.ctrfJsonTestEnvironment + } } hasEnvironmentDetails(environment: CtrfEnvironment): boolean { diff --git a/types/ctrf.d.ts b/types/ctrf.d.ts index 0bc5a4c..9d1bd75 100644 --- a/types/ctrf.d.ts +++ b/types/ctrf.d.ts @@ -36,9 +36,8 @@ export interface CtrfTest { tags?: string[] type?: string filePath?: string - retry?: number - flake?: boolean - attempts?: CtrfTest[] + retries?: number + flaky?: boolean browser?: string device?: string screenshot?: string @@ -55,6 +54,11 @@ export interface CtrfEnvironment { osVersion?: string buildName?: string buildNumber?: string + buildUrl?: string + repositoryName?: string + repositoryUrl?: string + branchName?: string + testEnvironment?: string extra?: Record }