From 3e5d9867f52a63240d68ef8b551a868df7fb4592 Mon Sep 17 00:00:00 2001 From: Samuel Reed Date: Tue, 4 Sep 2018 10:20:47 -0500 Subject: [PATCH 1/2] Fix #40 - afterEach unregistered in parent context --- lib/parallel.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/parallel.js b/lib/parallel.js index fc6be65..ad1b8f9 100644 --- a/lib/parallel.js +++ b/lib/parallel.js @@ -391,7 +391,7 @@ function enableEachHooks(context) { delete context._disabledAfterEach; if (!context.parent) return; - disableEachHooks(context.parent); + enableEachHooks(context.parent); } /** From b23fedc330be70f62d0965ac2022ed1472326738 Mon Sep 17 00:00:00 2001 From: Samuel Reed Date: Tue, 4 Sep 2018 10:48:49 -0500 Subject: [PATCH 2/2] Add test for previous Also added missing mocha devDep --- package.json | 3 +- spec/fixtures/parentHooks.js | 16 ++++- spec/fixtures/parentHooksRegression.js | 97 ++++++++++++++++++++++++++ spec/spec.js | 40 ++++++++++- 4 files changed, 153 insertions(+), 3 deletions(-) create mode 100644 spec/fixtures/parentHooksRegression.js diff --git a/package.json b/package.json index 5fb381e..f49fa43 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "semaphore": "^1.0.5" }, "devDependencies": { - "dirmap": "0.0.2" + "dirmap": "0.0.2", + "mocha": "^5.2.0" } } diff --git a/spec/fixtures/parentHooks.js b/spec/fixtures/parentHooks.js index af4a94b..60e59e9 100644 --- a/spec/fixtures/parentHooks.js +++ b/spec/fixtures/parentHooks.js @@ -20,6 +20,13 @@ describe('suiteA', function() { }, 50); }); + afterEach(function(done) { + process.stdout.write('suiteAAfterEach, '); + setTimeout(function() { + done(); + }, 50); + }); + describe('suiteB', function() { beforeEach(function(done) { process.stdout.write('suiteBBeforeEach, '); @@ -29,6 +36,13 @@ describe('suiteA', function() { }, 50); }); + afterEach(function(done) { + process.stdout.write('suiteBAfterEach, '); + setTimeout(function() { + done(); + }, 50); + }); + parallel('hooks', function() { beforeEach(function(done) { process.stdout.write('childBeforeEach, '); @@ -49,7 +63,7 @@ describe('suiteA', function() { it('test2', function(done) { process.stdout.write('test2\n'); - // Incremented by before, 6x beforeEach + // Incremented by before and 6x beforeEach setTimeout(function() { assert.equal(i, 7); done(); diff --git a/spec/fixtures/parentHooksRegression.js b/spec/fixtures/parentHooksRegression.js new file mode 100644 index 0000000..ea10ee6 --- /dev/null +++ b/spec/fixtures/parentHooksRegression.js @@ -0,0 +1,97 @@ +var assert = require('assert'); +var parallel = require('../../lib/parallel'); + +describe('parent hooks regression #47', function() { + + beforeEach(function() { + process.stdout.write('globalBeforeEach, ') + }); + + afterEach(function() { + process.stdout.write('globalAfterEach, ') + }); + + describe('run parallel', function() { + parallel('trigger hooks removal', function() { + it('one', function() { + assert(true); + }); + it('two', function() { + assert(true); + }); + }) + }); + + describe('suiteA', function() { + var i = 0; + + before(function(done) { + setTimeout(function() { + assert.equal(i, 0); + i++; + done(); + }, 100); + }); + + beforeEach(function(done) { + process.stdout.write('suiteABeforeEach, '); + setTimeout(function() { + i++; + done(); + }, 50); + }); + + afterEach(function(done) { + process.stdout.write('suiteAAfterEach, '); + setTimeout(function() { + done(); + }, 50); + }); + + describe('suiteB', function() { + beforeEach(function(done) { + process.stdout.write('suiteBBeforeEach, '); + setTimeout(function() { + i++; + done(); + }, 50); + }); + + afterEach(function(done) { + process.stdout.write('suiteBAfterEach, '); + setTimeout(function() { + done(); + }, 50); + }); + + describe('hooks', function() { + beforeEach(function(done) { + process.stdout.write('childBeforeEach, '); + setTimeout(function() { + i++; + done(); + }, 50); + }); + + it('test1', function(done) { + process.stdout.write('test1, '); + // Incremented by before and 4x beforeEach + setTimeout(function() { + assert.equal(i, 4); + done(); + }, 1000); + }); + + it('test2', function(done) { + process.stdout.write('test2\n'); + // Incremented by before and 6x beforeEach + setTimeout(function() { + assert.equal(i, 7); + done(); + }, 1000); + }); + }); + }); + }); + +}); diff --git a/spec/spec.js b/spec/spec.js index 29c1495..4f7eca2 100644 --- a/spec/spec.js +++ b/spec/spec.js @@ -84,6 +84,8 @@ describe('parallel', function() { it('supports parent hooks', function(done) { var hookStr = 'suiteABeforeEach, suiteBBeforeEach, suiteABeforeEach, ' + 'suiteBBeforeEach, childBeforeEach, childBeforeEach'; + var afterHookStr = 'suiteBAfterEach, suiteAAfterEach, ' + + 'suiteBAfterEach, suiteAAfterEach'; run(fixtures.parentHooks, function(err, stdout, stderr) { if (err) return done(err); @@ -91,12 +93,38 @@ describe('parallel', function() { assert(!stderr.length); assert(stdout.indexOf('2 passing') !== -1); assert(stdout.indexOf(hookStr) !== -1); + assert(stdout.indexOf(afterHookStr) !== -1); assertOneSecond(stdout); done(); }); }); + // https://github.com/danielstjules/mocha.parallel/pull/47 + it('parent hooks regression after parallel unregistered', function(done) { + var primerStr = 'trigger hooks removal\n' + + 'globalBeforeEach, globalBeforeEach, globalAfterEach, globalAfterEach,' + var test1Str = 'hooks\nglobalBeforeEach, suiteABeforeEach, suiteBBeforeEach, ' + + 'childBeforeEach, test1,'; + var test2Str = 'suiteBAfterEach, suiteAAfterEach, globalAfterEach, ' + + 'globalBeforeEach, suiteABeforeEach, suiteBBeforeEach, childBeforeEach, test2'; + var afterHookStr = 'suiteBAfterEach, suiteAAfterEach, globalAfterEach'; + + run(fixtures.parentHooksRegression, function(err, stdout, stderr) { + if (err) return done(err); + + assert(!stderr.length); + assert(stdout.indexOf('4 passing') !== -1); + assert(stdout.indexOf(primerStr) !== -1); + assert(stdout.indexOf(test1Str) !== -1); + assert(stdout.indexOf(test2Str) !== -1); + assert(stdout.indexOf(afterHookStr) !== -1); + assertSeconds(stdout, 3); // takes longer + + done(); + }); + }); + it('correctly handles the readme example', function(done) { run(fixtures.hooksExample, function(err, stdout, stderr) { if (err) return done(err); @@ -351,6 +379,16 @@ function assertSubstrings(str, substrings) { * @param {string} stdout */ function assertOneSecond(stdout) { + return assertSeconds(stdout, 1); +} + +/** + * Asserts that a given test suite ran for some number of seconds. + * + * @param {string} stdout + * @param {number} seconds + */ +function assertSeconds(stdout, seconds) { var timeStr = stdout.match(/passing \((\d+)s\)/)[1]; - assert(parseInt(timeStr, 10) === 1); + assert(parseInt(timeStr, 10) === seconds); }