From bc706859ad230903ea751ad6d0ca225354fc76ac Mon Sep 17 00:00:00 2001 From: Michal Dorner Date: Thu, 12 Nov 2020 23:34:42 +0100 Subject: [PATCH] Implements jest-junit report parsing --- .../__snapshots__/jest-junit.test.ts.snap | 40 ++++++ __tests__/fixtures/jest-junit.xml | 45 +++---- __tests__/jest-junit.test.ts | 17 +++ __tests__/main.test.ts | 3 - package-lock.json | 70 ++++++++-- package.json | 7 +- src/parsers/jest-junit/jest-junit-parser.ts | 121 ++++++++++++++++++ src/parsers/jest-junit/jest-junit-types.ts | 38 ++++++ src/parsers/test-parser.ts | 10 ++ src/utils/markdown-utils.ts | 52 ++++++++ src/utils/xml-utils.ts | 12 ++ 11 files changed, 381 insertions(+), 34 deletions(-) create mode 100644 __tests__/__snapshots__/jest-junit.test.ts.snap create mode 100644 __tests__/jest-junit.test.ts delete mode 100644 __tests__/main.test.ts create mode 100644 src/parsers/jest-junit/jest-junit-parser.ts create mode 100644 src/parsers/jest-junit/jest-junit-types.ts create mode 100644 src/parsers/test-parser.ts create mode 100644 src/utils/markdown-utils.ts create mode 100644 src/utils/xml-utils.ts diff --git a/__tests__/__snapshots__/jest-junit.test.ts.snap b/__tests__/__snapshots__/jest-junit.test.ts.snap new file mode 100644 index 0000000..1622191 --- /dev/null +++ b/__tests__/__snapshots__/jest-junit.test.ts.snap @@ -0,0 +1,40 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`jest-junit tests matches report snapshot 1`] = ` +"# jest tests ❌ +**6** tests were completed in **1.360s** with **1** passed, **1** skipped and **4** failed. +| Result | Suite | Tests | Time | Passed ✔️ | Failed ❌ | Skipped ✖️ | +| :---: | :--- | ---: | ---: | ---: | ---: | ---: | +| ❌ | [__tests__\\\\main.test.js](#testsmaintestjs-) | 4 | 0.486s | 1 | 3 | 0 | +| ❌ | [__tests__\\\\second.test.js](#testssecondtestjs-) | 2 | 0.082s | 0 | 1 | 1 | +## Test Suites + +### __tests__\\\\main.test.js ❌ + +#### Test 1 + +| Result | Test | Time | Details | +| :---: | :--- | ---: | --- | +| ✔️ | Passing test | 1ms | | + +#### Test 1 › Test 1.1 + +| Result | Test | Time | Details | +| :---: | :--- | ---: | --- | +| ❌ | Failing test | 2ms |
Error: expect(received).toBeTruthy()
Received: false
at Object. (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\__tests__\\\\main.test.js:10:21)
at Object.asyncJestTest (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-jasmine2\\\\build\\\\jasmineAsyncInstall.js:106:37)
at C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-jasmine2\\\\build\\\\queueRunner.js:45:12
at new Promise ()
at mapper (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-jasmine2\\\\build\\\\queueRunner.js:28:19)
at C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-jasmine2\\\\build\\\\queueRunner.js:75:41
at processTicksAndRejections (internal/process/task_queues.js:97:5)
| +| ❌ | Exception in target unit | 0ms |
Error: Some error
    at Object.throwError (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\lib\\\\main.js:2:9)
at Object. (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\__tests__\\\\main.test.js:14:11)
at Object.asyncJestTest (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-jasmine2\\\\build\\\\jasmineAsyncInstall.js:106:37)
at C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-jasmine2\\\\build\\\\queueRunner.js:45:12
at new Promise ()
at mapper (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-jasmine2\\\\build\\\\queueRunner.js:28:19)
at C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-jasmine2\\\\build\\\\queueRunner.js:75:41
at processTicksAndRejections (internal/process/task_queues.js:97:5)
| + +#### Test 2 + +| Result | Test | Time | Details | +| :---: | :--- | ---: | --- | +| ❌ | Exception in test | 0ms |
Error: Some error
    at Object. (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\__tests__\\\\main.test.js:21:11)
at Object.asyncJestTest (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-jasmine2\\\\build\\\\jasmineAsyncInstall.js:106:37)
at C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-jasmine2\\\\build\\\\queueRunner.js:45:12
at new Promise ()
at mapper (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-jasmine2\\\\build\\\\queueRunner.js:28:19)
at C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-jasmine2\\\\build\\\\queueRunner.js:75:41
at processTicksAndRejections (internal/process/task_queues.js:97:5)
| + +### __tests__\\\\second.test.js ❌ + +| Result | Test | Time | Details | +| :---: | :--- | ---: | --- | +| ❌ | Timeout test | 4ms |
: Timeout - Async callback was not invoked within the 1 ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 1 ms timeout specified by jest.setTimeout.Error:
    at new Spec (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-jasmine2\\\\build\\\\jasmine\\\\Spec.js:116:22)
at new Spec (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-jasmine2\\\\build\\\\setup_jest_globals.js:78:9)
at specFactory (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-jasmine2\\\\build\\\\jasmine\\\\Env.js:523:24)
at Env.it (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-jasmine2\\\\build\\\\jasmine\\\\Env.js:592:24)
at Env.it (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-jasmine2\\\\build\\\\jasmineAsyncInstall.js:134:23)
at it (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-jasmine2\\\\build\\\\jasmine\\\\jasmineLight.js:100:21)
at Object. (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\__tests__\\\\second.test.js:1:34)
at Runtime._execModule (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-runtime\\\\build\\\\index.js:1245:24)
at Runtime._loadModule (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-runtime\\\\build\\\\index.js:844:12)
at Runtime.requireModule (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-runtime\\\\build\\\\index.js:694:10)
at jasmine2 (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-jasmine2\\\\build\\\\index.js:230:13)
at runTestInternal (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-runner\\\\build\\\\runTest.js:380:22)
at runTest (C:\\\\Users\\\\Michal\\\\Workspace\\\\dorny\\\\test-check\\\\reports\\\\jest\\\\node_modules\\\\jest-runner\\\\build\\\\runTest.js:472:34)
| +| ✖️ | Skipped test | 0ms | Skipped | +" +`; diff --git a/__tests__/fixtures/jest-junit.xml b/__tests__/fixtures/jest-junit.xml index 2239f11..94513d7 100644 --- a/__tests__/fixtures/jest-junit.xml +++ b/__tests__/fixtures/jest-junit.xml @@ -1,44 +1,44 @@ - - - + + + - + Error: expect(received).toBeTruthy() Received: false - at Object.test (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\__tests__\main.test.js:10:21) + at Object.<anonymous> (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\__tests__\main.test.js:10:21) at Object.asyncJestTest (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-jasmine2\build\jasmineAsyncInstall.js:106:37) - at resolve (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-jasmine2\build\queueRunner.js:45:12) + at C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-jasmine2\build\queueRunner.js:45:12 at new Promise (<anonymous>) at mapper (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-jasmine2\build\queueRunner.js:28:19) - at promise.then (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-jasmine2\build\queueRunner.js:75:41) - at process._tickCallback (internal/process/next_tick.js:68:7) + at C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-jasmine2\build\queueRunner.js:75:41 + at processTicksAndRejections (internal/process/task_queues.js:97:5) - + Error: Some error at Object.throwError (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\lib\main.js:2:9) - at Object.test (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\__tests__\main.test.js:14:11) + at Object.<anonymous> (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\__tests__\main.test.js:14:11) at Object.asyncJestTest (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-jasmine2\build\jasmineAsyncInstall.js:106:37) - at resolve (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-jasmine2\build\queueRunner.js:45:12) + at C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-jasmine2\build\queueRunner.js:45:12 at new Promise (<anonymous>) at mapper (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-jasmine2\build\queueRunner.js:28:19) - at promise.then (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-jasmine2\build\queueRunner.js:75:41) - at process._tickCallback (internal/process/next_tick.js:68:7) + at C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-jasmine2\build\queueRunner.js:75:41 + at processTicksAndRejections (internal/process/task_queues.js:97:5) - + Error: Some error - at Object.test (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\__tests__\main.test.js:21:11) + at Object.<anonymous> (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\__tests__\main.test.js:21:11) at Object.asyncJestTest (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-jasmine2\build\jasmineAsyncInstall.js:106:37) - at resolve (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-jasmine2\build\queueRunner.js:45:12) + at C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-jasmine2\build\queueRunner.js:45:12 at new Promise (<anonymous>) at mapper (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-jasmine2\build\queueRunner.js:28:19) - at promise.then (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-jasmine2\build\queueRunner.js:75:41) - at process._tickCallback (internal/process/next_tick.js:68:7) + at C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-jasmine2\build\queueRunner.js:75:41 + at processTicksAndRejections (internal/process/task_queues.js:97:5) - - + + : Timeout - Async callback was not invoked within the 1 ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 1 ms timeout specified by jest.setTimeout.Error: at new Spec (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-jasmine2\build\jasmine\Spec.js:116:22) at new Spec (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-jasmine2\build\setup_jest_globals.js:78:9) @@ -51,9 +51,10 @@ Received: false at Runtime._loadModule (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-runtime\build\index.js:844:12) at Runtime.requireModule (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-runtime\build\index.js:694:10) at jasmine2 (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-jasmine2\build\index.js:230:13) - at runTestInternal (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-runner\build\runTest.js:380:22) + at runTestInternal (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-runner\build\runTest.js:380:22) + at runTest (C:\Users\Michal\Workspace\dorny\test-check\reports\jest\node_modules\jest-runner\build\runTest.js:472:34) - + diff --git a/__tests__/jest-junit.test.ts b/__tests__/jest-junit.test.ts new file mode 100644 index 0000000..f2cd21f --- /dev/null +++ b/__tests__/jest-junit.test.ts @@ -0,0 +1,17 @@ +import * as fs from 'fs' +import * as path from 'path' + +import {parseJestJunit} from '../src/parsers/jest-junit/jest-junit-parser' + +const xmlFixture = fs.readFileSync(path.join(__dirname, 'fixtures', 'jest-junit.xml'), {encoding: 'utf8'}) +const outputPath = __dirname + '/__outputs__/jest-junit.md' + +describe('jest-junit tests', () => { + it('matches report snapshot', async () => { + const result = await parseJestJunit(xmlFixture) + fs.writeFileSync(outputPath, result?.output?.summary ?? '') + + expect(result.success).toBeFalsy() + expect(result?.output?.summary).toMatchSnapshot() + }) +}) diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts deleted file mode 100644 index d0d954b..0000000 --- a/__tests__/main.test.ts +++ /dev/null @@ -1,3 +0,0 @@ -test('TODO', async () => { - await expect(true).toBeTruthy() -}) diff --git a/package-lock.json b/package-lock.json index 9ca65af..22f4a72 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1363,6 +1363,15 @@ "fastq": "^1.6.0" } }, + "@octokit/types": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/@octokit/types/-/types-5.5.0.tgz", + "integrity": "sha512-UZ1pErDue6bZNjYOotCNveTXArOMZQFG6hKJfOnGnulVCMcVVi7YIIuuR4WfBhjo7zgpmzn/BkPDnUXtNx+PcQ==", + "dev": true, + "requires": { + "@types/node": ">= 8" + } + }, "@sinonjs/commons": { "version": "1.8.1", "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-1.8.1.tgz", @@ -1422,6 +1431,12 @@ "@babel/types": "^7.3.0" } }, + "@types/github-slugger": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/@types/github-slugger/-/github-slugger-1.3.0.tgz", + "integrity": "sha512-J/rMZa7RqiH/rT29TEVZO4nBoDP9XJOjnbbIofg7GQKs4JIduEO3WLpte+6WeUz/TcrXKlY+bM7FYrp8yFB+3g==", + "dev": true + }, "@types/graceful-fs": { "version": "4.1.4", "resolved": "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.4.tgz", @@ -1632,6 +1647,15 @@ "integrity": "sha512-l42BggppR6zLmpfU6fq9HEa2oGPEI8yrSPL3GITjfRInppYFahObbIQOQK3UGxEnyQpltZLaPe75046NOZQikw==", "dev": true }, + "@types/xml2js": { + "version": "0.4.5", + "resolved": "https://registry.npmjs.org/@types/xml2js/-/xml2js-0.4.5.tgz", + "integrity": "sha512-yohU3zMn0fkhlape1nxXG2bLEGZRc1FeqF80RoHaYXJN7uibaauXfhzhOJr1Xh36sn+/tx21QAOf07b/xYVk1w==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/yargs": { "version": "13.0.3", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-13.0.3.tgz", @@ -4492,6 +4516,21 @@ "assert-plus": "^1.0.0" } }, + "github-slugger": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/github-slugger/-/github-slugger-1.3.0.tgz", + "integrity": "sha512-gwJScWVNhFYSRDvURk/8yhcFBee6aFjye2a7Lhb2bUyRulpIoek9p0I9Kt7PT67d/nUlZbFu8L9RLiA0woQN8Q==", + "requires": { + "emoji-regex": ">=6.0.0 <=6.1.1" + }, + "dependencies": { + "emoji-regex": { + "version": "6.1.1", + "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-6.1.1.tgz", + "integrity": "sha1-xs0OwbBkLio8Z6ETfvxeeW2k+I4=" + } + } + }, "glob": { "version": "7.1.6", "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz", @@ -4645,12 +4684,6 @@ "whatwg-encoding": "^1.0.1" } }, - "html-escaper": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-2.0.2.tgz", - "integrity": "sha512-H2iMtd0I4Mt5eYiapRdIDjp+XzelXQ0tFE4JS7YFwFevXXMmOp9myNrUvCg0D6ws8iqkRPBfKHgbwig1SmlLfg==", - "dev": true - }, "http-signature": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/http-signature/-/http-signature-1.2.0.tgz", @@ -5100,6 +5133,14 @@ "dev": true, "requires": { "html-escaper": "^2.0.0" + }, + "dependencies": { + "html-escaper": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-2.0.2.tgz", + "integrity": "sha512-H2iMtd0I4Mt5eYiapRdIDjp+XzelXQ0tFE4JS7YFwFevXXMmOp9myNrUvCg0D6ws8iqkRPBfKHgbwig1SmlLfg==", + "dev": true + } } }, "jest": { @@ -8323,8 +8364,7 @@ "sax": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz", - "integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==", - "dev": true + "integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==" }, "saxes": { "version": "5.0.1", @@ -9391,6 +9431,20 @@ "integrity": "sha512-A5CUptxDsvxKJEU3yO6DuWBSJz/qizqzJKOMIfUJHETbBw/sFaDxgd6fxm1ewUaM0jZ444Fc5vC5ROYurg/4Pw==", "dev": true }, + "xml2js": { + "version": "0.4.23", + "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.23.tgz", + "integrity": "sha512-ySPiMjM0+pLDftHgXY4By0uswI3SPKLDw/i3UXbnO8M/p28zqexCUoPmQFrYD+/1BzhGJSs2i1ERWKJAtiLrug==", + "requires": { + "sax": ">=0.6.0", + "xmlbuilder": "~11.0.0" + } + }, + "xmlbuilder": { + "version": "11.0.1", + "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-11.0.1.tgz", + "integrity": "sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA==" + }, "xmlchars": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/xmlchars/-/xmlchars-2.2.0.tgz", diff --git a/package.json b/package.json index 0c3eb63..cc27ef0 100644 --- a/package.json +++ b/package.json @@ -29,11 +29,16 @@ "author": "Michal Dorner ", "license": "MIT", "dependencies": { - "@actions/core": "^1.2.6" + "@actions/core": "^1.2.6", + "github-slugger": "^1.3.0", + "xml2js": "^0.4.23" }, "devDependencies": { + "@octokit/types": "^5.5.0", + "@types/github-slugger": "^1.3.0", "@types/jest": "^26.0.15", "@types/node": "^14.14.6", + "@types/xml2js": "^0.4.5", "@typescript-eslint/eslint-plugin": "^4.7.0", "@typescript-eslint/parser": "^4.7.0", "@vercel/ncc": "^0.24.1", diff --git a/src/parsers/jest-junit/jest-junit-parser.ts b/src/parsers/jest-junit/jest-junit-parser.ts new file mode 100644 index 0000000..fe6f507 --- /dev/null +++ b/src/parsers/jest-junit/jest-junit-parser.ts @@ -0,0 +1,121 @@ +import {TestResult} from '../test-parser' +import {parseStringPromise} from 'xml2js' +import GithubSlugger from 'github-slugger' + +import {JunitReport, TestCase, TestSuite, TestSuites} from './jest-junit-types' +import {Align, Icon, link, table, exceptionCell} from '../../utils/markdown-utils' +import {parseAttribute} from '../../utils/xml-utils' + +export async function parseJestJunit(content: string): Promise { + const junit = (await parseStringPromise(content, { + attrValueProcessors: [parseAttribute] + })) as JunitReport + const testsuites = junit.testsuites + + const slugger = new GithubSlugger() + const success = !(testsuites.$?.failures > 0 || testsuites.$?.errors > 0) + + return { + success, + output: { + title: junit.testsuites.$.name, + summary: getSummary(success, junit, slugger) + } + } +} + +function getSummary(success: boolean, junit: JunitReport, slugger: GithubSlugger): string { + const stats = junit.testsuites.$ + + const icon = success ? Icon.success : Icon.fail + const time = `${stats.time.toFixed(3)}s` + + const skipped = getSkippedCount(junit.testsuites) + const failed = stats.errors + stats.failures + const passed = stats.tests - failed - skipped + + const heading = `# ${stats.name} ${icon}` + const headingLine = `**${stats.tests}** tests were completed in **${time}** with **${passed}** passed, **${skipped}** skipped and **${failed}** failed.` + + const suitesSummary = junit.testsuites.testsuite.map(ts => { + const skip = ts.$.skipped + const fail = ts.$.errors + ts.$.failures + const pass = ts.$.tests - fail - skip + const tm = `${ts.$.time.toFixed(3)}s` + const result = success ? Icon.success : Icon.fail + const slug = slugger.slug(`${ts.$.name} ${result}`).replace(/_/g, '') + const tsAddr = `#${slug}` + const name = link(ts.$.name, tsAddr) + return [result, name, ts.$.tests, tm, pass, fail, skip] + }) + + const summary = table( + ['Result', 'Suite', 'Tests', 'Time', `Passed ${Icon.success}`, `Failed ${Icon.fail}`, `Skipped ${Icon.skip}`], + [Align.Center, Align.Left, Align.Right, Align.Right, Align.Right, Align.Right, Align.Right], + ...suitesSummary + ) + + const suites = junit.testsuites?.testsuite?.map(ts => getSuiteSummary(ts)).join('\n') + const suitesSection = `## Test Suites\n\n${suites}` + + return `${heading}\n${headingLine}\n${summary}\n${suitesSection}` +} + +function getSkippedCount(suites: TestSuites): number { + return suites.testsuite.reduce((sum, suite) => sum + suite.$.skipped, 0) +} + +function getSuiteSummary(suite: TestSuite): string { + const success = !(suite.$?.failures > 0 || suite.$?.errors > 0) + const icon = success ? Icon.success : Icon.fail + + const groups: {describe: string; tests: TestCase[]}[] = [] + for (const tc of suite.testcase) { + let grp = groups.find(g => g.describe === tc.$.classname) + if (grp === undefined) { + grp = {describe: tc.$.classname, tests: []} + groups.push(grp) + } + grp.tests.push(tc) + } + + const content = groups + .map(grp => { + const header = grp.describe !== '' ? `#### ${grp.describe}\n\n` : '' + const tests = table( + ['Result', 'Test', 'Time', 'Details'], + [Align.Center, Align.Left, Align.Right, Align.None], + ...grp.tests.map(tc => { + const name = tc.$.name + const time = `${Math.round(tc.$.time * 1000)}ms` + const result = getTestCaseIcon(tc) + const ex = getTestCaseDetails(tc) + return [result, name, time, ex] + }) + ) + + return `${header}${tests}\n` + }) + .join('\n') + + return `### ${suite.$.name} ${icon}\n\n${content}` +} + +function getTestCaseIcon(test: TestCase): string { + if (test.failure) return Icon.fail + if (test.skipped) return Icon.skip + return Icon.success +} + +function getTestCaseDetails(test: TestCase): string { + if (test.skipped !== undefined) { + return 'Skipped' + } + + if (test.failure !== undefined) { + const failure = test.failure.join('\n') + return exceptionCell(failure) + } + + return '' +} diff --git a/src/parsers/jest-junit/jest-junit-types.ts b/src/parsers/jest-junit/jest-junit-types.ts new file mode 100644 index 0000000..b5aa211 --- /dev/null +++ b/src/parsers/jest-junit/jest-junit-types.ts @@ -0,0 +1,38 @@ +export interface JunitReport { + testsuites: TestSuites +} + +export interface TestSuites { + $: { + name: string + tests: number + failures: number // assertion failed + errors: number // unhandled exception during test execution + time: number + } + testsuite: TestSuite[] +} + +export interface TestSuite { + $: { + name: string + tests: number + errors: number + failures: number + skipped: number + time: number + timestamp?: Date + } + testcase: TestCase[] +} + +export interface TestCase { + $: { + classname: string + file?: string + name: string + time: number + } + failure?: string[] + skipped?: string[] +} diff --git a/src/parsers/test-parser.ts b/src/parsers/test-parser.ts new file mode 100644 index 0000000..f441bf1 --- /dev/null +++ b/src/parsers/test-parser.ts @@ -0,0 +1,10 @@ +import {Endpoints} from '@octokit/types' + +type OutputParameters = Endpoints['POST /repos/:owner/:repo/check-runs']['parameters']['output'] + +export type ParseTestResult = (content: string) => Promise + +export interface TestResult { + success: boolean + output: OutputParameters +} diff --git a/src/utils/markdown-utils.ts b/src/utils/markdown-utils.ts new file mode 100644 index 0000000..a13d76f --- /dev/null +++ b/src/utils/markdown-utils.ts @@ -0,0 +1,52 @@ +export enum Align { + Left = ':---', + Center = ':---:', + Right = '---:', + None = '---' +} + +export const Icon = { + skip: '✖️', // ':heavy_multiplication_x:' + success: '✔️', // ':heavy_check_mark:' + fail: '❌' // ':x:' +} + +export function details(summary: string, content: string): string { + return `
${summary}${content}
` +} + +export function link(title: string, address: string): string { + return `[${title}](${address})` +} + +type ToString = string | number | boolean | Date +export function table(headers: ToString[], align: ToString[], ...rows: ToString[][]): string { + const headerRow = `| ${headers.join(' | ')} |` + const alignRow = `| ${align.join(' | ')} |` + const contentRows = rows.map(row => `| ${row.join(' | ')} |`).join('\n') + return [headerRow, alignRow, contentRows].join('\n') +} + +export function exceptionCell(ex: string): string { + const lines = ex.split(/\r?\n/) + if (lines.length === 0) { + return '' + } + + const summary = tableEscape(lines.shift()?.trim() || '') + const emptyLine = /^\s*$/ + const firstNonEmptyLine = lines.findIndex(l => !emptyLine.test(l)) + + if (firstNonEmptyLine === -1) { + return summary + } + + const contentLines = firstNonEmptyLine > 0 ? lines.slice(firstNonEmptyLine) : lines + + const content = '
' + tableEscape(contentLines.join('
')) + '
' + return details(summary, content) +} + +export function tableEscape(content: string): string { + return content.replace('|', '\\|') +} diff --git a/src/utils/xml-utils.ts b/src/utils/xml-utils.ts new file mode 100644 index 0000000..8d7b33c --- /dev/null +++ b/src/utils/xml-utils.ts @@ -0,0 +1,12 @@ +export function parseAttribute(str: string | undefined): string | number | undefined { + if (str === '' || str === undefined) { + return str + } + + const num = parseFloat(str) + if (isNaN(num)) { + return str + } + + return num +}