Skip to content

Commit

Permalink
✅ add tests for errors and failures accessing in the test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
ngarbezza committed Jan 12, 2021
1 parent be41db9 commit 224d950
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 28 deletions.
28 changes: 9 additions & 19 deletions lib/test_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ class TestRunner {

registerTest(name, testBody, callbacks) {
const testToAdd = new Test(name, testBody, callbacks);
this.currentSuite().addTest(testToAdd);
this._currentSuite.addTest(testToAdd);
}

registerBefore(beforeBlock) {
this.currentSuite().before(beforeBlock);
this._currentSuite.before(beforeBlock);
}

registerAfter(afterBlock) {
this.currentSuite().after(afterBlock);
this._currentSuite.after(afterBlock);
}

addSuite(suiteToAdd) {
this.suites().push(suiteToAdd);
this._suites.push(suiteToAdd);
this._setCurrentSuite(suiteToAdd);
return suiteToAdd;
}
Expand All @@ -56,7 +56,7 @@ class TestRunner {

run() {
this._randomizeSuites();
this.suites().forEach(suite => {
this._suites.forEach(suite => {
this._setCurrentSuite(suite);
const context = { failFastMode: this._failFastMode, randomOrderMode: this._randomOrder };
suite.run(context);
Expand All @@ -65,7 +65,7 @@ class TestRunner {
}

setResultForCurrentTest(result) {
this.currentSuite().currentTest().setResult(result);
this._currentSuite.currentTest().setResult(result);
}

finish() {
Expand Down Expand Up @@ -102,18 +102,8 @@ class TestRunner {
return this._countEach('totalCount');
}

// Accessing

currentSuite() {
return this._currentSuite;
}

suites() {
return this._suites;
}

allFailuresAndErrors() {
return this.suites().reduce((failures, suite) =>
return this._suites.reduce((failures, suite) =>
failures.concat(suite.allFailuresAndErrors()), [],
);
}
Expand All @@ -129,14 +119,14 @@ class TestRunner {
}

_countEach(property) {
return this.suites().reduce((count, suite) =>
return this._suites.reduce((count, suite) =>
count + suite[property](), 0,
);
}

_randomizeSuites() {
if (this._randomOrder) {
shuffle(this.suites());
shuffle(this._suites);
}
}
}
Expand Down
63 changes: 54 additions & 9 deletions tests/core/test_runner_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@

const { suite, test, assert } = require('../../testy');
const TestRunner = require('../../lib/test_runner');
const { suiteNamed } = require('../support/suites_factory');
const { withRunner } = require('../support/runner_helpers');
const { aFailingTest } = require('../support/tests_factory');
const { suiteNamed } = require('../support/suites_factory');
const { aFailingTest, anErroredTest } = require('../support/tests_factory');

suite('test runner', () => {
const emptyRunnerCallbacks = {
onFinish: () => {},
onSuccess: () => {},
onFailure: () => {},
};

test('with no tests, it finishes with success', () => {
let result = 'not called';
let finished = false;
Expand All @@ -39,7 +33,9 @@ suite('test runner', () => {
test('failures count is zero with no tests', () => {
withRunner(runner => {
runner.run();


assert.isFalse(runner.hasErrorsOrFailures());
assert.isEmpty(runner.allFailuresAndErrors());
assert.that(runner.failuresCount()).isEqualTo(0);
});
});
Expand All @@ -52,7 +48,56 @@ suite('test runner', () => {
runner.addSuite(suite);
runner.run();

assert.isTrue(runner.hasErrorsOrFailures());
assert.that(runner.failuresCount()).isEqualTo(1);
assert.that(runner.allFailuresAndErrors()).includesExactly(failingTest);
});
});

test('errors count is zero with no tests', () => {
withRunner(runner => {
runner.run();

assert.isFalse(runner.hasErrorsOrFailures());
assert.isEmpty(runner.allFailuresAndErrors());
assert.that(runner.errorsCount()).isEqualTo(0);
});
});

test('errors count is one with an errored test', () => {
withRunner((runner, asserter) => {
const suite = suiteNamed('with one error');
const erroredTest = anErroredTest(asserter);
suite.addTest(erroredTest);
runner.addSuite(suite);
runner.run();

assert.isTrue(runner.hasErrorsOrFailures());
assert.that(runner.errorsCount()).isEqualTo(1);
assert.that(runner.allFailuresAndErrors()).includesExactly(erroredTest);
});
});

test('counting several errors and failures', () => {
withRunner((runner, asserter) => {
const suite = suiteNamed('with errors and failures');
const errorOne = anErroredTest(asserter);
const errorTwo = anErroredTest(asserter);
const errorThree = anErroredTest(asserter);
const failureOne = aFailingTest(asserter);
const failureTwo = aFailingTest(asserter);
suite.addTest(errorOne);
suite.addTest(failureOne);
suite.addTest(errorTwo);
suite.addTest(errorThree);
suite.addTest(failureTwo);
runner.addSuite(suite);
runner.run();

assert.that(runner.errorsCount()).isEqualTo(3);
assert.that(runner.failuresCount()).isEqualTo(2);
assert.isTrue(runner.hasErrorsOrFailures());
assert.that(runner.allFailuresAndErrors()).includesExactly(errorOne, errorTwo, errorThree, failureOne, failureTwo);
});
});
});

0 comments on commit 224d950

Please sign in to comment.