-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: adds JSON reporter [sc-00] * docs: fix typo [sc-00] * feat: removes duration [sc-00] * fix: handle empty duration [sc-00] * feat: handle run errors [sc-00] * fix: commit snapshot [sc-00]
- Loading branch information
Showing
10 changed files
with
229 additions
and
5 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
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
83 changes: 83 additions & 0 deletions
83
packages/cli/src/reporters/__tests__/__snapshots__/json-builder.spec.ts.snap
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,83 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`JsonBuilder renders JSON markdown output with assets & links: json-with-assets-links 1`] = ` | ||
"{ | ||
"testSessionId": "0c4c64b3-79c5-44a6-ae07-b580ce73f328", | ||
"numChecks": 2, | ||
"runLocation": "eu-west-1", | ||
"checks": [ | ||
{ | ||
"result": "Pass", | ||
"name": "my-test.spec.ts", | ||
"checkType": "BROWSER", | ||
"durationMilliseconds": 6522, | ||
"filename": "src/__checks__/folder/browser.check.ts", | ||
"link": "https://app.checklyhq.com/test-sessions/0c4c64b3-79c5-44a6-ae07-b580ce73f328/results/702961fd-7e2c-45f0-97be-1aa9eabd4d82", | ||
"runError": "Run error" | ||
}, | ||
{ | ||
"result": "Pass", | ||
"name": "Test API check", | ||
"checkType": "API", | ||
"durationMilliseconds": 1234, | ||
"filename": "src/some-other-folder/api.check.ts", | ||
"link": "https://app.checklyhq.com/test-sessions/0c4c64b3-79c5-44a6-ae07-b580ce73f328/results/1c0be612-a5ec-432e-ac1c-837d2f70c010", | ||
"runError": "Run error" | ||
} | ||
] | ||
}" | ||
`; | ||
|
||
exports[`JsonBuilder renders basic JSON output with no assets & links: json-basic 1`] = ` | ||
"{ | ||
"numChecks": 2, | ||
"runLocation": "eu-west-1", | ||
"checks": [ | ||
{ | ||
"result": "Pass", | ||
"name": "my-test.spec.ts", | ||
"checkType": "BROWSER", | ||
"durationMilliseconds": 6522, | ||
"filename": "src/__checks__/folder/browser.check.ts", | ||
"link": null, | ||
"runError": null | ||
}, | ||
{ | ||
"result": "Pass", | ||
"name": "Test API check", | ||
"checkType": "API", | ||
"durationMilliseconds": 1234, | ||
"filename": "src/some-other-folder/api.check.ts", | ||
"link": null, | ||
"runError": null | ||
} | ||
] | ||
}" | ||
`; | ||
|
||
exports[`JsonBuilder renders basic JSON output with run errors: json-basic 1`] = ` | ||
"{ | ||
"numChecks": 2, | ||
"runLocation": "eu-west-1", | ||
"checks": [ | ||
{ | ||
"result": "Pass", | ||
"name": "my-test.spec.ts", | ||
"checkType": "BROWSER", | ||
"durationMilliseconds": 6522, | ||
"filename": "src/__checks__/folder/browser.check.ts", | ||
"link": null, | ||
"runError": "Run error" | ||
}, | ||
{ | ||
"result": "Pass", | ||
"name": "Test API check", | ||
"checkType": "API", | ||
"durationMilliseconds": 1234, | ||
"filename": "src/some-other-folder/api.check.ts", | ||
"link": null, | ||
"runError": "Run error" | ||
} | ||
] | ||
}" | ||
`; |
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 |
---|---|---|
|
@@ -135,4 +135,5 @@ export const apiCheckResult = { | |
requestError: null, | ||
}, | ||
scheduleError: '', | ||
runError: '', | ||
} |
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 |
---|---|---|
|
@@ -159,4 +159,5 @@ export const browserCheckResult = { | |
}, | ||
], | ||
scheduleError: '', | ||
runError: '', | ||
} |
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,37 @@ | ||
import { JsonBuilder } from '../json' | ||
import { generateMapAndTestResultIds } from './helpers' | ||
|
||
const testSessionId = '0c4c64b3-79c5-44a6-ae07-b580ce73f328' | ||
const runLocation = 'eu-west-1' | ||
describe('JsonBuilder', () => { | ||
test('renders basic JSON output with no assets & links', () => { | ||
const checkFilesMap = generateMapAndTestResultIds({ includeTestResultIds: false }) | ||
const json = new JsonBuilder({ | ||
testSessionId: undefined, | ||
numChecks: checkFilesMap.size, | ||
runLocation, | ||
checkFilesMap, | ||
}).render() | ||
expect(json).toMatchSnapshot('json-basic') | ||
}) | ||
test('renders basic JSON output with run errors', () => { | ||
const checkFilesMap = generateMapAndTestResultIds({ includeTestResultIds: false, includeRunErrors: true }) | ||
const json = new JsonBuilder({ | ||
testSessionId: undefined, | ||
numChecks: checkFilesMap.size, | ||
runLocation, | ||
checkFilesMap, | ||
}).render() | ||
expect(json).toMatchSnapshot('json-basic') | ||
}) | ||
test('renders JSON markdown output with assets & links', () => { | ||
const checkFilesMap = generateMapAndTestResultIds({ includeTestResultIds: true }) | ||
const json = new JsonBuilder({ | ||
testSessionId, | ||
numChecks: checkFilesMap.size, | ||
runLocation, | ||
checkFilesMap, | ||
}).render() | ||
expect(json).toMatchSnapshot('json-with-assets-links') | ||
}) | ||
}) |
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,95 @@ | ||
import * as fs from 'fs' | ||
import * as path from 'path' | ||
|
||
import AbstractListReporter, { checkFilesMap } from './abstract-list' | ||
import { CheckRunId } from '../services/abstract-check-runner' | ||
import { printLn, getTestSessionUrl } from './util' | ||
|
||
const outputFile = './checkly-json-report.json' | ||
|
||
type JsonBuilderOptions = { | ||
testSessionId?: string | ||
numChecks: number | ||
runLocation: string | ||
checkFilesMap: checkFilesMap | ||
} | ||
|
||
export class JsonBuilder { | ||
testSessionId?: string | ||
numChecks: number | ||
runLocation: string | ||
checkFilesMap: checkFilesMap | ||
hasFilenames: boolean | ||
|
||
constructor (options: JsonBuilderOptions) { | ||
this.testSessionId = options.testSessionId | ||
this.numChecks = options.numChecks | ||
this.runLocation = options.runLocation | ||
this.checkFilesMap = options.checkFilesMap | ||
this.hasFilenames = !(options.checkFilesMap.size === 1 && options.checkFilesMap.has(undefined)) | ||
} | ||
|
||
render (): string { | ||
const testSessionSummary: any = { | ||
testSessionId: this.testSessionId, | ||
numChecks: this.numChecks, | ||
runLocation: this.runLocation, | ||
checks: [], | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
for (const [_, checkMap] of this.checkFilesMap.entries()) { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
for (const [_, { result, testResultId }] of checkMap.entries()) { | ||
const check: any = { | ||
result: result.hasFailures ? 'Fail' : 'Pass', | ||
name: result.name, | ||
checkType: result.checkType, | ||
durationMilliseconds: result.responseTime ?? null, | ||
filename: null, | ||
link: null, | ||
runError: result.runError || null, | ||
} | ||
|
||
if (this.hasFilenames) { | ||
check.filename = result.sourceFile | ||
} | ||
|
||
if (this.testSessionId && testResultId) { | ||
check.link = `${getTestSessionUrl(this.testSessionId)}/results/${testResultId}` | ||
} | ||
|
||
testSessionSummary.checks.push(check) | ||
} | ||
} | ||
|
||
return JSON.stringify(testSessionSummary, null, 2) | ||
} | ||
} | ||
|
||
export default class JsonReporter extends AbstractListReporter { | ||
onBegin (checks: Array<{ check: any, checkRunId: CheckRunId, testResultId?: string }>, testSessionId?: string) { | ||
super.onBegin(checks, testSessionId) | ||
printLn(`Running ${this.numChecks} checks in ${this._runLocationString()}.`, 2, 1) | ||
} | ||
|
||
onEnd () { | ||
this._printBriefSummary() | ||
const jsonBuilder = new JsonBuilder({ | ||
testSessionId: this.testSessionId, | ||
numChecks: this.numChecks!, | ||
runLocation: this._runLocationString(), | ||
checkFilesMap: this.checkFilesMap!, | ||
}) | ||
|
||
const json = jsonBuilder.render() | ||
|
||
const summaryFilename = process.env.CHECKLY_REPORTER_JSON_OUTPUT ?? outputFile | ||
fs.mkdirSync(path.resolve(path.dirname(summaryFilename)), { recursive: true }) | ||
fs.writeFileSync(summaryFilename, json) | ||
|
||
printLn(`JSON report saved in '${path.resolve(summaryFilename)}'.`, 2) | ||
|
||
this._printTestSessionsUrl() | ||
} | ||
} |
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