-
Notifications
You must be signed in to change notification settings - Fork 10
/
mocha.js
84 lines (68 loc) · 1.96 KB
/
mocha.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
// Copyright (C) 2013, GoodData(R) Corporation. All rights reserved.
var AIT = require('./ait.js');
/**
* mocha it() wrap() method
*
* Allows the user to write tests as follows:
*
* it('click button', wrap(function() { $('button').click(); }));
*/
var Fiber = require('fibers');
function aitWrap(fn) {
var f = fn;
function runFn(context, done) {
Fiber(function() {
// inject out browser instance into the wrap() method functor
Fiber.current.browser = AIT.browser;
f.apply(context, [done]);
}).run();
}
// wrap the sync code into an async method
f = function(done) {
fn.call(this);
done();
};
return function(done) { runFn(this, done); };
}
AIT.wrap = aitWrap;
AIT.before = function aitBefore(callback) {
AIT._ait_beforeCalled = true;
AIT.init(callback);
};
AIT.after = function aitAfter(callback) {
Fiber(function() {
AIT.destroy(callback);
}).run();
};
var originalBeforeEach = global.beforeEach;
var wrappedBeforeEach = function(fn) {
originalBeforeEach.call(this, AIT.wrap(fn));
};
/**
* Wrap the mocha it() calls automatically.
*/
var originalIt = global.it;
var wrappedIt = function(desc, fn) {
originalIt.call(this, desc, AIT.wrap(function () {
if (!AIT._ait_beforeCalled) {
throw new Error('AIT: Be sure to issue before(AIT.before); after(AIT.after); calls in your describe().');
}
try {
fn.apply(this, arguments);
} catch (ex) {
if (ex.cause && ex.cause.value && ex.cause.value.message) {
console.log(ex.cause.value.message);
}
AIT.browser.screenshot('ait-error-' + new Date().getTime() + '.png');
throw ex;
}
}));
};
var init = function(wrapMocha) {
if (wrapMocha !== false) {
global.beforeEach = wrappedBeforeEach;
global.it = wrappedIt;
}
return AIT;
};
module.exports = { init: init };