-
Notifications
You must be signed in to change notification settings - Fork 24
/
adapter.js
189 lines (157 loc) · 4.28 KB
/
adapter.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* Ember Mocha Adapter | (C) 2014 Teddy Zeenny | https://github.com/teddyzeenny/ember-mocha-adapter */
(function() {
var done, doneTimeout, isAsync, emberBdd, isPromise;
done = null;
doneTimeout = null;
isAsync = 0;
Ember.Test.MochaAdapter = Ember.Test.Adapter.extend({
init: function() {
this._super();
window.Mocha.interfaces['ember-bdd'] = emberBdd;
window.mocha.ui('ember-bdd');
},
asyncStart: function() {
isAsync++;
clearTimeout(doneTimeout);
},
asyncEnd: function() {
if (--isAsync === 0 && done && !isPromise) {
doneTimeout = setTimeout(function() {
complete();
});
}
},
exception: function(reason) {
if (!(reason instanceof Error)) {
reason = new Error(reason);
}
if (done) {
complete(reason);
} else {
setTimeout(function() {
throw reason;
});
}
}
});
function fixAsync(suites, methodName) {
return function(name, fn) {
if (!fn) {
fn = name;
name = fn.name;
}
var suite = suites[0];
var hook = suite[methodName];
var asyncFn = fn;
if (fn.length === 0) {
asyncFn = function(d) {
invoke(this, fn, d);
};
}
if (hook.length === 2) {
hook.call(suite, name, asyncFn);
} else {
hook.call(suite, asyncFn);
}
};
}
function invoke(context, fn, d) {
done = d;
isPromise = false;
var result = fn.call(context);
// If a promise is returned,
// complete test when promise fulfills / rejects
if (result && typeof result.then === 'function') {
isPromise = true;
result.then(function() { complete(); }, complete);
} else {
if (isAsync === 0) { complete(); }
}
}
// Called whenever an async test passes or fails.
// if `e` is passed, that means the test
// failed with exception `e`
function complete(e) {
clearTimeout(doneTimeout);
if (!done) { return; }
var d = done;
done = null;
if (e) {
// test failure
if (!(e instanceof Error)) {
e = new Error(e);
}
d(e);
} else {
// test passed
d();
}
}
/**
ember-bdd mocha interface.
This interface allows
the Ember.js tester
to forget about sync / async
and treat all tests the same.
This interface, along with the adapter
will take care of handling sync vs async
*/
emberBdd = function(suite) {
var suites = [suite];
suite.on('pre-require', function(context, file, mocha) {
context.before = fixAsync(suites, 'beforeAll');
context.after = fixAsync(suites, 'afterAll');
context.beforeEach = fixAsync(suites, 'beforeEach');
context.afterEach = fixAsync(suites, 'afterEach');
context.it = context.specify = function(title, fn){
var suite = suites[0], test;
if (suite.pending) {
fn = null;
}
if (!fn || fn.length === 1) {
test = new Mocha.Test(title, fn);
} else {
var method = function(d) {
invoke(this, fn, d);
};
method.toString = function() {
return fn.toString();
}
test = new Mocha.Test(title, method);
}
suite.addTest(test);
return test;
};
context.describe = context.context = function(title, fn){
var suite = Mocha.Suite.create(suites[0], title);
suites.unshift(suite);
fn.call(suite);
suites.shift();
return suite;
};
context.xdescribe =
context.xcontext =
context.describe.skip = function(title, fn){
var suite = Mocha.Suite.create(suites[0], title);
suite.pending = true;
suites.unshift(suite);
fn.call(suite);
suites.shift();
};
context.describe.only = function(title, fn){
var suite = context.describe(title, fn);
mocha.grep(suite.fullTitle());
};
context.it.only = function(title, fn){
var test = context.it(title, fn);
mocha.grep(test.fullTitle());
};
context.xit =
context.xspecify =
context.it.skip = function(title){
context.it(title);
};
});
};
}());
Ember.Test.adapter = Ember.Test.MochaAdapter.create();