Skip to content

Commit

Permalink
Merge pull request #4 from phoenix-actions/feature/PR-improvements-fr…
Browse files Browse the repository at this point in the history
…om-external

Feature/pr improvements from external
  • Loading branch information
IanMoroney authored Feb 22, 2022
2 parents f4afdce + 84d1149 commit 7de269e
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 17 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ Configuration of `uniqueOutputName`, `suiteNameTemplate`, `classNameTemplate`, `
<summary>mocha-json</summary>

[Mocha](https://mochajs.org/) testing framework support requires:
- Mocha version [v7.2.0](https://github.com/mochajs/mocha/releases/tag/v7.2.0) or higher
- Mocha version [v9.1.0](https://github.com/mochajs/mocha/releases/tag/v9.1.0) or higher
- Usage of [json](https://mochajs.org/#json) reporter.

You can use the following example configuration in `package.json`:
Expand All @@ -297,6 +297,15 @@ You can use the following example configuration in `package.json`:
}
```

For Mocha < v9.1, the command should look like this:
```json
"scripts": {
"test": "mocha --reporter json > test-results.json"
}
```
Additionally, test processing might fail if any of your tests write anything on standard output.
Before version [v9.1.0](https://github.com/mochajs/mocha/releases/tag/v9.1.0), Mocha doesn't have the option to store `json` output directly to the file, and we have to rely on redirecting its standard output ([mocha#4607](https://github.com/mochajs/mocha/pull/4607)).
Please update Mocha to version [v9.1.0](https://github.com/mochajs/mocha/releases/tag/v9.1.0) or above if you encounter this issue.
</details>

<details>
Expand All @@ -323,7 +332,7 @@ Unfortunately, there are some known issues and limitations caused by GitHub API:
- Test report can't reference any additional files (e.g. screenshots). You can use `actions/upload-artifact@v2` to upload them and inspect them manually.
- Check Runs are created for specific commit SHA. It's not possible to specify under which workflow test report should belong if more
workflows are running for the same SHA. Thanks to this GitHub "feature" it's possible your test report will appear in an unexpected place in GitHub UI.
For more information, see [#67](https://github.com/phoenix-actions/test-reporting/issues/67).
For more information, see [#67](https://github.com/dorny/test-reporter/issues/67).

## See also
- [paths-filter](https://github.com/dorny/paths-filter) - Conditionally run actions based on files modified by PR, feature branch, or pushed commits
Expand Down
11 changes: 11 additions & 0 deletions __tests__/__outputs__/jest-junit-eslint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
![Tests passed successfully](https://img.shields.io/badge/tests-1%20passed-success)
## ✔️ <a id="user-content-r0" href="#r0">fixtures/jest-junit-eslint.xml</a>
**1** tests were completed in **0ms** with **1** passed, **0** failed and **0** skipped.
|Test suite|Passed|Failed|Skipped|Time|
|:---|---:|---:|---:|---:|
|[test.jsx](#r0s0)|1✔️|||0ms|
### ✔️ <a id="user-content-r0s0" href="#r0s0">test.jsx</a>
```
test
✔️ test.jsx
```
19 changes: 19 additions & 0 deletions __tests__/__outputs__/jest-test-errors-results.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
![Tests failed](https://img.shields.io/badge/tests-2%20failed-critical)
## ❌ <a id="user-content-r0" href="#r0">fixtures/test-errors/jest/jest-test-results.xml</a>
**2** tests were completed in **646ms** with **0** passed, **2** failed and **0** skipped.
|Test suite|Passed|Failed|Skipped|Time|
|:---|---:|---:|---:|---:|
|[libs/bar.spec.ts](#r0s0)||1❌||0ms|
|[libs/foo.spec.ts](#r0s1)||1❌||0ms|
### ❌ <a id="user-content-r0s0" href="#r0s0">libs/bar.spec.ts</a>
```
Test suite failed to run
❌ libs/bar.spec.ts
● Test suite failed to run
```
### ❌ <a id="user-content-r0s1" href="#r0s1">libs/foo.spec.ts</a>
```
Test suite failed to run
❌ libs/foo.spec.ts
● Test suite failed to run
```
89 changes: 89 additions & 0 deletions __tests__/__snapshots__/jest-junit.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,94 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`jest-junit tests jest testsuite errors example test results matches snapshot 1`] = `
TestRunResult {
"path": "fixtures/test-errors/jest/jest-test-results.xml",
"suites": Array [
TestSuiteResult {
"groups": Array [
TestGroupResult {
"name": "Test suite failed to run",
"tests": Array [
TestCaseResult {
"error": Object {
"details": " ● Test suite failed to run

tsconfig.json:13:3 - error TS6258: 'typeRoots' should be set inside the 'compilerOptions' object of the config json file

13 \\"typeRoots\\": [\\"./src/lib/types\\", \\"./node_modules/@types\\"],
~~~~~~~~~~~
",
"line": undefined,
"path": undefined,
},
"name": "libs/foo.spec.ts",
"result": "failed",
"time": 0,
},
],
},
],
"name": "libs/foo.spec.ts",
"totalTime": 0,
},
TestSuiteResult {
"groups": Array [
TestGroupResult {
"name": "Test suite failed to run",
"tests": Array [
TestCaseResult {
"error": Object {
"details": " ● Test suite failed to run

tsconfig.json:13:3 - error TS6258: 'typeRoots' should be set inside the 'compilerOptions' object of the config json file

13 \\"typeRoots\\": [\\"./src/lib/types\\", \\"./node_modules/@types\\"],
~~~~~~~~~~~
",
"line": undefined,
"path": undefined,
},
"name": "libs/bar.spec.ts",
"result": "failed",
"time": 0,
},
],
},
],
"name": "libs/bar.spec.ts",
"totalTime": 0,
},
],
"totalTime": 646,
}
`;

exports[`jest-junit tests parsing ESLint report without timing information works 1`] = `
TestRunResult {
"path": "fixtures/jest-junit-eslint.xml",
"suites": Array [
TestSuiteResult {
"groups": Array [
TestGroupResult {
"name": "test",
"tests": Array [
TestCaseResult {
"error": undefined,
"name": "test.jsx",
"result": "success",
"time": 0,
},
],
},
],
"name": "test.jsx",
"totalTime": 0,
},
],
"totalTime": undefined,
}
`;

exports[`jest-junit tests report from ./reports/jest test results matches snapshot 1`] = `
TestRunResult {
"path": "fixtures/jest-junit.xml",
Expand Down
6 changes: 6 additions & 0 deletions __tests__/fixtures/jest-junit-eslint.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<testsuites>
<testsuite package="org.eslint" time="0" tests="1" errors="0" name="test.jsx">
<testcase time="0" name="test.jsx" classname="test" />
</testsuite>
</testsuites>
3 changes: 3 additions & 0 deletions __tests__/fixtures/test-errors/jest/files.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
libs/bar.spec.ts
libs/foo.spec.ts
tsconfig.json
25 changes: 25 additions & 0 deletions __tests__/fixtures/test-errors/jest/jest-test-results.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="jest tests" tests="0" failures="0" errors="2" time="0.646">
<testsuite name="libs/foo.spec.ts" errors="1" failures="0" skipped="0" timestamp="1970-01-01T00:00:00" time="0" tests="0">
<testcase classname="Test suite failed to run" name="libs/foo.spec.ts" time="0">
<error> ● Test suite failed to run

tsconfig.json:13:3 - error TS6258: &apos;typeRoots&apos; should be set inside the &apos;compilerOptions&apos; object of the config json file

13 &quot;typeRoots&quot;: [&quot;./src/lib/types&quot;, &quot;./node_modules/@types&quot;],
~~~~~~~~~~~
</error>
</testcase>
</testsuite>
<testsuite name="libs/bar.spec.ts" errors="1" failures="0" skipped="0" timestamp="1970-01-01T00:00:00" time="0" tests="0">
<testcase classname="Test suite failed to run" name="libs/bar.spec.ts" time="0">
<error> ● Test suite failed to run

tsconfig.json:13:3 - error TS6258: &apos;typeRoots&apos; should be set inside the &apos;compilerOptions&apos; object of the config json file

13 &quot;typeRoots&quot;: [&quot;./src/lib/types&quot;, &quot;./node_modules/@types&quot;],
~~~~~~~~~~~
</error>
</testcase>
</testsuite>
</testsuites>
43 changes: 43 additions & 0 deletions __tests__/jest-junit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,47 @@ describe('jest-junit tests', () => {
fs.mkdirSync(path.dirname(outputPath), {recursive: true})
fs.writeFileSync(outputPath, report)
})

it('parsing ESLint report without timing information works', async () => {
const fixturePath = path.join(__dirname, 'fixtures', 'jest-junit-eslint.xml')
const outputPath = path.join(__dirname, '__outputs__', 'jest-junit-eslint.md')
const filePath = normalizeFilePath(path.relative(__dirname, fixturePath))
const fileContent = fs.readFileSync(fixturePath, {encoding: 'utf8'})

const opts: ParseOptions = {
parseErrors: true,
trackedFiles: ['test.js']
}

const parser = new JestJunitParser(opts)
const result = await parser.parse(filePath, fileContent)
expect(result).toMatchSnapshot()

const report = getReport([result])
fs.mkdirSync(path.dirname(outputPath), {recursive: true})
fs.writeFileSync(outputPath, report)
})

it('jest testsuite errors example test results matches snapshot', async () => {
const fixturePath = path.join(__dirname, 'fixtures', 'test-errors', 'jest', 'jest-test-results.xml')
const trackedFilesPath = path.join(__dirname, 'fixtures', 'test-errors', 'jest', 'files.txt')
const outputPath = path.join(__dirname, '__outputs__', 'jest-test-errors-results.md')
const filePath = normalizeFilePath(path.relative(__dirname, fixturePath))
const fileContent = fs.readFileSync(fixturePath, {encoding: 'utf8'})

const trackedFiles = fs.readFileSync(trackedFilesPath, {encoding: 'utf8'}).split(/\n\r?/g)
const opts: ParseOptions = {
parseErrors: true,
trackedFiles
//workDir: '/home/dorny/dorny/jest/'
}

const parser = new JestJunitParser(opts)
const result = await parser.parse(filePath, fileContent)
expect(result).toMatchSnapshot()

const report = getReport([result])
fs.mkdirSync(path.dirname(outputPath), {recursive: true})
fs.writeFileSync(outputPath, report)
})
})
16 changes: 9 additions & 7 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {MochawesomeJsonParser} from './parsers/mochawesome-json/mochawesome-json

import {normalizeDirPath, normalizeFilePath} from './utils/path-utils'
import {getCheckRunContext} from './utils/github-utils'
import {Icon} from './utils/markdown-utils'

async function main(): Promise<void> {
try {
Expand Down Expand Up @@ -176,15 +175,19 @@ class TestReporter {

const isFailed = results.some(tr => tr.result === 'failed')
const conclusion = isFailed ? 'failure' : 'success'
const icon = isFailed ? Icon.fail : Icon.success

const passed = results.reduce((sum, tr) => sum + tr.passed, 0)
const failed = results.reduce((sum, tr) => sum + tr.failed, 0)
const skipped = results.reduce((sum, tr) => sum + tr.skipped, 0)
const shortSummary = `${passed} passed, ${failed} failed and ${skipped} skipped `

core.info(`Updating check run conclusion (${conclusion}) and output`)
const resp = await this.octokit.checks.update({
check_run_id: createResp.data.id,
conclusion,
status: 'completed',
output: {
title: `${name} ${icon}`,
title: shortSummary,
summary,
annotations
},
Expand Down
8 changes: 4 additions & 4 deletions src/parsers/jest-junit/jest-junit-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class JestJunitParser implements TestParser {
return sr
})

const time = parseFloat(junit.testsuites.$.time) * 1000
const time = junit.testsuites.$ && parseFloat(junit.testsuites.$.time) * 1000
return new TestRunResult(path, suites, time)
}

Expand Down Expand Up @@ -71,17 +71,17 @@ export class JestJunitParser implements TestParser {
}

private getTestCaseResult(test: TestCase): TestExecutionResult {
if (test.failure) return 'failed'
if (test.failure || test.error) return 'failed'
if (test.skipped) return 'skipped'
return 'success'
}

private getTestCaseError(tc: TestCase): TestCaseError | undefined {
if (!this.options.parseErrors || !tc.failure) {
if (!this.options.parseErrors || !(tc.failure|| tc.error)) {
return undefined
}

const details = tc.failure[0]
const details = tc.failure ? tc.failure[0] : tc.error ? tc.error[0] : 'unknown failure'
let path
let line

Expand Down
1 change: 1 addition & 0 deletions src/parsers/jest-junit/jest-junit-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ export interface TestCase {
}
failure?: string[]
skipped?: string[]
error?: string[]
}

0 comments on commit 7de269e

Please sign in to comment.