Skip to content

Commit 05cd0c7

Browse files
authored
feat: report retried tests (#1)
1 parent 940bd5b commit 05cd0c7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1400
-535
lines changed

.eslintrc

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"__fixtures__",
2020
"coverage",
2121
"dist",
22+
"scripts",
2223
"website",
2324
"jest.config.js",
2425
"*.jest.config.js"
@@ -51,11 +52,12 @@
5152
"unicorn/prefer-node-protocol": "off",
5253
"unicorn/prefer-string-replace-all": "off",
5354
"unicorn/no-unused-properties": "off",
54-
"unicorn/prefer-module": "off"
55+
"unicorn/prefer-module": "off",
56+
"jsdoc/require-jsdoc": "off"
5557
},
5658
"overrides": [
5759
{
58-
"files": ["**/__tests__/*.ts", "*.test.ts"],
60+
"files": ["**/__tests__/*.ts", "**/__utils__/*.ts", "*.test.ts"],
5961
"rules": {
6062
"@typescript-eslint/no-explicit-any": "off",
6163
"node/no-unpublished-import": "off",

.github/workflows/ci.yml

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [master]
6+
paths-ignore:
7+
- '**/*.md'
8+
pull_request:
9+
branches: [master]
10+
paths-ignore:
11+
- '**/*.md'
12+
13+
jobs:
14+
15+
unit:
16+
name: Unit Tests
17+
strategy:
18+
matrix:
19+
os: [ubuntu-latest]
20+
jest: ['27.x.x', '28.x.x', '29.x.x']
21+
include:
22+
- node: 14
23+
jest: '27.x.x'
24+
- node: 16
25+
jest: '28.x.x'
26+
- node: 18
27+
jest: '29.x.x'
28+
runs-on: ${{ matrix.os }}
29+
steps:
30+
- uses: actions/checkout@v2
31+
- uses: actions/setup-node@v2
32+
with:
33+
node-version: ${{ matrix.node }}
34+
- uses: bahmutov/npm-install@v1
35+
with:
36+
useLockFile: false
37+
- name: Lint
38+
run: npm run lint:ci
39+
- name: Record fixtures
40+
run: npm run record
41+
env:
42+
JEST_VERSION: ${{ matrix.jest }}
43+
- name: Unit tests
44+
run: npm test -- --coverage
45+
- name: Semantic release
46+
if: github.ref == 'refs/heads/master'
47+
run: npx --no-install semantic-release --debug
48+
env:
49+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
50+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

.gitignore

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/tmp
2-
/node_modules
2+
3+
node_modules
34
package-lock.json
45
yarn.lock
56
/npm-debug.log
@@ -17,5 +18,6 @@ yarn.lock
1718
/dist
1819
/jest-allure2-reporter-*.tgz
1920

20-
/__fixtures__/allure-results
21-
/__fixtures__/results
21+
allure-results
22+
/__fixtures__
23+
!/__fixtures__/tests

.husky/pre-commit

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env sh
2+
. "$(dirname -- "$0")/_/husky.sh"
3+
4+
npm run lint:staged

CONTRIBUTING.md

+18-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ npm install
3232
Generate fixtures:
3333

3434
```bash
35-
npm run fixtures:results
35+
npm run record
36+
```
37+
38+
> If you want to check compatiblity with a specific Jest version, you can use the `JEST_VERSION` environment variable:
39+
40+
```bash
41+
JEST_VERSION=27 npm run record
3642
```
3743

3844
Run tests:
@@ -41,16 +47,22 @@ Run tests:
4147
npm test
4248
```
4349

44-
To view the test results, generate the Allure report:
50+
To view the test results, regenerate the full version of the fixtures:
51+
52+
```bash
53+
npm run start
54+
```
55+
56+
> If you want to check compatiblity with a specific Jest version, you can use the `JEST_VERSION` environment variable:
4557
4658
```bash
47-
npm run fixtures:allure
59+
JEST_VERSION=27 npm run start
4860
```
4961

50-
Start the Allure server:
62+
To re-run the Allure CLI server, run:
5163

5264
```bash
53-
npm start
65+
npm run serve
5466
```
5567

5668
### Checking your code
@@ -70,4 +82,4 @@ npx cz
7082

7183
and follow the instructions. We adhere to Angular's [commit message guidelines](https://github.com/angular/angular/blob/main/CONTRIBUTING.md#commit).
7284

73-
Thanks in advance for your contribution!
85+
Thanks in advance for your contribution!

README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ module.exports = {
5050

5151
Below is a list of all the available options:
5252

53-
| Property | Type | Default | Description |
54-
| ------------- | ---- | ------- | ----------- |
55-
| `resultsDir` | `string` | `<rootDir>/allure-results` | The directory where the results will be written to. |
56-
| `packageName` | `string` | `require('./package.json').name` | The name of your package. |
53+
| Property | Type | Default | Description |
54+
|----------------------------|--------------------------|----------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
55+
| `errorsAsFailedAssertions` | `boolean` | `false` | Treat thrown errors as failed assertions. By default, the reporter distinguishes between failed assertions and thrown errors. The former are reported as FAILED tests, the latter as BROKEN tests. |
56+
| `getEnvironmentInfo` | `function` or `boolean` | `true` | Can be customized with an async function to extract environment information from the test environment. By default, the environment information is extracted from the `process.env` object. Use `false` to disable environment information. |
57+
| `packageName` | `string` | `require('./package.json').name` | Add an extra label to each test case with the package name. Helpful when running tests from multiple packages in a monorepo. |
58+
| `resultsDir` | `string` | `<rootDir>/allure-results` | Path to the directory where the report will be generated. |
5759

5860
## Usage
5961

__fixtures__/dump-reporter.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const path = require('path');
2+
3+
// eslint-disable-next-line node/no-unpublished-import
4+
const fs = require('fs-extra');
5+
6+
class DumpReporter {
7+
constructor(globalConfig) {
8+
this._rootDir = globalConfig.rootDir;
9+
this._outFile = path.join(this._rootDir, 'recordings', require('jest/package.json').version + '.jsonl');
10+
}
11+
onRunStart(aggregatedResult, options) {
12+
console.log('Starting dump: %s', this._outFile);
13+
fs.ensureFileSync(this._outFile);
14+
fs.writeFileSync(this._outFile, '');
15+
this._append({ method: 'onRunStart', params: [aggregatedResult, options] });
16+
}
17+
onTestFileStart(test) {
18+
this._append({ method: 'onTestFileStart', params: [test] });
19+
}
20+
onTestCaseResult(test, testCaseResult) {
21+
this._append({ method: 'onTestCaseResult', params: [test, testCaseResult] });
22+
}
23+
onTestFileResult(test, testResult, aggregatedResult) {
24+
this._append({ method: 'onTestFileResult', params: [test, testResult, aggregatedResult] });
25+
}
26+
onRunComplete(_testContexts, results) {
27+
this._append({ method: 'onRunComplete', params: [{}, results] });
28+
}
29+
getLastError() { }
30+
_append(payload) {
31+
console.log('Append: %s', payload.method);
32+
const timedPayload = { ...payload, time: Date.now() };
33+
fs.appendFileSync(this._outFile, JSON.stringify(timedPayload) + '\n');
34+
}
35+
}
36+
37+
module.exports = DumpReporter;

__fixtures__/tests/simple/simple-suite-retry.test.js renamed to __fixtures__/tests/flaky.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
jest.retryTimes(1);
22

3-
describe('Suite', () => {
3+
describe('flaky test', () => {
44
let firstRun = true;
55

66
test('should pass on the second attempt', async () => {
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
2+
3+
describe('hook errors', () => {
4+
describe.each([
5+
['beforeAll'],
6+
['beforeEach'],
7+
['afterEach'],
8+
['afterAll'],
9+
])('%s', (hook) => {
10+
global[hook](async () => {
11+
await sleep(50);
12+
13+
throw new Error(`Simulated error in ${hook}(...)`);
14+
});
15+
16+
test('Test #1', async () => {
17+
// some test
18+
});
19+
20+
test('Test #2', async () => {
21+
// some test
22+
});
23+
});
24+
});

__fixtures__/tests/hooks/hooks-beforeAll-fail.test.js

-17
This file was deleted.

__fixtures__/tests/nested/nested-suite.test.js renamed to __fixtures__/tests/nesting.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('Grand parent suite', () => {
3232

3333
afterAll(async () => {
3434
console.log('Parent suite: afterAll');
35-
throw new Error('Parent suite error');
35+
throw new Error('Parent suite: afterAll error');
3636
});
3737
});
3838
});

__fixtures__/tests/root-test-fail.test.js

-3
This file was deleted.

__fixtures__/tests/root-test-pass.test.js

-5
This file was deleted.

__fixtures__/tests/root-test-skip.test.js

-3
This file was deleted.

__fixtures__/tests/simple/simple-suite-fail.test.js

-5
This file was deleted.

__fixtures__/tests/simple/simple-suite-pass.test.js

-7
This file was deleted.

__fixtures__/tests/simple/simple-suite-skip.test.js

-5
This file was deleted.

__fixtures__/tests/statuses.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
2+
3+
generateSimpleSuite('root');
4+
5+
describe('Suite', () => {
6+
generateSimpleSuite('inner');
7+
});
8+
9+
function generateSimpleSuite(name) {
10+
test.skip(`${name} skipped test`, async () => {
11+
// empty test
12+
});
13+
14+
test(`${name} broken test`, async () => {
15+
throw new Error('Simulated error');
16+
});
17+
18+
test(`${name} failed test`, async () => {
19+
expect(2 + 2).toBe(5);
20+
});
21+
22+
test(`${name} passed test (600ms)`, async () => {
23+
await sleep(600);
24+
});
25+
}

fixture-allure.jest.config.js

-10
This file was deleted.

fixture-results.jest.config.js

-7
This file was deleted.

jest.config.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
module.exports = {
33
preset: 'ts-jest',
44
testEnvironment: 'node',
5-
testMatch: ['<rootDir>/src/**/*.test.{js,ts}'],
5+
testMatch: [
6+
'<rootDir>/src/**/*.test.{js,ts}',
7+
'<rootDir>/src/__tests__/**/*.{js,ts}'
8+
],
69
};

0 commit comments

Comments
 (0)