Skip to content

Commit

Permalink
Merge pull request #185 from ngarbezza/small-fixes
Browse files Browse the repository at this point in the history
Small fixes
  • Loading branch information
ngarbezza authored Jan 12, 2021
2 parents 4f0b915 + 224d950 commit 8cd56f9
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 28 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 Nahuel Garbezza
Copyright (c) (2018-2021) Nahuel Garbezza

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
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
76 changes: 75 additions & 1 deletion tests/core/test_runner_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

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

suite('test runner', () => {
test('with no tests, it finishes with success', () => {
Expand All @@ -12,7 +15,7 @@ suite('test runner', () => {
finished = true;
},
onSuccess: () => {
result = 'success';
result = 'success';
},
onFailure: () => {
result = 'failure';
Expand All @@ -26,4 +29,75 @@ suite('test runner', () => {
assert.isTrue(finished);
assert.that(result).isEqualTo('success');
});

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);
});
});

test('failures count is one with one failed test', () => {
withRunner((runner, asserter) => {
const suite = suiteNamed('with one failure');
const failingTest = aFailingTest(asserter);
suite.addTest(failingTest);
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);
});
});
});
9 changes: 2 additions & 7 deletions tests/core/test_suite_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@

const { suite, test, before, assert } = require('../../testy');
const TestSuite = require('../../lib/test_suite');
const { withRunner } = require('../support/runner_helpers');
const FailFast = require('../../lib/fail_fast');
const { withRunner } = require('../support/runner_helpers');
const { newEmptySuite } = require('../support/suites_factory');
const { aPassingTest, aFailingTest, anErroredTest, aPendingTest } = require('../support/tests_factory');

const noop = () => {};
const emptySuiteCallbacks = { onStart: noop, onFinish: noop };

const newEmptySuite = () => suiteNamed('myTestSuite');
const suiteNamed = suiteName => new TestSuite(suiteName, () => {}, emptySuiteCallbacks);

suite('test suite behavior', () => {
let runner, mySuite;
let passingTest, failingTest, erroredTest, pendingTest;
Expand Down
20 changes: 20 additions & 0 deletions tests/support/suites_factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const TestSuite = require('../../lib/test_suite');

const noop = () => {};
const emptySuiteCallbacks = {
onStart: noop,
onFinish: noop,
};

const newEmptySuite = () =>
suiteNamed('myTestSuite');

const suiteNamed = suiteName =>
new TestSuite(suiteName, () => {}, emptySuiteCallbacks);

module.exports = {
newEmptySuite,
suiteNamed,
};

0 comments on commit 8cd56f9

Please sign in to comment.