-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathit.optional.js
92 lines (80 loc) · 1.98 KB
/
it.optional.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Dependencies
*/
var tryasync = require('./tryasync');
/**
* Expose `TestOptional`.
*/
exports = module.exports = TestOptional;
/**
* Initialize a new `Test` with the given `title` and callback `fn`, if test
* would fail marks it as pending and uses `titlePending`
*
* @param {String} title
* @param {String} titlePending
* @param {Function} fn
*/
function TestOptional(title, titlePending, fn) {
TestOptional.count = 0;
if (!fn) {
fn = titlePending;
titlePending = 'PENDING: ' + title;
}
it(title, function(done){
var self = this;
var timer;
var skipped = false;
function skipTest(cb){
if(skipped){
return;
}
skipped = true;
clearTimeout(timer);
self.test.title = titlePending;
TestOptional.count++;
cb();
}
// Increases the test default timeout and add a timeout of our own
var testTimeout = self.__proto__.timeout;
var resetTimer = function resetTimer(ms){
ms = ms || self.runnable()._timeout || 2000;
if (timer) { clearTimeout(timer); };
timer = setTimeout(function(){
titlePending = titlePending + ' (timeout)';
skipTest(done);
}, ms);
testTimeout.call(self, ms + 50);
};
self.timeout = function newTimeout(ms){
resetTimer(ms);
};
resetTimer();
function processResult(result) {
if (result) {
self.skip();
} else {
clearTimeout(timer);
done();
}
};
function executeTest(){
if (fn.length === 0) {
processResult(fn());
} else {
fn.call(self, processResult);
}
}
function handleErrorAsync(e){
// async skip not supported yet: https://github.com/mochajs/mocha/issues/1625
// self.skip();
skipTest(done);
}
try {
tryasync(executeTest)
.catch(handleErrorAsync);
} catch(e){
// sync'ed / same tick exception
skipTest(self.skip.bind(this));
}
});
};