Skip to content

Commit

Permalink
postpone sending test finished events until "afterEach" hook is done …
Browse files Browse the repository at this point in the history
…(workaround for #mochajs/mocha/issues/1860)
  • Loading branch information
segrey committed Mar 16, 2016
1 parent c98a13f commit 03345ee
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
27 changes: 21 additions & 6 deletions lib/mochaIntellijReporter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var Tree = require('./mochaIntellijTree')
, util = require('./mochaIntellijUtil')
, treeUtil = require('./mochaTreeUtil')
, normalizer = require('./normalizer');
, normalizer = require('./normalizer')
, SingleElementQueue = require('./single-element-queue');

/**
* @param {Tree} tree
Expand Down Expand Up @@ -159,8 +160,9 @@ function addStdErr(testNode, err) {
* @param {Tree} tree
* @param {Object} test mocha test object
* @param {Object} err mocha error object
* @param {SingleElementQueue} [finishingQueue]
*/
function finishTestNode(tree, test, err) {
function finishTestNode(tree, test, err, finishingQueue) {
var testNode = treeUtil.getNodeForTest(test);
if (testNode != null && testNode.isFinished()) {
/* See https://youtrack.jetbrains.com/issue/WEB-10637
Expand Down Expand Up @@ -188,7 +190,12 @@ function finishTestNode(tree, test, err) {
var status = test.pending ? Tree.TestOutcome.SKIPPED : Tree.TestOutcome.SUCCESS;
testNode.setOutcome(status, test.duration, null, null, null, null);
}
testNode.finish(false);
if (finishingQueue != null) {
finishingQueue.add(testNode);
}
else {
testNode.finish(false);
}
}

/**
Expand Down Expand Up @@ -267,6 +274,10 @@ function IntellijReporter(runner) {

var executeSafely = util.executeSafely;
var tree;
// allows to postpone sending test finished event until 'afterEach' is done
var finishingQueue = new SingleElementQueue(function (testNode) {
testNode.finish(false);
});

runner.on('start', function () {
executeSafely(function () {
Expand Down Expand Up @@ -295,24 +306,26 @@ function IntellijReporter(runner) {

runner.on('test', function (test) {
executeSafely(function () {
finishingQueue.processAll();
startTest(tree, test);
});
});

runner.on('pending', function (test) {
executeSafely(function () {
finishTestNode(tree, test, null);
finishTestNode(tree, test, null, finishingQueue);
});
});

runner.on('pass', function (test) {
executeSafely(function () {
finishTestNode(tree, test, null);
finishTestNode(tree, test, null, finishingQueue);
});
});

runner.on('fail', function (test, err) {
executeSafely(function () {
finishingQueue.processAll();
if (isBeforeEachHook(test)) {
handleBeforeEachHookFailure(tree, test, err);
}
Expand All @@ -321,13 +334,14 @@ function IntellijReporter(runner) {
markChildrenFailed(tree, test.parent, test.title + " failed");
}
else {
finishTestNode(tree, test, err);
finishTestNode(tree, test, err, finishingQueue);
}
});
});

runner.on('suite end', function (suite) {
executeSafely(function () {
finishingQueue.processAll();
if (!suite.root) {
finishSuite(suite);
}
Expand All @@ -336,6 +350,7 @@ function IntellijReporter(runner) {

runner.on('end', function () {
executeSafely(function () {
finishingQueue.processAll();
tree = null;
});
});
Expand Down
25 changes: 25 additions & 0 deletions lib/single-element-queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @constructor
* @param {Function} processor
*/
function SingleElementQueue(processor) {
this.processor = processor;
this.current = null;
}

SingleElementQueue.prototype.add = function (element) {
if (this.current != null) {
process.stderr.write("mocha-intellij: unexpectedly unprocessed element " + element);
this.processor(this.current);
}
this.current = element;
};

SingleElementQueue.prototype.processAll = function () {
if (this.current != null) {
this.processor(this.current);
this.current = null;
}
};

module.exports = SingleElementQueue;

0 comments on commit 03345ee

Please sign in to comment.