forked from aws/aws-lambda-nodejs-runtime-interface-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStdoutReporter.ts
More file actions
90 lines (77 loc) · 2.13 KB
/
StdoutReporter.ts
File metadata and controls
90 lines (77 loc) · 2.13 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
/** Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. */
"use strict";
import { MochaOptions, Runner, reporters } from "mocha";
const {
EVENT_SUITE_BEGIN,
EVENT_SUITE_END,
EVENT_RUN_BEGIN,
EVENT_RUN_END,
EVENT_TEST_PASS,
EVENT_TEST_FAIL,
} = Runner.constants;
/**
* Custom reporter does not depend on any of the console.* functions, which
* enables clean test output even when applying the lambda-runtime console
* patch.
*/
module.exports = class StdoutReporter extends reporters.Base {
private _alreadyWritten: boolean;
private _report: string;
private _indents: number;
public constructor(runner: Runner, options: MochaOptions) {
super(runner, options);
this._alreadyWritten = false;
this._report = "";
this._indents = 0;
const stats = runner.stats;
runner
.once(EVENT_RUN_BEGIN, () => {})
.on(EVENT_SUITE_BEGIN, (suite) => {
this.log(suite.title);
this.increaseIndent();
})
.on(EVENT_SUITE_END, () => {
this.decreaseIndent();
})
.on(EVENT_TEST_PASS, (test) => {
this.log(`✓ ${test.title}`);
})
.on(EVENT_TEST_FAIL, (test, err) => {
this.log(`✗ ${test.title}`);
this.increaseIndent();
err.stack.split("\n").forEach((msg) => this.log(msg));
this.decreaseIndent();
})
.once(EVENT_RUN_END, () => {
this.log(
"Results " +
stats.passes +
" passed out of " +
(stats.passes + stats.failures) +
" total tests"
);
this.dumpReport();
});
// This is hella nice if Mocha crashes for some reason
// (which turns out is easy to do if you fool around with console.log)
process.on("exit", () => this.dumpReport());
}
indent() {
return Array(this._indents).join(" ");
}
increaseIndent() {
this._indents++;
}
decreaseIndent() {
this._indents--;
}
log(line) {
this._report += `${this.indent()}${line}\n`;
}
dumpReport() {
if (!this._alreadyWritten) {
process.stdout.write(this._report);
this._alreadyWritten = true;
}
}
};