Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit 953faf7

Browse files
committed
feat(runner): allow onPrepare functions to return a promise
If onPrepare is a function which returns a promise (or a file which exports a promise), the test runner will now wait for that promise to be fulfilled before starting tests.
1 parent 9dcd2ed commit 953faf7

File tree

8 files changed

+64
-13
lines changed

8 files changed

+64
-13
lines changed

lib/frameworks/cucumber.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ exports.run = function(runner, specs, done) {
5252
}
5353
global.cucumber = Cucumber.Cli(execOptions);
5454

55-
runner.controlFlow().execute(function() {
56-
runner.runTestPreparers();
57-
}, 'run test preparers').then(function() {
58-
55+
runner.runTestPreparers().then(function() {
5956
global.cucumber.run(function(succeeded) {
6057
if (runner.getConfig().onComplete) {
6158
runner.getConfig().onComplete();

lib/frameworks/jasmine.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ exports.run = function(runner, specs, done) {
3535
// get to complete first.
3636
jasmine.getEnv().addReporter(new RunnerReporter(runner));
3737

38-
runner.controlFlow().execute(function() {
39-
runner.runTestPreparers();
40-
}, 'run test preparers').then(function() {
38+
runner.runTestPreparers().then(function() {
4139
var jasmineNodeOpts = runner.getConfig().jasmineNodeOpts;
4240
var originalOnComplete = runner.getConfig().onComplete;
4341
jasmineNodeOpts.onComplete = function(jasmineRunner, log) {

lib/frameworks/mocha.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ exports.run = function(runner, specs, done) {
4343

4444
mocha.loadFiles();
4545

46-
runner.controlFlow().execute(function() {
47-
runner.runTestPreparers();
48-
}, 'run test preparers').then(function() {
46+
runner.runTestPreparers().then(function() {
4947

5048
specs.forEach(function(file) {
5149
mocha.addFile(file);

lib/runner.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,30 @@ util.inherits(Runner, EventEmitter);
5454
* @private
5555
* @param {Array} source The Array that we'll be iterating through
5656
* as we evaluate whether to require or execute each item.
57+
* @return {q.Promise} A promise that will resolve when the test preparers
58+
* are finished.
5759
*/
5860
Runner.prototype.runFilenamesOrFns_ = function(source) {
5961
var filenameOrFn;
62+
var promises = [];
63+
var returned;
6064
for (var i = 0; i < source.length; i++) {
6165
filenameOrFn = source[i];
6266
if (filenameOrFn) {
6367
if (typeof filenameOrFn === 'function') {
64-
filenameOrFn();
68+
returned = filenameOrFn();
6569
} else if (typeof filenameOrFn === 'string') {
66-
require(path.resolve(this.config_.configDir, filenameOrFn));
70+
returned = require(path.resolve(this.config_.configDir, filenameOrFn));
6771
} else {
6872
// TODO - this is not generic.
6973
throw 'config.onPrepare must be a string or function';
7074
}
75+
if (q.isPromiseAlike(returned)) {
76+
promises.push(returned);
77+
}
7178
}
7279
}
80+
return q.all(promises);
7381
};
7482

7583

@@ -86,9 +94,11 @@ Runner.prototype.registerTestPreparer = function(filenameOrFn) {
8694
/**
8795
* Executor of testPreparers
8896
* @public
97+
* @return {q.Promise} A promise that will resolve when the test preparers
98+
* are finished.
8999
*/
90100
Runner.prototype.runTestPreparers = function() {
91-
this.runFilenamesOrFns_(this.preparers_);
101+
return this.runFilenamesOrFns_(this.preparers_);
92102
};
93103

94104

scripts/test.js

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ var scripts = [
99
'node lib/cli.js spec/altRootConf.js',
1010
'node lib/cli.js spec/onPrepareConf.js',
1111
'node lib/cli.js spec/onPrepareFileConf.js',
12+
'node lib/cli.js spec/onPreparePromiseConf.js',
13+
'node lib/cli.js spec/onPreparePromiseFileConf.js',
1214
'node lib/cli.js spec/mochaConf.js',
1315
'node lib/cli.js spec/cucumberConf.js',
1416
'node lib/cli.js spec/withLoginConf.js',

spec/onPrepare/asyncstartup.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var q = require('q');
2+
3+
module.exports = q.fcall(function() {
4+
browser.params.password = '12345';
5+
}).delay(1000);

spec/onPreparePromiseConf.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Configuration using a function in onPrepare to set a parameter before
2+
// testing.
3+
var env = require('./environment.js');
4+
var q = require('q');
5+
6+
// The main suite of Protractor tests.
7+
exports.config = {
8+
seleniumAddress: env.seleniumAddress,
9+
10+
specs: [
11+
'onPrepare/*_spec.js'
12+
],
13+
14+
capabilities: env.capabilities,
15+
16+
baseUrl: env.baseUrl,
17+
18+
onPrepare: function() {
19+
return q.fcall(function() {
20+
browser.params.password = '12345';
21+
}).delay(1000);
22+
}
23+
};

spec/onPreparePromiseFileConf.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var env = require('./environment.js');
2+
3+
// Configuration using a string in onPrepare to load a file with code to
4+
// execute once before tests.
5+
exports.config = {
6+
seleniumAddress: env.seleniumAddress,
7+
8+
// Spec patterns are relative to this directory.
9+
specs: [
10+
'onPrepare/*_spec.js'
11+
],
12+
13+
capabilities: env.capabilities,
14+
15+
baseUrl: env.baseUrl,
16+
17+
onPrepare: 'onPrepare/asyncstartup.js'
18+
};

0 commit comments

Comments
 (0)