From 224d950f4ddc16c0b0883d156dc359979b11c958 Mon Sep 17 00:00:00 2001 From: Nahuel Garbezza Date: Tue, 12 Jan 2021 07:00:13 -0300 Subject: [PATCH] :white_check_mark: add tests for errors and failures accessing in the test runner --- lib/test_runner.js | 28 +++++---------- tests/core/test_runner_test.js | 63 +++++++++++++++++++++++++++++----- 2 files changed, 63 insertions(+), 28 deletions(-) diff --git a/lib/test_runner.js b/lib/test_runner.js index 061d40e4..42f508ad 100644 --- a/lib/test_runner.js +++ b/lib/test_runner.js @@ -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; } @@ -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); @@ -65,7 +65,7 @@ class TestRunner { } setResultForCurrentTest(result) { - this.currentSuite().currentTest().setResult(result); + this._currentSuite.currentTest().setResult(result); } finish() { @@ -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()), [], ); } @@ -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); } } } diff --git a/tests/core/test_runner_test.js b/tests/core/test_runner_test.js index dcc66f01..4300cd12 100644 --- a/tests/core/test_runner_test.js +++ b/tests/core/test_runner_test.js @@ -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; @@ -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); }); }); @@ -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); }); }); });