Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(testrunner): remove setup() helper #1584

Merged
merged 1 commit into from
Mar 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions utils/testrunner/TestRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ const TestResult = {
Crashed: 'crashed', // If testrunner crashed due to this test
};

function isTestFailure(testResult) {
return testResult === TestResult.Failed || testResult === TestResult.TimedOut || testResult === TestResult.Crashed;
}

function createHook(callback, name) {
const location = getCallerLocation(__filename);
return { name, body: callback, location };
Expand Down Expand Up @@ -104,6 +100,7 @@ class Test {

setSkipped(skipped) {
this._skipped = skipped;
return this;
}

focused() {
Expand All @@ -112,6 +109,7 @@ class Test {

setFocused(focused) {
this._focused = focused;
return this;
}

timeout() {
Expand All @@ -120,6 +118,7 @@ class Test {

setTimeout(timeout) {
this._timeout = timeout;
return this;
}

expectation() {
Expand All @@ -128,6 +127,7 @@ class Test {

setExpectation(expectation) {
this._expectation = expectation;
return this;
}

repeat() {
Expand All @@ -136,14 +136,17 @@ class Test {

setRepeat(repeat) {
this._repeat = repeat;
return this;
}

before(callback) {
this._hooks.push(createHook(callback, 'before'));
return this;
}

after(callback) {
this._hooks.push(createHook(callback, 'after'));
return this;
}

hooks(name) {
Expand Down Expand Up @@ -184,6 +187,7 @@ class Suite {

setSkipped(skipped) {
this._skipped = skipped;
return this;
}

focused() {
Expand All @@ -192,6 +196,7 @@ class Suite {

setFocused(focused) {
this._focused = focused;
return this;
}

location() {
Expand All @@ -204,6 +209,7 @@ class Suite {

setExpectation(expectation) {
this._expectation = expectation;
return this;
}

repeat() {
Expand All @@ -212,22 +218,27 @@ class Suite {

setRepeat(repeat) {
this._repeat = repeat;
return this;
}

beforeEach(callback) {
this._hooks.push(createHook(callback, 'beforeEach'));
return this;
}

afterEach(callback) {
this._hooks.push(createHook(callback, 'afterEach'));
return this;
}

beforeAll(callback) {
this._hooks.push(createHook(callback, 'beforeAll'));
return this;
}

afterAll(callback) {
this._hooks.push(createHook(callback, 'afterAll'));
return this;
}

hooks(name) {
Expand Down Expand Up @@ -647,12 +658,13 @@ class TestRunner extends EventEmitter {

this.describe = this._suiteBuilder([]);
this.it = this._testBuilder([]);
this.Expectations = { ...TestExpectation };

if (installCommonHelpers) {
this.fdescribe = this.describe.setup(s => s.setFocused(true));
this.xdescribe = this.describe.setup(s => s.setSkipped(true));
this.fit = this.it.setup(t => t.setFocused(true));
this.xit = this.it.setup(t => t.setSkipped(true));
this.fdescribe = this._suiteBuilder([{ callback: s => s.setFocused(true), args: [] }]);
this.xdescribe = this._suiteBuilder([{ callback: s => s.setSkipped(true), args: [] }]);
this.fit = this._testBuilder([{ callback: t => t.setFocused(true), args: [] }]);
this.xit = this._testBuilder([{ callback: t => t.setSkipped(true), args: [] }]);
}
}

Expand All @@ -666,10 +678,9 @@ class TestRunner extends EventEmitter {
callback(...suiteArgs);
this._suites.push(suite);
this._currentSuite = suite.parentSuite();
return suite;
}, {
get: (obj, prop) => {
if (prop === 'setup')
return callback => this._suiteBuilder([...callbacks, { callback, args: [] }]);
if (this._suiteModifiers.has(prop))
return (...args) => this._suiteBuilder([...callbacks, { callback: this._suiteModifiers.get(prop), args }]);
if (this._suiteAttributes.has(prop))
Expand All @@ -687,10 +698,9 @@ class TestRunner extends EventEmitter {
for (const { callback, args } of callbacks)
callback(test, ...args);
this._tests.push(test);
return test;
}, {
get: (obj, prop) => {
if (prop === 'setup')
return callback => this._testBuilder([...callbacks, { callback, args: [] }]);
if (this._testModifiers.has(prop))
return (...args) => this._testBuilder([...callbacks, { callback: this._testModifiers.get(prop), args }]);
if (this._testAttributes.has(prop))
Expand Down
6 changes: 4 additions & 2 deletions utils/testrunner/test/testrunner.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ module.exports.addTests = function({testRunner, expect}) {
it('should run a failed focused test', async() => {
const t = newTestRunner();
let run = false;
t.fit.setup(t => t.setExpectation(t.Expectations.Fail))('uno', () => {
t.it('uno', () => {
run = true; throw new Error('failure');
});
}).setFocused(true).setExpectation(t.Expectations.Fail);
expect(t.tests()[0].focused()).toBe(true);
expect(t.tests()[0].expectation()).toBe(t.Expectations.Fail);
const result = await t.run();
expect(run).toBe(true);
expect(result.runs.length).toBe(1);
Expand Down