Skip to content

Commit

Permalink
fix: handle no assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ma11hewThomas committed Aug 6, 2024
1 parent 1c27b3c commit 07388c7
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 27 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "newman-reporter-ctrf-json",
"version": "0.0.3",
"version": "0.0.4",
"description": "",
"main": "dist/index.js",
"scripts": {
Expand Down
77 changes: 57 additions & 20 deletions src/generate-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ interface ReporterConfigOptions {
ctrfJsonOsVersion?: string
ctrfJsonBuildName?: string
ctrfJsonBuildNumber?: string
ctrfJsonBuildUrl?: string
ctrfJsonRepositoryName?: string
ctrfJsonRepositoryUrl?: string
ctrfJsonBranchName?: string
ctrfJsonTestEnvironment?: string
}

class GenerateCtrfReport {
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions types/ctrf.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<string, any>
}

Expand Down

0 comments on commit 07388c7

Please sign in to comment.