Skip to content

Commit 1c13ccc

Browse files
authored
Fix #3466 Wait for queue traversal to begin before calling done() (#3490)
* set tree to started immediately * if queue is not empty, wait for initial traversal to begin * remove unnecessary changes * add tests * fix tests
1 parent 3da1b1c commit 1c13ccc

File tree

6 files changed

+140
-4
lines changed

6 files changed

+140
-4
lines changed

lib/core/queue.js

+4
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ class CommandQueue extends EventEmitter {
107107
return this;
108108
}
109109

110+
get inProgress() {
111+
return this.tree.rootNode.childNodes.length > 0;
112+
}
113+
110114
done(err) {
111115
if (this.tree.rootNode.childNodes.length > 0) {
112116
return this;

lib/testsuite/hooks/_basehook.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,19 @@ class BaseHook {
8888
if (result instanceof Error) {
8989
result.skipTestCases = this.skipTestcasesOnError;
9090
}
91-
runnableDone && runnableDone(result);
92-
doneFn(result);
91+
92+
if (this.context.queue && this.context.queue.inProgress) {
93+
process.nextTick(() => {
94+
95+
this.context.queue.once('queue:finished', _ => {
96+
runnableDone && runnableDone(result);
97+
doneFn(result);
98+
});
99+
});
100+
} else {
101+
runnableDone && runnableDone(result);
102+
doneFn(result);
103+
}
93104
});
94105

95106
if (argsCount <= 1 && (invocationResult instanceof Promise)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var assert = require('assert');
2+
3+
module.exports = {
4+
before: function(client, cb) {
5+
client.assert.ok(false);
6+
client.globals.calls++;
7+
cb();
8+
},
9+
10+
beforeEach: function(client, cb) {
11+
client.globals.calls++;
12+
cb();
13+
},
14+
15+
demoTestSyncOne: function (client) {
16+
client.url('http://localhost');
17+
var testName = client.currentTest.name;
18+
assert.strictEqual(testName, 'demoTestSyncOne');
19+
},
20+
21+
demoTestSyncTwo: function (client) {
22+
var testName = client.currentTest.name;
23+
assert.strictEqual(testName, 'demoTestSyncTwo');
24+
client.end();
25+
},
26+
27+
afterEach: function(client) {
28+
client.globals.calls++;
29+
},
30+
31+
after: function(client) {
32+
client.globals.calls++;
33+
}
34+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var assert = require('assert');
2+
3+
module.exports = {
4+
before: function() {
5+
browser.assert.ok(false);
6+
browser.globals.calls++;
7+
},
8+
9+
beforeEach: function(client, cb) {
10+
client.globals.calls++;
11+
cb();
12+
},
13+
14+
demoTestSyncOne: function (client) {
15+
client.url('http://localhost');
16+
var testName = client.currentTest.name;
17+
assert.strictEqual(testName, 'demoTestSyncOne');
18+
},
19+
20+
demoTestSyncTwo: function (client) {
21+
var testName = client.currentTest.name;
22+
assert.strictEqual(testName, 'demoTestSyncTwo');
23+
client.end();
24+
},
25+
26+
afterEach: function(client) {
27+
client.globals.calls++;
28+
},
29+
30+
after: function(client) {
31+
client.globals.calls++;
32+
}
33+
};

test/src/runner/testRunWithHooks.js

+54
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,60 @@ describe('testRunWithHooks', function() {
300300
}));
301301
});
302302

303+
it('testRunner with command inside before', function() {
304+
let testsPath = path.join(__dirname, '../../sampletests/beforewithcommand/commandInsideBefore.js');
305+
let globals = {
306+
calls: 0,
307+
reporter(results) {
308+
assert.ok(results.lastError instanceof Error);
309+
310+
assert.strictEqual(globals.calls, 6);
311+
let result = results.modules.commandInsideBefore.completed;
312+
// assert.ok('demoTestSyncOne' in result);
313+
assert.ok(!('beforeEach' in result));
314+
assert.ok(!('before' in result));
315+
assert.ok(!('afterEach' in result));
316+
assert.ok(!('after' in result));
317+
}
318+
};
319+
320+
321+
return runTests({
322+
_source: [testsPath]
323+
}, settings({
324+
seleniumPort: 10195,
325+
globals,
326+
output_folder: false
327+
}));
328+
});
329+
330+
it('testRunner with command inside before with 0 parameters', function() {
331+
let testsPath = path.join(__dirname, '../../sampletests/beforewithcommand/commandInsideBeforeWithNoParams.js');
332+
let globals = {
333+
calls: 0,
334+
reporter(results) {
335+
assert.ok(results.lastError instanceof Error);
336+
337+
assert.strictEqual(globals.calls, 6);
338+
let result = results.modules.commandInsideBeforeWithNoParams.completed;
339+
// assert.ok('demoTestSyncOne' in result);
340+
assert.ok(!('beforeEach' in result));
341+
assert.ok(!('before' in result));
342+
assert.ok(!('afterEach' in result));
343+
assert.ok(!('after' in result));
344+
}
345+
};
346+
347+
348+
return runTests({
349+
_source: [testsPath]
350+
}, settings({
351+
seleniumPort: 10195,
352+
globals,
353+
output_folder: false
354+
}));
355+
});
356+
303357
it('testRunWithAsyncHooks', function() {
304358
let testsPath = path.join(__dirname, '../../sampletests/withasynchooks');
305359
let globals = {

test/src/runner/testRunnerWithAsyncAssertionFailure.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const {runTests} = common.require('index.js');
88

99
describe('test Runner with async and assertion failure', function() {
1010
const originalExit = process.exit;
11-
this.timeout(10000);
11+
this.timeout(15000);
1212

1313
beforeEach(function(done) {
1414
this.server = MockServer.init();
@@ -25,7 +25,7 @@ describe('test Runner with async and assertion failure', function() {
2525
Globals.afterEach.call(this, done);
2626
});
2727

28-
it('test run mocha samples with async and failures', function() {
28+
it('test run mocha samples with async and assertion failures', function() {
2929
const globals = {
3030
test_calls: 0,
3131
retryAssertionTimeout: 0,

0 commit comments

Comments
 (0)