Skip to content

Commit

Permalink
chore(promises): Wait for promises explicitly
Browse files Browse the repository at this point in the history
See angular#68 for details
  • Loading branch information
sjelin committed Nov 23, 2016
1 parent e05cd3c commit d45aa6e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
16 changes: 11 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,26 @@ function wrapInControlFlow(flow, globalFn, fnName) {

flow.execute(function controlFlowExecute() {
return new webdriver.promise.Promise(function(fulfill, reject) {
function wrappedReject(err) {
var wrappedErr = new Error(err);
reject(wrappedErr);
}
if (async) {
// If testFn is async (it expects a done callback), resolve the promise of this
// test whenever that callback says to. Any promises returned from testFn are
// ignored.
var proxyDone = fulfill;
proxyDone.fail = function(err) {
var wrappedErr = new Error(err);
reject(wrappedErr);
};
proxyDone.fail = wrappedReject;
testFn(proxyDone);
} else {
// Without a callback, testFn can return a promise, or it will
// be assumed to have completed synchronously.
fulfill(testFn());
var ret = testFn();
if (webdriver.promise.isPromise(ret)) {
ret.then(fulfill, wrappedReject);
} else {
fulfill(ret);
}
}
}, flow);
}, 'Run ' + fnName + description + ' in control flow').then(seal(done), function(err) {
Expand Down
20 changes: 20 additions & 0 deletions spec/adapterSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,24 @@ describe('webdriverJS Jasmine adapter', function() {
expect(spec3.description).toBe('test3');
});
});

describe('should handle returning native promises', () => {
var currentTest = null;

it('test A', () => {
currentTest = 'A';
return new Promise((resolve) => {
setTimeout(() => {
fakeDriver.sleep(100).then(() => {
expect(currentTest).toBe('A');
});
resolve();
}, 100);
});
});

it('test B', () => {
currentTest = 'B';
});
});
});

0 comments on commit d45aa6e

Please sign in to comment.