Skip to content

Commit

Permalink
Fixed nightwatchjs#1947 - a problem with testsuite before/after hooks…
Browse files Browse the repository at this point in the history
… when called without the callback
  • Loading branch information
beatfactor committed Dec 29, 2018
1 parent 22329c7 commit 5ace7c6
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 23 deletions.
4 changes: 0 additions & 4 deletions lib/core/asynctree.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,6 @@ class AsyncTree {
}
}

if (childNode) {
//return AsyncTree.getNextChildNode(childNode);
}

return false;
}

Expand Down
8 changes: 8 additions & 0 deletions lib/core/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class CommandQueue extends EventEmitter {
constructor() {
super();

this.isDone = false;
this.tree = new AsyncTree();
this.scheduleTimeoutId = null;
}
Expand Down Expand Up @@ -56,6 +57,9 @@ class CommandQueue extends EventEmitter {
.traverse()
.catch(err => {
return err;
})
.then(err => {
return err;
});
}, 0);
}
Expand Down Expand Up @@ -92,6 +96,10 @@ class CommandQueue extends EventEmitter {
}

done(err) {
if (this.tree.rootNode.childNodes.length > 0) {
return this;
}

this.emit('queue:finished', err);
this.deferred.resolve(err);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/testsuite/hooks/_basehook.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class BaseHook {
// For global hooks (and unit tests), when we have only one argument, the argument is the "done" callback:
// E.g.: (global) afterEach(done) {}
//
// For normal test hooks, the 1st argument is the 'api' object and the "done" callback is optional,
// For normal test hooks, the 1st argument is the 'client' object and the "done" callback is optional,
// and thus the callback is called implicitly if it's not passed explicitly, e.g.: after(client) {}
if (this.onlyApiArgPassed(argsCount)) {
this.implicitlyCallDoneCallback(doneFn);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
before(client) {
client.url('http://localhost').perform(function() {
client.globals.calls++;
});
},

demoTestAsyncOne : function (client) {
client.url('http://localhost');
},

after(client) {
client.end().perform(function() {
client.globals.calls++;
});
}
};
6 changes: 3 additions & 3 deletions test/src/runner/cli/testParallelExecution.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('test Parallel Execution', function() {
assert.ok(runner.test_settings.test_workers);

return runner.runTests().then(_ => {
assert.equal(allArgs.length, 23);
assert.equal(allArgs.length, 24);
assert.strictEqual(runner.concurrency.globalExitCode, 0);
});
});
Expand Down Expand Up @@ -132,7 +132,7 @@ describe('test Parallel Execution', function() {
});

return runner.runTests().then(_ => {
assert.equal(allArgs.length, 23);
assert.equal(allArgs.length, 24);
});
});

Expand Down Expand Up @@ -161,7 +161,7 @@ describe('test Parallel Execution', function() {
});

return runner.runTests().then(_ => {
assert.equal(allArgs.length, 23);
assert.equal(allArgs.length, 24);
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/src/runner/testRunTestcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('testRunTestcase', function() {
persist_globals: true,
globals: globals,
output_folder: false,
output: true,
output: false,
silent: false
};

Expand Down
22 changes: 11 additions & 11 deletions test/src/runner/testRunWithGlobalHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ describe('testRunWithGlobalHooks', function() {
afterEachCount++;
},
reporter(results, cb) {
assert.equal(globals.calls, 17);
assert.equal(beforeEachCount, 3);
assert.equal(afterEachCount, 3);
assert.equal(globals.calls, 19);
assert.equal(beforeEachCount, 4);
assert.equal(afterEachCount, 4);
cb();
}
};
Expand Down Expand Up @@ -86,9 +86,9 @@ describe('testRunWithGlobalHooks', function() {
}, 15);
},
reporter(results, cb) {
assert.equal(beforeEachCount, 3);
assert.equal(afterEachCount, 3);
assert.equal(globals.calls, 17);
assert.equal(beforeEachCount, 4);
assert.equal(afterEachCount, 4);
assert.equal(globals.calls, 19);
cb();
}
};
Expand Down Expand Up @@ -128,9 +128,9 @@ describe('testRunWithGlobalHooks', function() {
}, 10);
},
reporter(results, cb) {
assert.equal(globals.calls, 17);
assert.equal(beforeEachCount, 3);
assert.equal(afterEachCount, 3);
assert.equal(globals.calls, 19);
assert.equal(beforeEachCount, 4);
assert.equal(afterEachCount, 4);
cb();
}
};
Expand Down Expand Up @@ -175,8 +175,8 @@ describe('testRunWithGlobalHooks', function() {
},
reporter(results, cb) {
assert.ok(results.lastError instanceof Error);
assert.equal(results.failed, 3);
assert.equal(beforeEachCount, 3);
assert.equal(results.failed, 4);
assert.equal(beforeEachCount, 4);
cb();
}
},
Expand Down
33 changes: 32 additions & 1 deletion test/src/runner/testRunWithHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ describe('testRunWithHooks', function() {
if (results.lastError) {
throw results.lastError;
}
assert.equal(settings.globals.calls, 17);
assert.equal(settings.globals.calls, 19);
assert.ok('sampleWithBeforeAndAfter' in results.modules);

let result = results.modules.sampleWithBeforeAndAfter.completed;
Expand Down Expand Up @@ -190,6 +190,37 @@ describe('testRunWithHooks', function() {
return NightwatchClient.runTests(testsPath, settings);
});

it('testRunner before and after without callback', function() {
let testsPath = path.join(__dirname, '../../sampletests/before-after/sampleWithBeforeAndAfterNoCallback.js');
let globals = {
calls: 0,
reporter(results) {
if (results.lastError) {
throw results.lastError;
}
assert.equal(settings.globals.calls, 2);
assert.ok('sampleWithBeforeAndAfterNoCallback' in results.modules);
}
};

let settings = {
selenium: {
port: 10195,
version2: true,
start_process: true
},
seleniumPort: 10195,
silent: false,
output: false,
persist_globals: true,
globals: globals,
output_folder: false
};

return NightwatchClient.runTests(testsPath, settings);
});


it('testRunner with --testcase and before and after', function() {
let testsPath = path.join(__dirname, '../../sampletests/before-after/syncBeforeAndAfter.js');
let globals = {
Expand Down
4 changes: 2 additions & 2 deletions test/src/runner/testRunWithMultipleSources.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ describe('testRunWithMultipleSources', function() {
let globals = {
calls: 0,
reporter(results) {
assert.equal(globals.calls, 19);
assert.equal(Object.keys(results.modules).length, 4);
assert.equal(globals.calls, 21);
assert.equal(Object.keys(results.modules).length, 5);
assert.ok('sample' in results.modules);
assert.ok('sampleSingleTest' in results.modules);
assert.ok('sampleWithBeforeAndAfter' in results.modules);
Expand Down

0 comments on commit 5ace7c6

Please sign in to comment.