This repository has been archived by the owner on Jul 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
reporter.mjs
153 lines (127 loc) · 3.67 KB
/
reporter.mjs
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import * as Mocha from 'mocha'
import { Provider } from 'remote-event-emitter'
import { mkaddress } from '@atom-ide/utils'
import serialisers from './serialisers'
/**
* Mocha reporter which relays all Mocha's events to a TCP or Unix socket
*/
class RemoteReporter extends Mocha.reporters.Base {
/**
* List of Mocha events to forward
* @type {Array}
*/
static events = [
'start',
'end',
'suite',
'suite end',
'test',
'test end',
'hook',
'hook end',
'pass',
'fail',
'pending',
]
/**
* Reporter options, as provided by the user
*
* @private
* @type {Object}
*/
#options = null
/**
* An instance of Mocha's Test Runner
*
* @private
* @type {Object}
*/
#runner = null
/**
* An event emitter capable of forwarding emitted events to a remote destination
*
* @private
* @type {Provider}
*/
#provider = null
/**
* Construct a new reporeter
*
* @param {Object} runner Instance of Mocha's Test Runner
* @param {Object} opts Options object
* @param {Object} opts.reporterOptions Reporter options, as provided by Mocha
* @param {String} opts.reporterOptions.address Address to send the events to
* @param {String} opts.reporterOptions.root Root directory of the project
* @param {String} opts.reporterOptions.mode Networking mode. either `unix` or `ip`.
* @param {Boolean} opts.reporterOptions.nostats If set to a truthy value, the reporter will
* not print the final suite stats to stdout
*/
constructor(runner, { reporterOptions: options }) {
super(runner)
this.#runner = runner
this.#options = options
if (this.#options.root) {
this.#options.address = mkaddress({
root: this.#options.root,
mode: this.#options.mode || 'unix',
})
}
// Initialise the remote event emitter provider
this.#provider = new Provider({ destination: options.address })
// Bind Mocha events to functions defined on this class
RemoteReporter.events.forEach(event => {
runner.on(event, ::this[event])
})
// If the provider fails for some reason, show the error in the console
this.#provider.on('error', ::this.onError)
}
onError(err) {
// eslint-disable-next-line no-console
console.error(err)
throw err
}
start() {
this.#provider.emit('start', serialisers.runner(this.#runner))
}
end() {
this.#provider.emit('end', serialisers.runner(this.#runner))
// Print the final test results to the console, just in case
if (!this.#options.nostats) {
this.epilogue()
}
// Explicitly close the remote so that Node does not hang indefinitely
this.#provider.end()
this.#runner = null
this.#provider = null
}
suite(suite) {
this.#provider.emit('suite', serialisers.suite(suite))
}
'suite end'(suite) {
this.#provider.emit('suite end', serialisers.suite(suite))
}
test(test) {
this.#provider.emit('test', serialisers.runnable(test))
}
'test end'(test) {
this.#provider.emit('test end', serialisers.runnable(test))
}
hook(hook) {
this.#provider.emit('hook', serialisers.runnable(hook))
}
'hook end'(hook) {
this.#provider.emit('hook end', serialisers.runnable(hook))
}
pass(test) {
this.#provider.emit('pass', serialisers.runnable(test))
}
fail(test, err) {
this.#provider.emit('fail', serialisers.runnable(test), serialisers.err(err))
}
pending(test) {
this.#provider.emit('pending', serialisers.runnable(test))
}
}
export {
RemoteReporter,
}