Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mocha reporter #6

Merged
merged 8 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions .c8rc.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"all": true,
"check-coverage": true,
"statements": 100,
"branches": 100,
"statements": 95,
"branches": 85,
"functions": 100,
"lines": 100,
"lines": 95,
"include": [
"src/**/*.js"
"src/**/*.js",
"src/**/*.cjs"
],
"exclude": [
"src/index.js"
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,3 @@ jobs:
aws-access-key-id: ${{secrets.AWS_ACCESS_KEY_ID}}
aws-secret-access-key: ${{secrets.AWS_SECRET_ACCESS_KEY}}
aws-session-token: ${{secrets.AWS_SESSION_TOKEN}}
report-path: ./test/data/d2l-test-report.json
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.nyc_output/
coverage/
node_modules/
d2l-test-report.json
6 changes: 3 additions & 3 deletions dist/index.js

Large diffs are not rendered by default.

1,977 changes: 344 additions & 1,633 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@
"prebuild": "rimraf dist/",
"build": "ncc build src/index.js -m -o dist/",
"lint": "npm run lint:eslint",
"lint:eslint": "eslint . --ext .js",
"lint:eslint": "eslint . --ext .js,.cjs",
"fix": "npm run fix:eslint",
"fix:eslint": "npm run lint:eslint -- --fix",
"test": "npm run lint && npm run test:unit",
"test:unit": "c8 env-cmd -f test/.env mocha \"test/**/*.test.js\""
"test:unit": "c8 env-cmd -f test/.env mocha \"test/**/*.test.js\" -R src/internal/mocha-reporter.cjs --retries 3"
},
"dependencies": {
"@actions/core": "^1",
"@actions/github": "^6",
"@aws-sdk/client-sts": "^3",
"@aws-sdk/client-timestream-write": "^3",
"ajv": "^8",
"lodash-es": "^4",
"yn": "^5"
"chalk": "^4",
"uuid": "^9"
},
"devDependencies": {
"@vercel/ncc": "^0.38",
Expand Down
142 changes: 142 additions & 0 deletions src/internal/mocha-reporter.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
const { relative, sep: platformSeparator, resolve } = require('path');
const chalk = require('chalk');
const { join } = require('path/posix');
const { reporters: { Spec } } = require('mocha');
const { Runner: { constants } } = require('mocha');
const { type } = require('os');
const { v4: uuid } = require('uuid');
const { writeFileSync } = require('fs');

const { red, blue } = chalk;

const {
EVENT_RUN_BEGIN,
EVENT_RUN_END,
EVENT_TEST_BEGIN,
EVENT_TEST_END,
EVENT_TEST_PENDING,
EVENT_TEST_RETRY
} = constants;

const getOperatingSystem = () => {
switch (type()) {
case 'Linux':
return 'linux';
case 'Darwin':
return 'macos';
case 'Windows_NT':
return 'windows';
default:
throw new Error('Unknown operating system');
}
};
Comment on lines +21 to +32
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be something I can pull out into a common helper for reporters.


const makeTestName = (test) => {
return test.titlePath().join(' > ');
};

const makeLocation = (filePath) => {
const path = relative(process.cwd(), filePath);
const pathParts = path.split(platformSeparator);

return join(...pathParts);
};
Comment on lines +38 to +43
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be something I can pull out into a common helper for reporters.


const convertEndState = (state) => {
return state === 'pending' ? 'skipped' : state;
};

class TestReportingMochaReporter extends Spec {
constructor(runner, options) {
super(runner, options);

const { stats } = runner;

this._report = {
reportId: uuid(),
reportVersion: 1,
summary: {
operatingSystem: getOperatingSystem(),
framework: 'mocha'
}
};
this._tests = new Map();
this._testsFlaky = new Set();

runner
.once(EVENT_RUN_BEGIN, () => this._onRunBegin(stats))
.once(EVENT_RUN_END, () => this._onRunEnd(stats))
.on(EVENT_TEST_PENDING, test => this._onTestPending(test))
.on(EVENT_TEST_BEGIN, test => this._onTestBegin(test))
.on(EVENT_TEST_END, test => this._onTestEnd(test))
.on(EVENT_TEST_RETRY, test => this._onTestRetry(test));
}

_onRunBegin(stats) {
this._report.summary.started = stats.start.toISOString();
}

_onRunEnd(stats) {
this._report.summary = {
...this._report.summary,
totalDuration: stats.duration,
state: stats.failures !== 0 ? 'failed' : 'passed',
countPassed: stats.passes,
countFailed: stats.failures,
countSkipped: stats.pending,
countFlaky: this._testsFlaky.size
};
this._report.details = [...this._tests]
.map(([name, values]) => ({ name, ...values }));

try {
const reportOutput = JSON.stringify(this._report);
const filePath = './d2l-test-report.json';

writeFileSync(filePath, reportOutput, 'utf8');

console.info(` D2L test report available at: ${blue(resolve(filePath))}\n`);
} catch {
console.error(red(' Failed to generate D2L test report\n'));
}
}

_onTestPending(test) {
this._onTestBegin(test);
}

_onTestBegin(test) {
const name = makeTestName(test);
const values = this._tests.get(name) ?? {};

values.started = values.started ?? new Date().toISOString();
values.location = values.location ?? makeLocation(test.file);
values.retries = values.retries === undefined ? 0 : values.retries + 1;
values.totalDuration = values.totalDuration ?? 0;

this._tests.set(name, values);
}

_onTestRetry(test) {
const name = makeTestName(test);
const values = this._tests.get(name);

values.totalDuration += test.duration;

this._tests.set(name, values);
this._testsFlaky.add(name);
}

_onTestEnd(test) {
const name = makeTestName(test);
const values = this._tests.get(name);

values.status = convertEndState(test.state);
values.duration = test.duration ?? 0;
values.totalDuration += values.duration;

this._tests.set(name, values);
}
}

module.exports = TestReportingMochaReporter;
19 changes: 19 additions & 0 deletions test/reporter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const delay = () => {
return new Promise(resolve => setTimeout(resolve, 1000));
};

describe('reporter', () => {
let count = 0;

it.skip('skipped test', () => {});

it('flaky test', async() => {
if (count < 2) {
await delay();

count++;

throw new Error('flaky test failure');
}
});
});