Skip to content

Commit c0f13d2

Browse files
committed
refactor(asyncTestFn): refactor async test wrapping to show more info
Test wrapping for Jasmine 2 now more closely follows the test wrapping for Mocha at https://github.com/SeleniumHQ/selenium/blob/master/javascript/node/selenium-webdriver/testing/index.js This also adds more information to the task names in the control flow, for easier debugging.
1 parent 759dc73 commit c0f13d2

File tree

3 files changed

+31
-29
lines changed

3 files changed

+31
-29
lines changed

Diff for: index.js

+26-28
Original file line numberDiff line numberDiff line change
@@ -72,34 +72,32 @@ function wrapInControlFlow(globalFn, fnName) {
7272
var driverError = new Error();
7373
driverError.stack = driverError.stack.replace(/ +at.+jasminewd.+\n/, '');
7474

75-
function asyncTestFn(fn) {
75+
function asyncTestFn(fn, description) {
76+
description = description ? ('("' + description + '")') : '';
7677
return function(done) {
77-
// deferred object for signaling completion of asychronous function within globalFn
78-
var asyncFnDone = webdriver.promise.defer();
78+
var async = fn.length > 0;
79+
testFn = fn.bind(this);
7980

80-
if (fn.length === 0) {
81-
// function with globalFn not asychronous
82-
asyncFnDone.fulfill();
83-
} else if (fn.length > 1) {
84-
throw Error('Invalid # arguments (' + fn.length + ') within function "' + fnName +'"');
85-
} else {
86-
// Add fail method to async done callback and override env fail to
87-
// reject async done promise
88-
asyncFnDone.fulfill.fail = function(userError) {
89-
asyncFnDone.reject(new Error(userError));
90-
};
91-
92-
}
93-
94-
var flowFinished = flow.execute(function() {
95-
fn.call(jasmine.getEnv(), asyncFnDone.fulfill);
96-
}, 'Run ' + fnName + ' in control flow');
97-
98-
webdriver.promise.all([asyncFnDone, flowFinished]).then(function() {
99-
seal(done)();
100-
}, function(e) {
101-
e.stack = e.stack + '\n==== async task ====\n' + driverError.stack;
102-
done.fail(e);
81+
flow.execute(function controlFlowExecute() {
82+
return new webdriver.promise.Promise(function(fulfill, reject) {
83+
if (async) {
84+
// If testFn is async (it expects a done callback), resolve the promise of this
85+
// test whenever that callback says to. Any promises returned from testFn are
86+
// ignored.
87+
var proxyDone = fulfill;
88+
proxyDone.fail = function(err) {
89+
reject(err);
90+
};
91+
testFn(proxyDone);
92+
} else {
93+
// Without a callback, testFn can return a promise, or it will
94+
// be assumed to have completed synchronously.
95+
fulfill(testFn());
96+
}
97+
}, flow);
98+
}, 'Run ' + fnName + description + ' in control flow').then(seal(done), function(err) {
99+
err.stack = err.stack + '\n==== async task ====\n' + driverError.stack;
100+
done.fail(err);
103101
});
104102
};
105103
}
@@ -111,10 +109,10 @@ function wrapInControlFlow(globalFn, fnName) {
111109
description = validateString(arguments[0]);
112110
func = validateFunction(arguments[1]);
113111
if (!arguments[2]) {
114-
globalFn(description, asyncTestFn(func));
112+
globalFn(description, asyncTestFn(func, description));
115113
} else {
116114
timeout = validateNumber(arguments[2]);
117-
globalFn(description, asyncTestFn(func), timeout);
115+
globalFn(description, asyncTestFn(func, description), timeout);
118116
}
119117
break;
120118
case 'beforeEach':

Diff for: scripts/test.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ $CMD
88
[ "$?" -eq 0 ] || exit 1
99
echo
1010

11-
EXPECTED_RESULTS="13 specs, 12 failures"
11+
EXPECTED_RESULTS="14 specs, 13 failures"
1212
echo "### running failing specs (expecting $EXPECTED_RESULTS)"
1313
CMD=$CMD_BASE$FAILING_SPECS
1414
res=`$CMD 2>/dev/null`

Diff for: spec/errorSpec.js

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ describe('things that should fail', function() {
3838
expect(true).toBe(false);
3939
});
4040

41+
it('should fail when an error is thrown', function() {
42+
throw new Error('I am an intentional error');
43+
});
44+
4145
it('should compare a promise to a primitive', function() {
4246
expect(fakeDriver.getValueA()).toEqual('d');
4347
expect(fakeDriver.getValueB()).toEqual('e');

0 commit comments

Comments
 (0)