test: chain test modifiers (#1175)

This commit is contained in:
Pavel Feldman 2020-03-02 11:18:42 -08:00 committed by GitHub
parent e3ec6b28a9
commit cbf65a9c56
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -71,7 +71,9 @@ class UserCallback {
const TestMode = { const TestMode = {
Run: 'run', Run: 'run',
Skip: 'skip', Skip: 'skip',
Focus: 'focus' Focus: 'focus',
Fail: 'fail',
Flake: 'flake'
}; };
const TestResult = { const TestResult = {
@ -276,6 +278,39 @@ class TestPass {
} }
} }
function specBuilder(action) {
let mode = TestMode.Run;
let repeat = 1;
const func = (...args) => {
for (let i = 0; i < repeat; ++i)
action(mode, ...args);
mode = TestMode.Run;
repeat = 1;
};
func.skip = condition => {
if (condition)
mode = TestMode.Skip;
return func;
};
func.fail = condition => {
if (condition)
mode = TestMode.Fail;
return func;
};
func.flake = condition => {
if (condition)
mode = TestMode.Flake;
return func;
};
func.repeat = count => {
repeat = count;
return func;
};
return func;
}
class TestRunner extends EventEmitter { class TestRunner extends EventEmitter {
constructor(options = {}) { constructor(options = {}) {
super(); super();
@ -303,40 +338,12 @@ class TestRunner extends EventEmitter {
} }
} }
const duplicateTest = (amount, mode, timeout) => { this.describe = specBuilder((mode, ...args) => this._addSuite(mode, ...args));
return (name, callback) => { this.fdescribe = specBuilder((mode, ...args) => this._addSuite(TestMode.Focus, ...args));
for (let i = 0; i < amount; ++i) this.xdescribe = specBuilder((mode, ...args) => this._addSuite(TestMode.Skip, ...args));
this._addTest(name, callback, mode, timeout); this.it = specBuilder((mode, name, callback) => this._addTest(name, callback, mode, this._timeout));
} this.fit = specBuilder((mode, name, callback) => this._addTest(name, callback, TestMode.Focus, this._timeout));
} this.xit = specBuilder((mode, name, callback) => this._addTest(name, callback, TestMode.Skip, this._timeout));
const duplicateSuite = (amount, mode) => {
return (name, callback, ...args) => {
for (let i = 0; i < amount; ++i)
this._addSuite(mode, name, callback, ...args);
}
}
// bind methods so that they can be used as a DSL.
this.describe = this._addSuite.bind(this, TestMode.Run);
this.describe.skip = condition => condition ? this.xdescribe : this.describe;
this.describe.repeat = number => duplicateSuite(number, TestMode.Run);
this.fdescribe = this._addSuite.bind(this, TestMode.Focus);
this.fdescribe.skip = () => this.fdescribe; // no-op
this.fdescribe.repeat = number => duplicateSuite(number, TestMode.Focus);
this.xdescribe = this._addSuite.bind(this, TestMode.Skip);
this.xdescribe.skip = () => this.xdescribe; // no-op
this.xdescribe.repeat = number => duplicateSuite(number, TestMode.Skip);
this.it = (name, callback) => void this._addTest(name, callback, TestMode.Run, this._timeout);
this.it.skip = condition => condition ? this.xit : this.it;
this.it.repeat = number => duplicateTest(number, TestMode.Run, this._timeout);
this.fit = (name, callback) => void this._addTest(name, callback, TestMode.Focus, this._timeout);
this.fit.skip = () => this.fit; // no-op
this.fit.repeat = number => duplicateTest(number, TestMode.Focus, this._timeout);
this.xit = (name, callback) => void this._addTest(name, callback, TestMode.Skip, this._timeout);
this.xit.skip = () => this.xit; // no-op
this.xit.repeat = number => duplicateTest(number, TestMode.Skip, this._timeout);
this._debuggerLogBreakpointLines = new Multimap(); this._debuggerLogBreakpointLines = new Multimap();
this.dit = (name, callback) => { this.dit = (name, callback) => {