Skip to content

Commit cbf65a9

Browse files
authored
test: chain test modifiers (#1175)
1 parent e3ec6b2 commit cbf65a9

File tree

1 file changed

+43
-36
lines changed

1 file changed

+43
-36
lines changed

utils/testrunner/TestRunner.js

+43-36
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ class UserCallback {
7171
const TestMode = {
7272
Run: 'run',
7373
Skip: 'skip',
74-
Focus: 'focus'
74+
Focus: 'focus',
75+
Fail: 'fail',
76+
Flake: 'flake'
7577
};
7678

7779
const TestResult = {
@@ -276,6 +278,39 @@ class TestPass {
276278
}
277279
}
278280

281+
function specBuilder(action) {
282+
let mode = TestMode.Run;
283+
let repeat = 1;
284+
285+
const func = (...args) => {
286+
for (let i = 0; i < repeat; ++i)
287+
action(mode, ...args);
288+
mode = TestMode.Run;
289+
repeat = 1;
290+
};
291+
292+
func.skip = condition => {
293+
if (condition)
294+
mode = TestMode.Skip;
295+
return func;
296+
};
297+
func.fail = condition => {
298+
if (condition)
299+
mode = TestMode.Fail;
300+
return func;
301+
};
302+
func.flake = condition => {
303+
if (condition)
304+
mode = TestMode.Flake;
305+
return func;
306+
};
307+
func.repeat = count => {
308+
repeat = count;
309+
return func;
310+
};
311+
return func;
312+
}
313+
279314
class TestRunner extends EventEmitter {
280315
constructor(options = {}) {
281316
super();
@@ -303,41 +338,13 @@ class TestRunner extends EventEmitter {
303338
}
304339
}
305340

306-
const duplicateTest = (amount, mode, timeout) => {
307-
return (name, callback) => {
308-
for (let i = 0; i < amount; ++i)
309-
this._addTest(name, callback, mode, timeout);
310-
}
311-
}
312-
313-
const duplicateSuite = (amount, mode) => {
314-
return (name, callback, ...args) => {
315-
for (let i = 0; i < amount; ++i)
316-
this._addSuite(mode, name, callback, ...args);
317-
}
318-
}
319-
320-
// bind methods so that they can be used as a DSL.
321-
this.describe = this._addSuite.bind(this, TestMode.Run);
322-
this.describe.skip = condition => condition ? this.xdescribe : this.describe;
323-
this.describe.repeat = number => duplicateSuite(number, TestMode.Run);
324-
this.fdescribe = this._addSuite.bind(this, TestMode.Focus);
325-
this.fdescribe.skip = () => this.fdescribe; // no-op
326-
this.fdescribe.repeat = number => duplicateSuite(number, TestMode.Focus);
327-
this.xdescribe = this._addSuite.bind(this, TestMode.Skip);
328-
this.xdescribe.skip = () => this.xdescribe; // no-op
329-
this.xdescribe.repeat = number => duplicateSuite(number, TestMode.Skip);
330-
331-
this.it = (name, callback) => void this._addTest(name, callback, TestMode.Run, this._timeout);
332-
this.it.skip = condition => condition ? this.xit : this.it;
333-
this.it.repeat = number => duplicateTest(number, TestMode.Run, this._timeout);
334-
this.fit = (name, callback) => void this._addTest(name, callback, TestMode.Focus, this._timeout);
335-
this.fit.skip = () => this.fit; // no-op
336-
this.fit.repeat = number => duplicateTest(number, TestMode.Focus, this._timeout);
337-
this.xit = (name, callback) => void this._addTest(name, callback, TestMode.Skip, this._timeout);
338-
this.xit.skip = () => this.xit; // no-op
339-
this.xit.repeat = number => duplicateTest(number, TestMode.Skip, this._timeout);
340-
341+
this.describe = specBuilder((mode, ...args) => this._addSuite(mode, ...args));
342+
this.fdescribe = specBuilder((mode, ...args) => this._addSuite(TestMode.Focus, ...args));
343+
this.xdescribe = specBuilder((mode, ...args) => this._addSuite(TestMode.Skip, ...args));
344+
this.it = specBuilder((mode, name, callback) => this._addTest(name, callback, mode, this._timeout));
345+
this.fit = specBuilder((mode, name, callback) => this._addTest(name, callback, TestMode.Focus, this._timeout));
346+
this.xit = specBuilder((mode, name, callback) => this._addTest(name, callback, TestMode.Skip, this._timeout));
347+
341348
this._debuggerLogBreakpointLines = new Multimap();
342349
this.dit = (name, callback) => {
343350
const test = this._addTest(name, callback, TestMode.Focus, INFINITE_TIMEOUT);

0 commit comments

Comments
 (0)