/
neotest-hardhat-reporter.js
70 lines (51 loc) · 1.55 KB
/
neotest-hardhat-reporter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Documentation
// https://github.com/mochajs/mocha/wiki/Third-party-reporters
// https://githu.com/mochajs/mocha/tree/master/lib/reporters
var mocha =require('mocha');
const {
EVENT_RUN_END,
EVENT_TEST_PASS,
EVENT_TEST_FAIL,
EVENT_TEST_PENDING,
} = mocha.Runner.constants
const TEST_STATUS = {
passed: "passed",
skipped: "skipped",
failed: "failed",
}
function MyReporter(runner) {
let tests = {} ;
runner.on(EVENT_TEST_FAIL, function (test, err) {
const testId = getTestId(test);
tests[testId] = { status: TEST_STATUS.failed , errors: [ {message: err.message} ]}
});
runner.on(EVENT_TEST_PENDING, function(test) {
const testId = getTestId(test);
tests[testId] = { status: TEST_STATUS.skipped }
});
runner.on(EVENT_TEST_PASS, function(test) {
const testId = getTestId(test);
tests[testId] = { status: TEST_STATUS.passed }
});
runner.on(EVENT_RUN_END, function(test) {
const testResults = {
stats: runner.stats,
tests,
}
console.log(JSON.stringify(testResults))
})
}
function getTestId(test) {
const suiteTitles = getTestParentsIds(test.parent);
return `${test.file}${suiteTitles}::${test.title}`
}
function getTestParentsIds(suite) {
let title = suite.title
if (suite.parent) {
let suiteTittle = getTestParentsIds(suite.parent)
title = `${suiteTittle}::${title}`
}
return title
}
mocha.utils.inherits(MyReporter, mocha.reporters.json);
module.exports = MyReporter;