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

Commit 6de2e32

Browse files
bclinkinbeardjuliemr
authored andcommitted
feat(runner): Add support for async onCleanUp functions
If the onCleanUp function returns a promise, the process will allow it to resolve before exiting. This is useful for performing async operations like writing to a file or calling an API at the end of a test run.
1 parent cd575ee commit 6de2e32

6 files changed

+73
-3
lines changed

lib/runner.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,12 @@ Runner.prototype.loadDriverProvider_ = function() {
140140
*/
141141
Runner.prototype.exit_ = function(exitCode) {
142142
if (typeof this.config_.onCleanUp === 'function') {
143-
this.config_.onCleanUp(exitCode);
143+
var val = this.config_.onCleanUp(exitCode);
144+
if (typeof val === 'number' || q.isPromiseAlike(val)) {
145+
return val;
146+
}
144147
}
148+
return exitCode;
145149
};
146150

147151

@@ -277,8 +281,7 @@ Runner.prototype.run = function() {
277281
}).then(function() {
278282
var passed = testResult.failedCount === 0;
279283
var exitCode = passed ? 0 : 1;
280-
self.exit_(exitCode);
281-
return exitCode;
284+
return q.when(self.exit_(exitCode));
282285
});
283286
};
284287

scripts/test.js

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ var scripts = [
77
'node lib/cli.js spec/basicConf.js',
88
'node lib/cli.js spec/multiConf.js',
99
'node lib/cli.js spec/altRootConf.js',
10+
'node lib/cli.js spec/onCleanUpAsyncReturnValueConf.js',
11+
'node lib/cli.js spec/onCleanUpNoReturnValueConf.js',
12+
'node lib/cli.js spec/onCleanUpSyncReturnValueConf.js',
1013
'node lib/cli.js spec/onPrepareConf.js',
1114
'node lib/cli.js spec/onPrepareFileConf.js',
1215
'node lib/cli.js spec/onPreparePromiseConf.js',

spec/onCleanUp/onCleanUp_spec.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('onCleanUp function in the config', function() {
2+
it('should not be affected by tests', function() {
3+
expect(true).toBe(true);
4+
});
5+
});

spec/onCleanUpAsyncReturnValueConf.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var env = require('./environment.js');
2+
var q = require('q');
3+
4+
// The main suite of Protractor tests.
5+
exports.config = {
6+
seleniumAddress: env.seleniumAddress,
7+
8+
specs: [
9+
'onCleanUp/*_spec.js'
10+
],
11+
12+
capabilities: env.capabilities,
13+
14+
baseUrl: env.baseUrl,
15+
16+
onCleanUp: function(exitCode) {
17+
var deferred = q.defer();
18+
setTimeout(function () {
19+
deferred.resolve(exitCode);
20+
}, 500);
21+
return deferred.promise;
22+
}
23+
};

spec/onCleanUpNoReturnValueConf.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var env = require('./environment.js');
2+
3+
// The main suite of Protractor tests.
4+
exports.config = {
5+
seleniumAddress: env.seleniumAddress,
6+
7+
specs: [
8+
'onCleanUp/*_spec.js'
9+
],
10+
11+
capabilities: env.capabilities,
12+
13+
baseUrl: env.baseUrl,
14+
15+
onCleanUp: function(exitCode) {
16+
// no return
17+
}
18+
};

spec/onCleanUpSyncReturnValueConf.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var env = require('./environment.js');
2+
3+
// The main suite of Protractor tests.
4+
exports.config = {
5+
seleniumAddress: env.seleniumAddress,
6+
7+
specs: [
8+
'onCleanUp/*_spec.js'
9+
],
10+
11+
capabilities: env.capabilities,
12+
13+
baseUrl: env.baseUrl,
14+
15+
onCleanUp: function(exitCode) {
16+
return exitCode;
17+
}
18+
};

0 commit comments

Comments
 (0)