-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cba2ad5
Add mocha reporter
devpow112 13b31ec
Add info of report location
devpow112 19ba175
Lint fixes
devpow112 e2a98ee
Remove no override for now
devpow112 b706975
Remove unused pacakges for now
devpow112 20bdbbb
Add flaky test and skipped test for testing
devpow112 871bf3f
Add mocha reporter to test coverage
devpow112 f28d28c
Merge branch 'main' into depowell/mocha-reporter
devpow112 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
.nyc_output/ | ||
coverage/ | ||
node_modules/ | ||
d2l-test-report.json |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,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'); | ||
} | ||
}; | ||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
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,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'); | ||
} | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.