-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathreporter.ts
More file actions
98 lines (79 loc) · 2.45 KB
/
reporter.ts
File metadata and controls
98 lines (79 loc) · 2.45 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//===----------------------------------------------------------------------===//
//
// This file is Modular Inc proprietary.
//
//===----------------------------------------------------------------------===//
import { reporters, Runner, Test } from 'mocha';
import { setLogHook } from '../extension';
const {
EVENT_TEST_BEGIN,
EVENT_SUITE_BEGIN,
EVENT_SUITE_END,
EVENT_TEST_PASS,
EVENT_TEST_PENDING,
EVENT_TEST_FAIL,
EVENT_RUN_END,
} = Runner.constants;
const Base = reporters.Base;
class VsCodeReporter extends Base {
constructor(runner: Runner) {
super(runner);
const testLogs: { [id: string]: string[] } = {};
let indentDepth = 0;
const indent = (str: string) => {
let result = '';
for (const line of str.split('\n')) {
result += ' '.repeat(indentDepth) + line + '\n';
}
return result.trimEnd();
};
runner.on(EVENT_SUITE_BEGIN, (suite) => {
console.log(indent(Base.color('suite', suite.title)));
indentDepth += 2;
});
runner.on(EVENT_SUITE_END, () => {
indentDepth -= 2;
});
runner.on(EVENT_TEST_BEGIN, (test: Test) => {
const testId = test.fullTitle();
testLogs[testId] = [];
const callback = (level: string, message: string) =>
testLogs[testId].push(`[${level.padEnd(5, ' ')}]: ${message}`);
setLogHook(callback);
});
runner.on(EVENT_TEST_PASS, (test: Test) => {
console.log(
indent(
`${Base.color('checkmark', Base.symbols.ok)} ${Base.color('pass', test.title)}`,
),
);
});
runner.on(EVENT_TEST_FAIL, (test: Test, err) => {
console.log(
indent(
`${Base.color('fail', Base.symbols.err)} ${Base.color('fail', test.title)}`,
),
);
indentDepth += 2;
if (err.stack) {
console.log(indent(`${Base.color('error stack', err.stack)}`));
} else {
console.log(indent(`${Base.color('error title', err.name)}`));
console.log(indent(`${Base.color('error message', err.message)}`));
}
console.log(indent('\nExtension logs:\n'));
const logs = testLogs[test.fullTitle()];
for (const log of logs) {
console.log(indent(log));
}
indentDepth -= 2;
});
runner.on(EVENT_TEST_PENDING, (test: Test) => {
console.log(indent(Base.color('pending', `- ${test.title}`)));
});
runner.on(EVENT_RUN_END, () => {
this.epilogue();
});
}
}
module.exports = VsCodeReporter;