Skip to content

Commit d721596

Browse files
committed
fix build
1 parent c5fa096 commit d721596

3 files changed

Lines changed: 152 additions & 146 deletions

File tree

Lines changed: 136 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,171 +1,173 @@
1-
const Mocha = require('mocha');
2-
31
function formatMilliseconds (duration) {
42
if (duration < 1000)
53
return `${duration}ms`;
64

75
return `${(duration / 1000).toFixed(2).replace(/\.?0+$/, '')}s`;
86
}
97

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');
2010

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;
2221

23-
const Base = Mocha.reporters.Base;
24-
const color = Base.color;
22+
const Base = Mocha.reporters.Base;
23+
const color = Base.color;
2524

26-
exports = module.exports = SpecWithRetries;
25+
class SpecWithRetries extends Base {
26+
constructor (runner, options) {
27+
super(runner, options);
2728

28-
function SpecWithRetries (runner, options) {
29-
Base.call(this, runner, options);
29+
this.stats.unstables = [];
3030

31-
this.stats.unstables = [];
31+
const self = this;
32+
let indents = 0;
33+
let n = 0;
3234

33-
const self = this;
34-
let indents = 0;
35-
let n = 0;
35+
function indent () {
36+
return Array(indents).join(' ');
37+
}
3638

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+
}
4042

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;
4446

45-
function epilogue () {
46-
const stats = this.stats;
47-
let fmt;
47+
Base.consoleLog();
4848

49-
Base.consoleLog();
49+
// passes
50+
fmt =
51+
color('bright pass', ' ') +
52+
color('green', ' %d passing') +
53+
color('light', ' (%s)');
5054

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));
5656

57-
Base.consoleLog(fmt, stats.passes || 0, formatMilliseconds(stats.duration));
57+
// pending
58+
if (stats.pending) {
59+
fmt = color('pending', ' ') + color('pending', ' %d pending');
5860

59-
// pending
60-
if (stats.pending) {
61-
fmt = color('pending', ' ') + color('pending', ' %d pending');
61+
Base.consoleLog(fmt, stats.pending);
62+
}
6263

63-
Base.consoleLog(fmt, stats.pending);
64-
}
64+
// failures
65+
if (stats.failures) {
66+
fmt = color('fail', ' %d failing');
6567

66-
// failures
67-
if (stats.failures) {
68-
fmt = color('fail', ' %d failing');
68+
Base.consoleLog(fmt, stats.failures);
6969

70-
Base.consoleLog(fmt, stats.failures);
70+
Base.list(this.failures);
71+
}
7172

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):');
7478

75-
// unstable tests
76-
if (stats.unstables.length) {
77-
Base.consoleLog();
79+
Base.consoleLog(fmt);
7880

79-
fmt = color('bright yellow', ' Unstable test(s):');
81+
const groupedByFile = groupBy(stats.unstables, unstable => unstable.file);
8082

81-
Base.consoleLog(fmt);
83+
Object.entries(groupedByFile)
84+
.forEach(([key, value]) => {
85+
Base.consoleLog(color('bright yellow', ' %s'), key);
8286

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+
}
8492

85-
Object.entries(groupedByFile)
86-
.forEach(([key, value]) => {
87-
Base.consoleLog(color('bright yellow', ' %s'), key);
93+
Base.consoleLog();
94+
}
8895

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;
92100
});
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+
});
93165
}
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-
});
103166
}
104167

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';
117169

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;
167171
}
168172

169-
inherits(SpecWithRetries, Base);
170-
171-
SpecWithRetries.description = 'hierarchical & verbose & displays retried tests';
173+
module.exports = createReporter;

gulp/helpers/test-functional.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
const { castArray } = require('lodash');
2-
const getTimeout = require('./get-timeout');
3-
const chai = require('chai');
4-
const globby = require('globby');
5-
const Mocha = require('mocha');
6-
const SpecWithRetries = require('./mocha-reporter-spec-with-retries');
1+
const { castArray } = require('lodash');
2+
const getTimeout = require('./get-timeout');
3+
const chai = require('chai');
4+
const globby = require('globby');
5+
const createSpecReporter = require('./mocha-reporter-spec-with-retries');
76

87
const {
98
TESTS_GLOB,
@@ -29,6 +28,9 @@ function getGroupOfTests (tests, groupNumber, groupsCount) {
2928
}
3029

3130
module.exports = async function testFunctional (src, testingEnvironmentName, { nativeAutomation } = {}) {
31+
const { default: Mocha } = await import('mocha');
32+
const SpecWithRetries = await createSpecReporter();
33+
3234
process.env.TESTING_ENVIRONMENT = testingEnvironmentName;
3335
process.env.BROWSERSTACK_USE_AUTOMATE = 1;
3436

test/server/mocha-reporter-spec-with-retries-test.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
const { expect } = require('chai');
2-
const path = require('path');
3-
const util = require('util');
4-
const Mocha = require('mocha');
5-
const SpecWithRetries = require('../../gulp/helpers/mocha-reporter-spec-with-retries');
1+
const { expect } = require('chai');
2+
const path = require('path');
3+
const util = require('util');
4+
const createSpecReporter = require('../../gulp/helpers/mocha-reporter-spec-with-retries');
5+
6+
async function runSuitesWithReporter (files) {
7+
const { default: Mocha } = await import('mocha');
8+
const SpecWithRetries = await createSpecReporter();
69

7-
function runSuitesWithReporter (files) {
810
return new Promise((resolve, reject) => {
911
const output = [];
1012
const originalConsoleLog = Mocha.reporters.Base.consoleLog;

0 commit comments

Comments
 (0)