☕️ Classic, reliable, trusted test framework for Node.js and the browser
Rework hook error tests to actually assert
Glen Mailer092ea4684478
diff --git a/test/integration/helpers.js b/test/integration/helpers.jsindex 7187d9d7..e6483674 100644--- a/test/integration/helpers.js+++ b/test/integration/helpers.js@@ -1,5 +1,6 @@var spawn = require('child_process').spawn;var path = require('path');+var fs = require('fs');module.exports = {/**@@ -35,6 +36,47 @@ module.exports = {});},+ /**+ * Invokes the mocha binary on the code of the body of the function.+ * Accepts an array of additional command line args to pass. The callback is+ * invoked with a summary of the run, in addition to its output. The summary+ * includes the number of passing, pending, and failing tests, as well as the+ * exit code. Useful for testing different reporters.+ *+ * Example response:+ * {+ * pending: 0,+ * passing: 0,+ * failing: 1,+ * code: 1,+ * output: '...'+ * }+ *+ * @param {function} fixture+ * @param {array} args+ * @param {function} fn+ */+ runMochaFunction: function(fixture, args, fn) {+ var path = resolveFixturePath(fixture.name + '.js' || 'tempfile.js');+ args = args || [];++ var fixtureContent = 'var fn = ' + fixture.toString() + '; fn()';+ fs.writeFileSync(path, fixtureContent, 'utf8');++ function cleanup() {+ fs.unlink(path);+ fn.apply(this, arguments);+ }++ invokeMocha(args.concat(['-C', path]), function(err, res) {+ if (err) {+ return cleanup(err);+ }++ cleanup(null, getSummary(res));+ });+ },+/*** Invokes the mocha binary for the given fixture using the JSON reporter,* returning the parsed output, as well as exit code.diff --git a/test/integration/hook.err.js b/test/integration/hook.err.jsindex d7e6c2e4..0e332952 100644--- a/test/integration/hook.err.js+++ b/test/integration/hook.err.js@@ -1,296 +1,360 @@-describe('hook error handling', function(){- // Lines in this test should be uncommented to see actual behavior- // You will also see errors in hooks- describe('before hook error', function() {- var calls = [];- describe('spec 1', function () {- describe('spec 1 nested', function () {- it('should not be called, because hook error was in a parent suite', function() {- calls.push('test nested');- })- })- before(function(){- calls.push('before');- // throw new Error('before hook error');- })- after(function(){- calls.push('after');- })- it('should not be called because of error in before hook', function() {- calls.push('test');- })- })- describe('spec 2', function () {- before(function(){- calls.push('before 2');- })- after(function(){- calls.push('after 2');- })- it('should be called, because hook error was in a sibling suite', function() {- calls.push('test 2');- })- })- after(function () {- // calls.should.eql(['before', 'after', 'before 2', 'test 2', 'after 2']);- })- })+var assert = require('assert');+var runMochaFunction = require('./helpers').runMochaFunction;- describe('before each hook error', function() {- var calls = [];- describe('spec 1', function () {- describe('spec 1 nested', function () {- it('should not be called, because hook error was in a parent suite', function() {- calls.push('test nested');- })- })- beforeEach(function(){- calls.push('before');- // throw new Error('before each hook error');- })- afterEach(function(){- calls.push('after');- })- it('should not be called because of error in before each hook', function() {- calls.push('test');- })- })- describe('spec 2', function () {- before(function(){- calls.push('before 2');- })- after(function(){- calls.push('after 2');- })- it('should be called, because hook error was in a sibling suite', function() {- calls.push('test 2');- })- })- after(function () {- // This should be called !- // calls.should.eql(['before', 'after', 'before 2', 'test 2', 'after 2']);- })- })+describe('hook error handling', function() {+ this.timeout(1000);- describe('after hook error', function() {- var calls = [];- describe('spec 1', function () {- describe('spec 1 nested', function () {- it('should be called, because hook error will happen after parent suite', function() {- calls.push('test nested');- })- })- before(function(){- calls.push('before');- })- after(function(){- calls.push('after');- // throw new Error('after hook error');- })- it('should be called because error is in after hook', function() {- calls.push('test');- })- })- describe('spec 2', function () {- before(function(){- calls.push('before 2');- })- after(function(){- calls.push('after 2');- })- it('should be called, because hook error was in a sibling suite', function() {- calls.push('test 2');- })- })- after(function () {- // Even this should be called !- // calls.should.eql(['before', 'test', 'test nested', 'after', 'before 2', 'test 2', 'after 2']);- })- })+ var lines;- describe('after each hook error', function() {- var calls = [];- describe('spec 1', function () {- describe('spec 1 nested', function () {- it('should not be called, because hook error has already happened in parent suite', function() {- calls.push('test nested');- })- })- beforeEach(function(){- calls.push('before');- })- afterEach(function(){- calls.push('after');- // throw new Error('after each hook error');- })- it('should be called because error is in after each hook, and this is the first test', function() {- calls.push('test');- })- it('should not be called because error is in after each hook, and this is the second test', function() {- calls.push('another test');- })- })- describe('spec 2', function () {- before(function(){- calls.push('before 2');- })- after(function(){- calls.push('after 2');- })- it('should be called, because hook error was in a sibling suite', function() {- calls.push('test 2');- })- })- after(function () {- // This should be called !- // calls.should.eql(['before', 'test', 'after', 'before 2', 'test 2', 'after 2']);- })- })+ describe('before hook error', function() {+ before(run(function beforeHookError() {+ describe('spec 1', function() {+ describe('spec 1 nested', function() {+ it('should not be called, because hook error was in a parent suite', function() {+ console.log('test nested');+ });+ });+ before(function() {+ console.log('before');+ throw new Error('before hook error');+ });+ after(function() {+ console.log('after');+ });+ it('should not be called because of error in before hook', function() {+ console.log('test');+ });+ });+ describe('spec 2', function() {+ before(function() {+ console.log('before 2');+ });+ after(function() {+ console.log('after 2');+ });+ it('should be called, because hook error was in a sibling suite', function() {+ console.log('test 2');+ });+ });+ }));- describe('multiple hook errors', function() {- var calls = [];- before(function(){- calls.push("root before");+ it('should verify results', function() {+ assert.deepEqual(+ lines,+ ['before', 'after', 'before 2', 'test 2', 'after 2']+ );});- beforeEach(function(){- calls.push("root before each");- });- describe('1', function(){- beforeEach(function() {- calls.push('1 before each')- })+ });- describe('1.1', function(){- before(function() {- calls.push('1.1 before');+ describe('before each hook error', function() {+ before(run(function beforeEachHookError() {+ describe('spec 1', function() {+ describe('spec 1 nested', function() {+ it('should not be called, because hook error was in a parent suite', function() {+ console.log('test nested');+ });});beforeEach(function() {- calls.push('1.1 before each')- // throw new Error('1.1 before each hook failed')+ console.log('before');+ throw new Error('before each hook error');});- it('1.1 test 1', function () {calls.push('1.1 test 1')});- it('1.1 test 2', function () {calls.push('1.1 test 2')});afterEach(function() {- calls.push("1.1 after each");+ console.log('after');+ });+ it('should not be called because of error in before each hook', function() {+ console.log('test');+ });+ });+ describe('spec 2', function() {+ before(function() {+ console.log('before 2');+ });+ after(function() {+ console.log('after 2');});- after(function(){- calls.push("1.1 after");- // throw new Error('1.1 after hook failed')+ it('should be called, because hook error was in a sibling suite', function() {+ console.log('test 2');});});+ }));+ it('should verify results', function() {+ assert.deepEqual(+ lines,+ ['before', 'after', 'before 2', 'test 2', 'after 2']+ );+ });+ });- describe('1.2', function(){+ describe('after hook error', function() {+ before(run(function afterHookError() {+ describe('spec 1', function() {+ describe('spec 1 nested', function() {+ it('should be called, because hook error will happen after parent suite', function() {+ console.log('test nested');+ });+ });before(function() {- calls.push('1.2 before');+ console.log('before');});- beforeEach(function() {- calls.push('1.2 before each')+ after(function() {+ console.log('after');+ throw new Error('after hook error');});- it('1.2 test 1', function () {calls.push('1.2 test 1')});- it('1.2 test 2', function () {calls.push('1.2 test 2')});- afterEach(function() {- calls.push("1.2 after each");- // throw new Error('1.2 after each hook failed')+ it('should be called because error is in after hook', function() {+ console.log('test');+ });+ });+ describe('spec 2', function() {+ before(function() {+ console.log('before 2');});- after(function(){- calls.push("1.2 after");+ after(function() {+ console.log('after 2');+ });+ it('should be called, because hook error was in a sibling suite', function() {+ console.log('test 2');});});+ }));+ it('should verify results', function() {+ assert.deepEqual(+ lines,+ ['before', 'test', 'test nested', 'after', 'before 2', 'test 2', 'after 2']+ );+ });+ });- afterEach(function() {- calls.push('1 after each')- })-- after(function(){- calls.push("1 after");+ describe('after each hook error', function() {+ before(run(function afterEachHookError() {+ describe('spec 1', function() {+ describe('spec 1 nested', function() {+ it('should be called, because hook error will happen after parent suite', function() {+ console.log('test nested');+ });+ });+ before(function() {+ console.log('before');+ });+ after(function() {+ console.log('after');+ throw new Error('after hook error');+ });+ it('should be called because error is in after hook', function() {+ console.log('test');+ });});- })+ describe('spec 2', function() {+ before(function() {+ console.log('before 2');+ });+ after(function() {+ console.log('after 2');+ });+ it('should be called, because hook error was in a sibling suite', function() {+ console.log('test 2');+ });+ });+ }));+ it('should verify results', function() {+ assert.deepEqual(+ lines,+ ['before', 'test', 'test nested', 'after', 'before 2', 'test 2', 'after 2']+ );+ });+ });- describe('2', function(){+ describe('multiple hook errors', function() {+ before(run(function multipleHookErrors() {+ before(function() {+ console.log('root before');+ });beforeEach(function() {- calls.push('2 before each')- // throw new Error('2 before each hook failed')- })+ console.log('root before each');+ });+ describe('1', function() {+ beforeEach(function() {+ console.log('1 before each');+ });- describe('2.1', function(){- before(function() {- calls.push('2.1 before');+ describe('1.1', function() {+ before(function() {+ console.log('1.1 before');+ });+ beforeEach(function() {+ console.log('1.1 before each');+ throw new Error('1.1 before each hook failed');+ });+ it('1.1 test 1', function() {+ console.log('1.1 test 1');+ });+ it('1.1 test 2', function() {+ console.log('1.1 test 2');+ });+ afterEach(function() {+ console.log('1.1 after each');+ });+ after(function() {+ console.log('1.1 after');+ throw new Error('1.1 after hook failed');+ });});- beforeEach(function() {- calls.push('2.1 before each')++ describe('1.2', function() {+ before(function() {+ console.log('1.2 before');+ });+ beforeEach(function() {+ console.log('1.2 before each');+ });+ it('1.2 test 1', function() {+ console.log('1.2 test 1');+ });+ it('1.2 test 2', function() {+ console.log('1.2 test 2');+ });+ afterEach(function() {+ console.log('1.2 after each');+ throw new Error('1.2 after each hook failed');+ });+ after(function() {+ console.log('1.2 after');+ });});- it('2.1 test 1', function () {calls.push('2.1 test 1')});- it('2.1 test 2', function () {calls.push('2.1 test 2')});+afterEach(function() {- calls.push("2.1 after each");+ console.log('1 after each');});- after(function(){- calls.push("2.1 after");++ after(function() {+ console.log('1 after');});});- describe('2.2', function(){- before(function() {- calls.push('2.2 before');- });+ describe('2', function() {beforeEach(function() {- calls.push('2.2 before each')+ console.log('2 before each');+ throw new Error('2 before each hook failed');});- it('2.2 test 1', function () {calls.push('2.2 test 1')});- it('2.2 test 2', function () {calls.push('2.2 test 2')});++ describe('2.1', function() {+ before(function() {+ console.log('2.1 before');+ });+ beforeEach(function() {+ console.log('2.1 before each');+ });+ it('2.1 test 1', function() {+ console.log('2.1 test 1');+ });+ it('2.1 test 2', function() {+ console.log('2.1 test 2');+ });+ afterEach(function() {+ console.log('2.1 after each');+ });+ after(function() {+ console.log('2.1 after');+ });+ });++ describe('2.2', function() {+ before(function() {+ console.log('2.2 before');+ });+ beforeEach(function() {+ console.log('2.2 before each');+ });+ it('2.2 test 1', function() {+ console.log('2.2 test 1');+ });+ it('2.2 test 2', function() {+ console.log('2.2 test 2');+ });+ afterEach(function() {+ console.log('2.2 after each');+ });+ after(function() {+ console.log('2.2 after');+ });+ });+afterEach(function() {- calls.push("2.2 after each");+ console.log('2 after each');+ throw new Error('2 after each hook failed');});- after(function(){- calls.push("2.2 after");++ after(function() {+ console.log('2 after');});});+ after(function() {+ console.log('root after');+ });afterEach(function() {- calls.push('2 after each')- // throw new Error('2 after each hook failed')- })-- after(function(){- calls.push("2 after");+ console.log('root after each');});- })-- after(function(){- calls.push("root after");- /* calls.should.eql([- "root before",- "1.1 before",- "root before each",- "1 before each",- "1.1 before each",- "1.1 after each",- "1 after each",- "root after each",- "1.1 after",- "1.2 before",- "root before each",- "1 before each",- "1.2 before each",- "1.2 test 1",- "1.2 after each",- "1 after each",- "root after each",- "1.2 after",- "1 after",- "2.1 before",- "root before each",- "2 before each",- "2 after each",- "root after each",- "2.1 after",- "2 after",- "root after"- ]); */- });- afterEach(function(){- calls.push("root after each");+ }));+ it('should verify results', function() {+ assert.deepEqual(+ lines,+ [+ 'root before',+ '1.1 before',+ 'root before each',+ '1 before each',+ '1.1 before each',+ '1.1 after each',+ '1 after each',+ 'root after each',+ '1.1 after',+ '1.2 before',+ 'root before each',+ '1 before each',+ '1.2 before each',+ '1.2 test 1',+ '1.2 after each',+ '1 after each',+ 'root after each',+ '1.2 after',+ '1 after',+ '2.1 before',+ 'root before each',+ '2 before each',+ '2 after each',+ 'root after each',+ '2.1 after',+ '2 after',+ 'root after'+ ]+ );});- })+ });++ function run(fn) {+ return function(done) {+ runMochaFunction(fn, [], function(err, res) {+ assert.ifError(err);++ lines = res.output+ .split(/[\n․]+/)+ .map(function(line) {+ return line.trim();+ })+ .filter(onlyConsoleOutput());++ done();+ });+ };+ }+});-})+function onlyConsoleOutput() {+ var foundSummary = false;+ return function(line) {+ if (!foundSummary) {+ foundSummary = !!(/\(\d+ms\)/).exec(line);+ }+ return !foundSummary && line.length > 0;+ };+}