-
-
Notifications
You must be signed in to change notification settings - Fork 153
/
index.js
315 lines (267 loc) · 8.56 KB
/
index.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
export { default as moduleFor } from './legacy-2-x/module-for';
export {
default as moduleForComponent,
} from './legacy-2-x/module-for-component';
export { default as moduleForModel } from './legacy-2-x/module-for-model';
export { default as QUnitAdapter } from './adapter';
export { module, test, skip, only, todo } from 'qunit';
export { loadTests } from './test-loader';
import { deprecate } from '@ember/debug';
import { loadTests } from './test-loader';
import Ember from 'ember';
import QUnit from 'qunit';
import QUnitAdapter from './adapter';
import {
setResolver as upstreamSetResolver,
render as upstreamRender,
clearRender as upstreamClearRender,
settled as upstreamSettled,
pauseTest as upstreamPauseTest,
resumeTest as upstreamResumeTest,
setupContext,
teardownContext,
setupRenderingContext,
teardownRenderingContext,
setupApplicationContext,
teardownApplicationContext,
validateErrorHandler,
} from '@ember/test-helpers';
import {
detectIfTestNotIsolated,
reportIfTestNotIsolated,
} from './test-isolation-validation';
export function setResolver() {
deprecate(
'`setResolver` should be imported from `@ember/test-helpers`, but was imported from `ember-qunit`',
false,
{
id: 'ember-qunit.deprecated-reexports.setResolver',
until: '4.0.0',
}
);
return upstreamSetResolver(...arguments);
}
export function render() {
deprecate(
'`render` should be imported from `@ember/test-helpers`, but was imported from `ember-qunit`',
false,
{
id: 'ember-qunit.deprecated-reexports.render',
until: '4.0.0',
}
);
return upstreamRender(...arguments);
}
export function clearRender() {
deprecate(
'`clearRender` should be imported from `@ember/test-helpers`, but was imported from `ember-qunit`',
false,
{
id: 'ember-qunit.deprecated-reexports.clearRender',
until: '4.0.0',
}
);
return upstreamClearRender(...arguments);
}
export function settled() {
deprecate(
'`settled` should be imported from `@ember/test-helpers`, but was imported from `ember-qunit`',
false,
{
id: 'ember-qunit.deprecated-reexports.settled',
until: '4.0.0',
}
);
return upstreamSettled(...arguments);
}
export function pauseTest() {
deprecate(
'`pauseTest` should be imported from `@ember/test-helpers`, but was imported from `ember-qunit`',
false,
{
id: 'ember-qunit.deprecated-reexports.pauseTest',
until: '4.0.0',
}
);
return upstreamPauseTest(...arguments);
}
export function resumeTest() {
deprecate(
'`resumeTest` should be imported from `@ember/test-helpers`, but was imported from `ember-qunit`',
false,
{
id: 'ember-qunit.deprecated-reexports.resumeTest',
until: '4.0.0',
}
);
return upstreamResumeTest(...arguments);
}
export function setupTest(hooks, options) {
hooks.beforeEach(function(assert) {
return setupContext(this, options).then(() => {
let originalPauseTest = this.pauseTest;
this.pauseTest = function QUnit_pauseTest() {
assert.timeout(-1); // prevent the test from timing out
return originalPauseTest.call(this);
};
});
});
hooks.afterEach(function() {
return teardownContext(this);
});
}
export function setupRenderingTest(hooks, options) {
setupTest(hooks, options);
hooks.beforeEach(function() {
return setupRenderingContext(this);
});
hooks.afterEach(function() {
return teardownRenderingContext(this);
});
}
export function setupApplicationTest(hooks, options) {
setupTest(hooks, options);
hooks.beforeEach(function() {
return setupApplicationContext(this);
});
hooks.afterEach(function() {
return teardownApplicationContext(this);
});
}
/**
Uses current URL configuration to setup the test container.
* If `?nocontainer` is set, the test container will be hidden.
* If `?dockcontainer` or `?devmode` are set the test container will be
absolutely positioned.
* If `?devmode` is set, the test container will be made full screen.
@method setupTestContainer
*/
export function setupTestContainer() {
let testContainer = document.getElementById('ember-testing-container');
if (!testContainer) {
return;
}
let params = QUnit.urlParams;
let containerVisibility = params.nocontainer ? 'hidden' : 'visible';
let containerPosition =
params.dockcontainer || params.devmode ? 'fixed' : 'relative';
if (params.devmode) {
testContainer.className = ' full-screen';
}
testContainer.style.visibility = containerVisibility;
testContainer.style.position = containerPosition;
let qunitContainer = document.getElementById('qunit');
if (params.dockcontainer) {
qunitContainer.style.marginBottom = window.getComputedStyle(
testContainer
).height;
}
}
/**
Instruct QUnit to start the tests.
@method startTests
*/
export function startTests() {
QUnit.start();
}
/**
Sets up the `Ember.Test` adapter for usage with QUnit 2.x.
@method setupTestAdapter
*/
export function setupTestAdapter() {
Ember.Test.adapter = QUnitAdapter.create();
}
/**
Ensures that `Ember.testing` is set to `true` before each test begins
(including `before` / `beforeEach`), and reset to `false` after each test is
completed. This is done via `QUnit.testStart` and `QUnit.testDone`.
*/
export function setupEmberTesting() {
QUnit.testStart(() => {
Ember.testing = true;
});
QUnit.testDone(() => {
Ember.testing = false;
});
}
/**
Ensures that `Ember.onerror` (if present) is properly configured to re-throw
errors that occur while `Ember.testing` is `true`.
*/
export function setupEmberOnerrorValidation() {
QUnit.module('ember-qunit: Ember.onerror validation', function() {
QUnit.test('Ember.onerror is functioning properly', function(assert) {
assert.expect(1);
let result = validateErrorHandler();
assert.ok(
result.isValid,
`Ember.onerror handler with invalid testing behavior detected. An Ember.onerror handler _must_ rethrow exceptions when \`Ember.testing\` is \`true\` or the test suite is unreliable. See https://git.io/vbine for more details.`
);
});
});
}
export function setupTestIsolationValidation() {
QUnit.testDone(detectIfTestNotIsolated);
QUnit.done(reportIfTestNotIsolated);
}
// This polyfills the changes from https://github.com/qunitjs/qunit/pull/1279
// and should be removed when that change is released and included in a release
// version of QUnit
function polyfillMemoryLeakPrevention() {
QUnit.testDone(function() {
// release the test callback
QUnit.config.current.callback = undefined;
});
QUnit.moduleDone(function() {
// release the module hooks
QUnit.config.current.module.hooks = {};
});
}
/**
@method start
@param {Object} [options] Options to be used for enabling/disabling behaviors
@param {Boolean} [options.loadTests] If `false` tests will not be loaded automatically.
@param {Boolean} [options.setupTestContainer] If `false` the test container will not
be setup based on `devmode`, `dockcontainer`, or `nocontainer` URL params.
@param {Boolean} [options.startTests] If `false` tests will not be automatically started
(you must run `QUnit.start()` to kick them off).
@param {Boolean} [options.setupTestAdapter] If `false` the default Ember.Test adapter will
not be updated.
@param {Boolean} [options.setupEmberTesting] `false` opts out of the
default behavior of setting `Ember.testing` to `true` before all tests and
back to `false` after each test will.
@param {Boolean} [options.setupEmberOnerrorValidation] If `false` validation
of `Ember.onerror` will be disabled.
@param {Boolean} [options.setupTestIsolationValidation] If `false` test isolation validation
will be disabled.
@param {Boolean} [options._polyfillMemoryLeakPrevention] If `false` the polyfilled memory leak prevention will not be enabled.
*/
export function start(options = {}) {
if (options.loadTests !== false) {
loadTests();
}
if (options.setupTestContainer !== false) {
setupTestContainer();
}
if (options.setupTestAdapter !== false) {
setupTestAdapter();
}
if (options.setupEmberTesting !== false) {
setupEmberTesting();
}
if (options.setupEmberOnerrorValidation !== false) {
setupEmberOnerrorValidation();
}
if (
typeof options.setupTestIsolationValidation !== 'undefined' &&
options.setupTestIsolationValidation !== false
) {
setupTestIsolationValidation();
}
if (options._polyfillMemoryLeakPrevention !== false) {
polyfillMemoryLeakPrevention();
}
if (options.startTests !== false) {
startTests();
}
}