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

Commit 8abea3c

Browse files
hankduanjuliemr
authored andcommitted
fix(jasminewd): fix timeout for beforeEach and afterEach
1 parent eab9110 commit 8abea3c

File tree

1 file changed

+72
-23
lines changed

1 file changed

+72
-23
lines changed

jasminewd/index.js

+72-23
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,48 @@ function seal(fn) {
1919
};
2020
};
2121

22+
/**
23+
* Validates that the parameter is a function.
24+
* @param {Object} functionToValidate The function to validate.
25+
* @throws {Error}
26+
* @return {Object} The original parameter.
27+
*/
28+
function validateFunction(functionToValidate) {
29+
if (functionToValidate && Object.prototype.toString.call(functionToValidate) === '[object Function]') {
30+
return functionToValidate;
31+
} else {
32+
throw Error(functionToValidate + ' is not a function');
33+
}
34+
}
35+
36+
/**
37+
* Validates that the parameter is a number.
38+
* @param {Object} numberToValidate The number to validate.
39+
* @throws {Error}
40+
* @return {Object} The original number.
41+
*/
42+
function validateNumber(numberToValidate) {
43+
if (!isNaN(numberToValidate)) {
44+
return numberToValidate;
45+
} else {
46+
throw Error(numberToValidate + ' is not a number');
47+
}
48+
}
49+
50+
/**
51+
* Validates that the parameter is a string.
52+
* @param {Object} stringToValidate The string to validate.
53+
* @throws {Error}
54+
* @return {Object} The original string.
55+
*/
56+
function validateString(stringtoValidate) {
57+
if (typeof stringtoValidate == 'string' || stringtoValidate instanceof String) {
58+
return stringtoValidate;
59+
} else {
60+
throw Error(stringtoValidate + ' is not a string');
61+
}
62+
}
63+
2264
/**
2365
* Wraps a function so it runs inside a webdriver.promise.ControlFlow and
2466
* waits for the flow to complete before continuing.
@@ -27,48 +69,55 @@ function seal(fn) {
2769
*/
2870
function wrapInControlFlow(globalFn, fnName) {
2971
return function() {
30-
3172
var driverError = new Error();
3273
driverError.stack = driverError.stack.replace(/ +at.+jasminewd.+\n/, '');
3374

34-
var description = 'Asynchronous test function: ' + fnName + '(';
35-
if (arguments.length >= 2) {
36-
description += '"' + arguments[0] + '"';
37-
}
38-
description += ')';
39-
40-
function asyncTestFn(fn) {
75+
function asyncTestFn(fn, desc) {
4176
return function(done) {
77+
var desc_ = 'Asynchronous test function: ' + fnName + '(';
78+
if (desc) {
79+
desc_ += '"' + desc + '"';
80+
}
81+
desc_ += ')';
82+
4283
flow.execute(function() {
4384
fn.call(jasmine.getEnv().currentSpec, function() {
4485
throw new Error('Do not use a done callback with WebDriverJS tests. ' +
4586
'The tests are patched to be asynchronous and will terminate when ' +
4687
'the webdriver control flow is empty.');
4788
});
48-
}, description).then(seal(done), function(e) {
89+
}, desc_).then(seal(done), function(e) {
4990
e.stack = e.stack + '==== async task ====\n' + driverError.stack;
5091
done(e);
5192
});
5293
};
5394
};
5495

55-
switch (arguments.length) {
56-
case 1:
57-
globalFn(asyncTestFn(arguments[0]));
58-
break;
59-
60-
case 2:
61-
// The first variable is a description.
62-
globalFn(arguments[0], asyncTestFn(arguments[1]));
96+
var description, func, timeout;
97+
switch (fnName) {
98+
case 'it':
99+
case 'iit':
100+
description = validateString(arguments[0]);
101+
func = validateFunction(arguments[1]);
102+
if (!arguments[2]) {
103+
globalFn(description, asyncTestFn(func));
104+
} else {
105+
timeout = validateNumber(arguments[2]);
106+
globalFn(description, asyncTestFn(func), timeout);
107+
}
63108
break;
64-
65-
case 3:
66-
// The third variable should be the timeout in ms.
67-
globalFn(arguments[0], asyncTestFn(arguments[1]), arguments[2]);
109+
case 'beforeEach':
110+
case 'afterEach':
111+
func = validateFunction(arguments[0]);
112+
if (!arguments[1]) {
113+
globalFn(asyncTestFn(func));
114+
} else {
115+
timeout = validateNumber(arguments[1]);
116+
globalFn(asyncTestFn(func), timeout);
117+
}
68118
break;
69-
70119
default:
71-
throw Error('Invalid # arguments: ' + arguments.length);
120+
throw Error('invalid function: ' + fnName);
72121
}
73122
};
74123
};

0 commit comments

Comments
 (0)