Last active
February 16, 2021 07:23
-
-
Save donocode/f497190ab145fbe7bbcfd817949cf6fa to your computer and use it in GitHub Desktop.
Output JSON structured logs from cypress for machine readable test logs
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
{ | |
"reporter": "structured-log.js", | |
"quiet": true | |
} |
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
const Mocha = require('mocha'); | |
const { | |
EVENT_RUN_BEGIN, | |
EVENT_RUN_END, | |
EVENT_TEST_FAIL, | |
EVENT_TEST_PASS, | |
EVENT_TEST_RETRY, | |
EVENT_TEST_PENDING, | |
} = Mocha.Runner.constants; | |
class StructuredLog { | |
constructor(runner) { | |
const stats = runner.stats; | |
runner.once(EVENT_RUN_BEGIN, function () { | |
writeEvent({ type: 'run:start', total: runner.total }); | |
}); | |
runner.on(EVENT_TEST_PASS, function (test) { | |
writeEvent({ type: 'test:pass', ...clean(test) }); | |
}); | |
runner.on(EVENT_TEST_RETRY, function (test) { | |
writeEvent({ type: 'test:retry', ...clean(test) }); | |
}); | |
runner.on(EVENT_TEST_PENDING, function (test) { | |
writeEvent({ type: 'test:skipped', ...clean(test) }); | |
}); | |
runner.on(EVENT_TEST_FAIL, function (test) { | |
writeEvent({ type: 'test:fail', ...clean(test) }); | |
}); | |
runner.once(EVENT_RUN_END, function () { | |
writeEvent({ type: 'run:end', ...stats }); | |
}); | |
} | |
} | |
function writeEvent(event) { | |
process.stdout.write(JSON.stringify(event) + '\n'); | |
} | |
function clean(test) { | |
return { | |
title: test.title, | |
fullTitle: typeof test.fullTitle === 'function' ? test.fullTitle() : test.fullTitle, | |
file: test.invocationDetails && test.invocationDetails.relativeFile, | |
duration: test.duration, | |
currentRetry: typeof test.currentRetry === 'function' ? test.currentRetry() : test.currentRetry, | |
retries: typeof test.retries === 'function' ? test.retries() : test.retries, | |
speed: test.speed, | |
error: test.err | |
? { | |
message: test.err.message, | |
stack: test.err.sourceMappedStack || test.err.stack, | |
} | |
: undefined, | |
}; | |
} | |
module.exports = StructuredLog; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment