/
qunit-once.js
81 lines (66 loc) · 1.87 KB
/
qunit-once.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
/* jshint -W117:false */
(function (QUnit, env) {
if (env.__quit_once_initialized) {
return;
}
env.__quit_once_initialized = true;
if (typeof QUnit !== 'object') {
throw new Error('undefined QUnit object');
}
var _module = QUnit.module;
if (typeof _module !== 'function') {
throw new Error('QUnit.module should be a function');
}
QUnit.module = function (name, config) {
if (typeof config !== 'object') {
return;
}
(function addSetupOnce() {
if (QUnit.supports &&
QUnit.supports.setupOnce) {
return;
}
if (typeof config.setupOnce === 'function') {
var _setupOnceRan = false;
var _setup = typeof config.setup === 'function' ?
config.setup : null;
config.setup = function () {
if (!_setupOnceRan) {
config.setupOnce();
_setupOnceRan = true;
}
if (_setup) {
_setup.call(config);
}
};
}
}());
(function addTeardownOnce() {
if (QUnit.supports &&
QUnit.supports.teardownOnce) {
return;
}
function isLastTestInModule() {
if (QUnit.config && Array.isArray(QUnit.config.queue)) {
return QUnit.config.queue.length === 1;
} else {
// we cannot determine if the test is the last one in this module
return false;
}
}
if (typeof config.teardownOnce === 'function') {
var _teardown = typeof config.teardown === 'function' ?
config.teardown : null;
config.teardown = function () {
if (_teardown) {
_teardown.call(config);
}
if (isLastTestInModule()) {
config.teardownOnce();
}
};
}
}());
_module.call(QUnit, name, config);
};
}(QUnit, typeof global === 'object' ? global : window));