|
1 | | -const Mocha = require('mocha'); |
2 | | - |
3 | 1 | function formatMilliseconds (duration) { |
4 | 2 | if (duration < 1000) |
5 | 3 | return `${duration}ms`; |
6 | 4 |
|
7 | 5 | return `${(duration / 1000).toFixed(2).replace(/\.?0+$/, '')}s`; |
8 | 6 | } |
9 | 7 |
|
10 | | -const { |
11 | | - EVENT_RUN_BEGIN, |
12 | | - EVENT_RUN_END, |
13 | | - EVENT_SUITE_BEGIN, |
14 | | - EVENT_SUITE_END, |
15 | | - EVENT_TEST_FAIL, |
16 | | - EVENT_TEST_PASS, |
17 | | - EVENT_TEST_PENDING, |
18 | | - EVENT_TEST_RETRY, |
19 | | -} = Mocha.Runner.constants; |
| 8 | +async function createReporter () { |
| 9 | + const { default: Mocha } = await import('mocha'); |
20 | 10 |
|
21 | | -const { inherits } = require('util'); |
| 11 | + const { |
| 12 | + EVENT_RUN_BEGIN, |
| 13 | + EVENT_RUN_END, |
| 14 | + EVENT_SUITE_BEGIN, |
| 15 | + EVENT_SUITE_END, |
| 16 | + EVENT_TEST_FAIL, |
| 17 | + EVENT_TEST_PASS, |
| 18 | + EVENT_TEST_PENDING, |
| 19 | + EVENT_TEST_RETRY, |
| 20 | + } = Mocha.Runner.constants; |
22 | 21 |
|
23 | | -const Base = Mocha.reporters.Base; |
24 | | -const color = Base.color; |
| 22 | + const Base = Mocha.reporters.Base; |
| 23 | + const color = Base.color; |
25 | 24 |
|
26 | | -exports = module.exports = SpecWithRetries; |
| 25 | + class SpecWithRetries extends Base { |
| 26 | + constructor (runner, options) { |
| 27 | + super(runner, options); |
27 | 28 |
|
28 | | -function SpecWithRetries (runner, options) { |
29 | | - Base.call(this, runner, options); |
| 29 | + this.stats.unstables = []; |
30 | 30 |
|
31 | | - this.stats.unstables = []; |
| 31 | + const self = this; |
| 32 | + let indents = 0; |
| 33 | + let n = 0; |
32 | 34 |
|
33 | | - const self = this; |
34 | | - let indents = 0; |
35 | | - let n = 0; |
| 35 | + function indent () { |
| 36 | + return Array(indents).join(' '); |
| 37 | + } |
36 | 38 |
|
37 | | - function indent () { |
38 | | - return Array(indents).join(' '); |
39 | | - } |
| 39 | + function groupBy (collection, predicate) { |
| 40 | + return collection.reduce((r, v, i, a, k = predicate(v)) => ((r[k] || (r[k] = [])).push(v), r), {}); // eslint-disable-line no-sequences |
| 41 | + } |
40 | 42 |
|
41 | | - function groupBy (collection, predicate) { |
42 | | - return collection.reduce((r, v, i, a, k = predicate(v)) => ((r[k] || (r[k] = [])).push(v), r), {}); // eslint-disable-line no-sequences |
43 | | - } |
| 43 | + function epilogue () { |
| 44 | + const stats = this.stats; |
| 45 | + let fmt; |
44 | 46 |
|
45 | | - function epilogue () { |
46 | | - const stats = this.stats; |
47 | | - let fmt; |
| 47 | + Base.consoleLog(); |
48 | 48 |
|
49 | | - Base.consoleLog(); |
| 49 | + // passes |
| 50 | + fmt = |
| 51 | + color('bright pass', ' ') + |
| 52 | + color('green', ' %d passing') + |
| 53 | + color('light', ' (%s)'); |
50 | 54 |
|
51 | | - // passes |
52 | | - fmt = |
53 | | - color('bright pass', ' ') + |
54 | | - color('green', ' %d passing') + |
55 | | - color('light', ' (%s)'); |
| 55 | + Base.consoleLog(fmt, stats.passes || 0, formatMilliseconds(stats.duration)); |
56 | 56 |
|
57 | | - Base.consoleLog(fmt, stats.passes || 0, formatMilliseconds(stats.duration)); |
| 57 | + // pending |
| 58 | + if (stats.pending) { |
| 59 | + fmt = color('pending', ' ') + color('pending', ' %d pending'); |
58 | 60 |
|
59 | | - // pending |
60 | | - if (stats.pending) { |
61 | | - fmt = color('pending', ' ') + color('pending', ' %d pending'); |
| 61 | + Base.consoleLog(fmt, stats.pending); |
| 62 | + } |
62 | 63 |
|
63 | | - Base.consoleLog(fmt, stats.pending); |
64 | | - } |
| 64 | + // failures |
| 65 | + if (stats.failures) { |
| 66 | + fmt = color('fail', ' %d failing'); |
65 | 67 |
|
66 | | - // failures |
67 | | - if (stats.failures) { |
68 | | - fmt = color('fail', ' %d failing'); |
| 68 | + Base.consoleLog(fmt, stats.failures); |
69 | 69 |
|
70 | | - Base.consoleLog(fmt, stats.failures); |
| 70 | + Base.list(this.failures); |
| 71 | + } |
71 | 72 |
|
72 | | - Base.list(this.failures); |
73 | | - } |
| 73 | + // unstable tests |
| 74 | + if (stats.unstables.length) { |
| 75 | + Base.consoleLog(); |
| 76 | + |
| 77 | + fmt = color('bright yellow', ' Unstable test(s):'); |
74 | 78 |
|
75 | | - // unstable tests |
76 | | - if (stats.unstables.length) { |
77 | | - Base.consoleLog(); |
| 79 | + Base.consoleLog(fmt); |
78 | 80 |
|
79 | | - fmt = color('bright yellow', ' Unstable test(s):'); |
| 81 | + const groupedByFile = groupBy(stats.unstables, unstable => unstable.file); |
80 | 82 |
|
81 | | - Base.consoleLog(fmt); |
| 83 | + Object.entries(groupedByFile) |
| 84 | + .forEach(([key, value]) => { |
| 85 | + Base.consoleLog(color('bright yellow', ' %s'), key); |
82 | 86 |
|
83 | | - const groupedByFile = groupBy(stats.unstables, unstable => unstable.file); |
| 87 | + value.forEach(unstableTest => { |
| 88 | + Base.consoleLog(color('bright yellow', ' %s'), unstableTest.title); |
| 89 | + }); |
| 90 | + }); |
| 91 | + } |
84 | 92 |
|
85 | | - Object.entries(groupedByFile) |
86 | | - .forEach(([key, value]) => { |
87 | | - Base.consoleLog(color('bright yellow', ' %s'), key); |
| 93 | + Base.consoleLog(); |
| 94 | + } |
88 | 95 |
|
89 | | - value.forEach(unstableTest => { |
90 | | - Base.consoleLog(color('bright yellow', ' %s'), unstableTest.title); |
91 | | - }); |
| 96 | + function findTestIndex (collection, test) { |
| 97 | + return collection.findIndex(item => { |
| 98 | + return item.file === test.file && |
| 99 | + item.title === test.title; |
92 | 100 | }); |
| 101 | + } |
| 102 | + |
| 103 | + function isInUnstables (test) { |
| 104 | + return findTestIndex(this.stats.unstables, test) > -1; |
| 105 | + } |
| 106 | + |
| 107 | + runner.on(EVENT_RUN_BEGIN, function () { |
| 108 | + Base.consoleLog(); |
| 109 | + }); |
| 110 | + |
| 111 | + runner.on(EVENT_SUITE_BEGIN, function (suite) { |
| 112 | + ++indents; |
| 113 | + Base.consoleLog(color('suite', '%s%s'), indent(), suite.title); |
| 114 | + }); |
| 115 | + |
| 116 | + runner.on(EVENT_SUITE_END, function () { |
| 117 | + --indents; |
| 118 | + if (indents === 1) |
| 119 | + Base.consoleLog(); |
| 120 | + }); |
| 121 | + |
| 122 | + runner.on(EVENT_TEST_PENDING, function (test) { |
| 123 | + const fmt = indent() + color('pending', ' - %s'); |
| 124 | + |
| 125 | + Base.consoleLog(fmt, test.title); |
| 126 | + }); |
| 127 | + |
| 128 | + runner.on(EVENT_TEST_PASS, function (test) { |
| 129 | + let fmt; |
| 130 | + |
| 131 | + if (test.speed === 'fast') { |
| 132 | + fmt = |
| 133 | + indent() + |
| 134 | + color('checkmark', ' ' + Base.symbols.ok) + |
| 135 | + color('pass', ' %s'); |
| 136 | + Base.consoleLog(fmt, test.title); |
| 137 | + } |
| 138 | + else { |
| 139 | + fmt = |
| 140 | + indent() + |
| 141 | + color('checkmark', ' ' + Base.symbols.ok) + |
| 142 | + color('pass', ' %s') + |
| 143 | + color(test.speed, ' (%dms)'); |
| 144 | + Base.consoleLog(fmt, test.title, test.duration); |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + runner.on(EVENT_TEST_FAIL, function (test) { |
| 149 | + Base.consoleLog(indent() + color('fail', ' %d) %s'), ++n, test.title); |
| 150 | + |
| 151 | + const index = findTestIndex(self.stats.unstables, test); |
| 152 | + |
| 153 | + if (index > -1) |
| 154 | + self.stats.unstables.splice(index, 1); |
| 155 | + }); |
| 156 | + |
| 157 | + runner.on(EVENT_TEST_RETRY, test => { |
| 158 | + if (!isInUnstables.call(self, test)) |
| 159 | + self.stats.unstables.push(test); |
| 160 | + }); |
| 161 | + |
| 162 | + runner.once(EVENT_RUN_END, () => { |
| 163 | + epilogue.call(self); |
| 164 | + }); |
93 | 165 | } |
94 | | - |
95 | | - Base.consoleLog(); |
96 | | - } |
97 | | - |
98 | | - function findTestIndex (collection, test) { |
99 | | - return collection.findIndex(item => { |
100 | | - return item.file === test.file && |
101 | | - item.title === test.title; |
102 | | - }); |
103 | 166 | } |
104 | 167 |
|
105 | | - function isInUnstables (test) { |
106 | | - return findTestIndex(this.stats.unstables, test) > -1; |
107 | | - } |
108 | | - |
109 | | - runner.on(EVENT_RUN_BEGIN, function () { |
110 | | - Base.consoleLog(); |
111 | | - }); |
112 | | - |
113 | | - runner.on(EVENT_SUITE_BEGIN, function (suite) { |
114 | | - ++indents; |
115 | | - Base.consoleLog(color('suite', '%s%s'), indent(), suite.title); |
116 | | - }); |
| 168 | + SpecWithRetries.description = 'hierarchical & verbose & displays retried tests'; |
117 | 169 |
|
118 | | - runner.on(EVENT_SUITE_END, function () { |
119 | | - --indents; |
120 | | - if (indents === 1) |
121 | | - Base.consoleLog(); |
122 | | - }); |
123 | | - |
124 | | - runner.on(EVENT_TEST_PENDING, function (test) { |
125 | | - const fmt = indent() + color('pending', ' - %s'); |
126 | | - |
127 | | - Base.consoleLog(fmt, test.title); |
128 | | - }); |
129 | | - |
130 | | - runner.on(EVENT_TEST_PASS, function (test) { |
131 | | - let fmt; |
132 | | - |
133 | | - if (test.speed === 'fast') { |
134 | | - fmt = |
135 | | - indent() + |
136 | | - color('checkmark', ' ' + Base.symbols.ok) + |
137 | | - color('pass', ' %s'); |
138 | | - Base.consoleLog(fmt, test.title); |
139 | | - } |
140 | | - else { |
141 | | - fmt = |
142 | | - indent() + |
143 | | - color('checkmark', ' ' + Base.symbols.ok) + |
144 | | - color('pass', ' %s') + |
145 | | - color(test.speed, ' (%dms)'); |
146 | | - Base.consoleLog(fmt, test.title, test.duration); |
147 | | - } |
148 | | - }); |
149 | | - |
150 | | - runner.on(EVENT_TEST_FAIL, function (test) { |
151 | | - Base.consoleLog(indent() + color('fail', ' %d) %s'), ++n, test.title); |
152 | | - |
153 | | - const index = findTestIndex(self.stats.unstables, test); |
154 | | - |
155 | | - if (index > -1) |
156 | | - self.stats.unstables.splice(index, 1); |
157 | | - }); |
158 | | - |
159 | | - runner.on(EVENT_TEST_RETRY, test => { |
160 | | - if (!isInUnstables.call(self, test)) |
161 | | - self.stats.unstables.push(test); |
162 | | - }); |
163 | | - |
164 | | - runner.once(EVENT_RUN_END, () => { |
165 | | - epilogue.call(self); |
166 | | - }); |
| 170 | + return SpecWithRetries; |
167 | 171 | } |
168 | 172 |
|
169 | | -inherits(SpecWithRetries, Base); |
170 | | - |
171 | | -SpecWithRetries.description = 'hierarchical & verbose & displays retried tests'; |
| 173 | +module.exports = createReporter; |
0 commit comments