Last active
April 13, 2023 11:25
-
-
Save dopry/691e6b21170b55c41768d02dc059c48b to your computer and use it in GitHub Desktop.
jest-mochawesome-reporter
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
// based on https://gist.github.com/Prophet32j/05a2ceda3743b9fd93e98e56a9227309 | |
const fs = require('fs'); | |
const uuid = require('uuid-v4'); | |
const buildMargeInput = (testResults) => { | |
let elapsed = buildElapsedTime(testResults.testResults); | |
let testSuites = testResults.testResults.map(createSuite); | |
var input = {}; | |
input.stats = { | |
suites: testResults.numTotalTestSuites, | |
tests: testResults.numTotalTests, | |
testsRegistered: testResults.numTotalTests, | |
passes: testResults.numPassedTests, | |
pending: testResults.numPendingTests, | |
failures: testResults.numFailedTests, | |
start: new Date(testResults.startTime), // Jest does epochs | |
end: new Date(testResults.startTime + elapsed), | |
duration: elapsed, | |
passPercent: testResults.numPassedTests / testResults.numTotalTests * 100, | |
pendingPercent: testResults.numPendingTests / testResults.numTotalTests * 100, | |
other: 0, | |
hasOther: false, | |
skipped: 0, | |
hasSkipped: false, | |
}; | |
input.results = testSuites; | |
return input; | |
} | |
function createSuite(suite) { | |
let id = uuid(); | |
let tests = suite.testResults.map((test) => { | |
return createTest(test, id); | |
}); | |
let passes = tests.filter(test => test.pass).map(test => test.uuid); | |
let failures = tests.filter(test => test.fail).map(test => test.uuid); | |
let pending = tests.filter(test => test.pending).map(test => test.uuid); | |
return { | |
title: suite.testResults[0].ancestorTitles[0], | |
suites: [], | |
tests: tests, | |
pending: pending, | |
root: false, | |
uuid: id, | |
_timeout: 5000, | |
fullFile: suite.testFilePath, | |
file: suite.testFilePath.split('/').pop(), | |
beforeHooks: [], | |
afterHooks: [], | |
passes: passes, | |
failures: failures, | |
skipped: [], | |
duration: suite.perfStats.end - suite.perfStats.start | |
} | |
} | |
function createTest(test, parentId) { | |
return { | |
title: test.title, | |
fullTitle: fullTitle(test), | |
timedOut: false, | |
duration: test.duration, | |
pass: passed(test), | |
fail: failed(test), | |
pending: pending(test), | |
code: '', | |
// isRoot: false, | |
uuid: uuid(), | |
parentUUID: parentId, | |
skipped: false, | |
isHook: false, | |
err: {} | |
} | |
} | |
function passed(test) { | |
return test.status === 'passed'; | |
} | |
function failed(test) { | |
return test.status === 'failed'; | |
} | |
function pending(test) { | |
return test.status === 'pending'; | |
} | |
function fullTitle(test) { | |
if (test.fullName) { | |
return test.fullName; | |
} | |
return test.ancestorTitles.reduce((sum, val) => { | |
sum + val + ' '; | |
}, ''); | |
} | |
function buildElapsedTime(suites) { | |
return suites.reduce((sum, suite) => { | |
return sum + suite.testResults.reduce((_sum, test) => { | |
return _sum + test.duration; | |
}, 0); | |
}, 0); | |
} | |
function writeOutput(payload, filepath) { | |
const json = JSON.stringify(payload); | |
fs.writeFile(filepath, json, 'utf8', (err) => { | |
if (err) { throw err; } | |
console.log('file saved'); | |
}); | |
} | |
/** | |
* TODO: Extend BaseReporter | |
*/ | |
class JestMochawesomeReporter { // extends BaseReporter { | |
constructor(globalConfig, options) { | |
this._globalConfig = globalConfig | |
this._options = options | |
} | |
onRunComplete(contexts, results) { | |
const report = buildMargeInput(results); | |
writeOutput(report, `${this._globalConfig.rootDir}/jest-mochawesome.json`) | |
} | |
} | |
module.exports = JestMochawesomeReporter |
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
module.exports = { | |
reporters: [ | |
["./jest-mochawesome.js", {}] | |
] | |
}; |
This dose not works with @jest/reporters
29.0.3.
As it does not have the method execute(ReporteContext)
.
_istanbulReports()
.default.create(reporter, {
maxCols: process.stdout.columns || Infinity,
...additionalOptions
})
.execute(reportContext); // ERROR: TypeError: _istanbulReports(...).default.create(...).execute is not a function
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
This is great!
Is there an option to get the test code/failureMessages as well?