-
Notifications
You must be signed in to change notification settings - Fork 784
/
core.js
200 lines (156 loc) 路 4.85 KB
/
core.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
import { window, document, setTimeout } from "./globals";
import equiv from "./equiv";
import dump from "./dump";
import module from "./module";
import Assert from "./assert";
import Logger from "./logger";
import Test, { test, pushFailure } from "./test";
import exportQUnit from "./export";
import reporters from "./reporters";
import config from "./core/config";
import { extend, objectType, is, now } from "./core/utilities";
import { registerLoggingCallbacks, runLoggingCallbacks } from "./core/logging";
import { sourceFromStacktrace } from "./core/stacktrace";
import ProcessingQueue from "./core/processing-queue";
import SuiteReport from "./reports/suite";
import { on, emit } from "./events";
import onWindowError from "./core/onerror";
import onUncaughtException from "./core/on-uncaught-exception";
const QUnit = {};
export const globalSuite = new SuiteReport();
// The initial "currentModule" represents the global (or top-level) module that
// is not explicitly defined by the user, therefore we add the "globalSuite" to
// it since each module has a suiteReport associated with it.
config.currentModule.suiteReport = globalSuite;
let globalStartCalled = false;
let runStarted = false;
// Figure out if we're running the tests from a server or not
QUnit.isLocal = ( window && window.location && window.location.protocol === "file:" );
// Expose the current QUnit version
QUnit.version = "@VERSION";
extend( QUnit, {
config,
dump,
equiv,
reporters,
is,
objectType,
on,
onError: onWindowError,
onUncaughtException,
pushFailure,
assert: Assert.prototype,
module,
test,
// alias other test flavors for easy access
todo: test.todo,
skip: test.skip,
only: test.only,
start: function( count ) {
if ( config.current ) {
throw new Error( "QUnit.start cannot be called inside a test context." );
}
const globalStartAlreadyCalled = globalStartCalled;
globalStartCalled = true;
if ( runStarted ) {
throw new Error( "Called start() while test already started running" );
}
if ( globalStartAlreadyCalled || count > 1 ) {
throw new Error( "Called start() outside of a test context too many times" );
}
if ( config.autostart ) {
throw new Error( "Called start() outside of a test context when " +
"QUnit.config.autostart was true" );
}
if ( !config.pageLoaded ) {
// The page isn't completely loaded yet, so we set autostart and then
// load if we're in Node or wait for the browser's load event.
config.autostart = true;
// Starts from Node even if .load was not previously called. We still return
// early otherwise we'll wind up "beginning" twice.
if ( !document ) {
QUnit.load();
}
return;
}
scheduleBegin();
},
onUnhandledRejection: function( reason ) {
Logger.warn( "QUnit.onUnhandledRejection is deprecated and will be removed in QUnit 3.0." +
" Please use QUnit.onUncaughtException instead." );
onUncaughtException( reason );
},
extend: function( ...args ) {
Logger.warn( "QUnit.extend is deprecated and will be removed in QUnit 3.0." +
" Please use Object.assign instead." );
// delegate to utility implementation, which does not warn and can be used elsewhere internally
return extend.apply( this, args );
},
load: function() {
config.pageLoaded = true;
// Initialize the configuration options
extend( config, {
stats: { all: 0, bad: 0, testCount: 0 },
started: 0,
updateRate: 1000,
autostart: true,
filter: ""
}, true );
if ( !runStarted ) {
config.blocking = false;
if ( config.autostart ) {
scheduleBegin();
}
}
},
stack: function( offset ) {
offset = ( offset || 0 ) + 2;
return sourceFromStacktrace( offset );
}
} );
registerLoggingCallbacks( QUnit );
function scheduleBegin() {
runStarted = true;
// Add a slight delay to allow definition of more modules and tests.
if ( setTimeout ) {
setTimeout( function() {
begin();
} );
} else {
begin();
}
}
function unblockAndAdvanceQueue() {
config.blocking = false;
ProcessingQueue.advance();
}
export function begin() {
if ( config.started ) {
unblockAndAdvanceQueue();
return;
}
// The test run hasn't officially begun yet
// Record the time of the test run's beginning
config.started = now();
// Delete the loose unnamed module if unused.
if ( config.modules[ 0 ].name === "" && config.modules[ 0 ].tests.length === 0 ) {
config.modules.shift();
}
// Avoid unnecessary information by not logging modules' test environments
const l = config.modules.length;
const modulesLog = [];
for ( let i = 0; i < l; i++ ) {
modulesLog.push( {
name: config.modules[ i ].name,
tests: config.modules[ i ].tests
} );
}
// The test run is officially beginning now
emit( "runStart", globalSuite.start( true ) );
runLoggingCallbacks( "begin", {
totalTests: Test.count,
modules: modulesLog
} ).then( unblockAndAdvanceQueue );
}
exportQUnit( QUnit );
export default QUnit;