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

Commit 43ff9e5

Browse files
hankduanjuliemr
authored andcommitted
fix(jasminewd): allow asynchronous callbacks for jasmine tests
Closes #728, Closes #704
1 parent 35dcea0 commit 43ff9e5

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

jasminewd/index.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,25 @@ function wrapInControlFlow(globalFn, fnName) {
8080
}
8181
desc_ += ')';
8282

83+
// deferred object for signaling completion of asychronous function within globalFn
84+
var asyncFnDone = webdriver.promise.defer();
85+
86+
if (fn.length == 0) {
87+
// function with globalFn not asychronous
88+
asyncFnDone.fulfill();
89+
} else if (fn.length > 1) {
90+
throw Error('Invalid # arguments (' + fn.length + ') within function "' + fnName +'"');
91+
}
92+
8393
flow.execute(function() {
84-
fn.call(jasmine.getEnv().currentSpec, function() {
85-
throw new Error('Do not use a done callback with WebDriverJS tests. ' +
86-
'The tests are patched to be asynchronous and will terminate when ' +
87-
'the webdriver control flow is empty.');
94+
fn.call(jasmine.getEnv().currentSpec, function(userError) {
95+
if (userError) {
96+
asyncFnDone.reject(new Error(userError));
97+
} else {
98+
asyncFnDone.fulfill();
99+
}
88100
});
101+
return asyncFnDone.promise;
89102
}, desc_).then(seal(done), function(e) {
90103
e.stack = e.stack + '==== async task ====\n' + driverError.stack;
91104
done(e);

jasminewd/spec/adapterSpec.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,34 @@ describe('webdriverJS Jasmine adapter', function() {
186186
// expect(fakeDriver.getValueB()).toEqual('b');
187187
// }, 300);
188188

189-
// it('should error with a warning if done callback is used', function(done) {
190-
// done();
189+
// it('should pass errors from done callback', function(done) {
190+
// done('an error');
191191
// });
192192

193193
it('should pass after the timed out tests', function() {
194194
expect(true).toEqual(true);
195195
});
196+
197+
describe('should work for both synchronous and asynchronous tests', function() {
198+
var x;
199+
200+
beforeEach(function() {
201+
x = 0;
202+
});
203+
204+
afterEach(function() {
205+
expect(x).toBe(1);
206+
});
207+
208+
it('should execute a synchronous test', function() {
209+
x = 1;
210+
});
211+
212+
it('should execute an asynchronous test', function(done) {
213+
setTimeout(function(){
214+
x = 1;
215+
done();
216+
}, 500);
217+
});
218+
});
196219
});

0 commit comments

Comments
 (0)