1 ( function ( $, mw
, QUnit
, undefined ) {
4 var mwTestIgnore
, mwTester
, addons
;
7 * Add bogus to url to prevent IE crazy caching
9 * @param value {String} a relative path (eg. 'data/foo.js'
10 * or 'data/test.php?foo=bar').
11 * @return {String} Such as 'data/foo.js?131031765087663960'
13 QUnit
.fixurl = function ( value
) {
14 return value
+ (/\?/.test( value
) ? '&' : '?')
15 + String( new Date().getTime() )
16 + String( parseInt( Math
.random() * 100000, 10 ) );
23 // When a test() indicates asynchronicity with stop(),
24 // allow 10 seconds to pass before killing the test(),
25 // and assuming failure.
26 QUnit
.config
.testTimeout
= 10 * 1000;
28 // Add a checkbox to QUnit header to toggle MediaWiki ResourceLoader debug mode.
29 QUnit
.config
.urlConfig
.push( {
31 label
: 'Enable ResourceLoaderDebug',
32 tooltip
: 'Enable debug mode in ResourceLoader'
36 * Load TestSwarm agent
38 // Only if the current url indicates that there is a TestSwarm instance watching us
39 // (TestSwarm appends swarmURL to the test suites url it loads in iframes).
40 // Otherwise this is just a simple view of Special:JavaScriptTest/qunit directly,
41 // no point in loading inject.js in that case. Also, make sure that this instance
42 // of MediaWiki has actually been configured with the required url to that inject.js
43 // script. By default it is false.
44 if ( QUnit
.urlParams
.swarmURL
&& mw
.config
.get( 'QUnitTestSwarmInjectJSPath' ) ) {
45 document
.write( "<scr" + "ipt src='" + QUnit
.fixurl( mw
.config
.get( 'QUnitTestSwarmInjectJSPath' ) ) + "'></scr" + "ipt>" );
51 // Adds toggle checkbox to header
52 QUnit
.config
.urlConfig
.push( {
53 id
: 'completenesstest',
54 label
: 'Run CompletenessTest',
55 tooltip
: 'Run the completeness test'
58 // Initiate when enabled
59 if ( QUnit
.urlParams
.completenesstest
) {
61 // Return true to ignore
62 mwTestIgnore = function ( val
, tester
, funcPath
) {
64 // Don't record methods of the properties of constructors,
65 // to avoid getting into a loop (prototype.constructor.prototype..).
66 // Since we're therefor skipping any injection for
67 // "new mw.Foo()", manually set it to true here.
68 if ( val
instanceof mw
.Map
) {
69 tester
.methodCallTracker
.Map
= true;
72 if ( val
instanceof mw
.Title
) {
73 tester
.methodCallTracker
.Title
= true;
77 // Don't record methods of the properties of a jQuery object
78 if ( val
instanceof $ ) {
85 mwTester
= new CompletenessTest( mw
, mwTestIgnore
);
89 * Test environment recommended for all QUnit test modules
91 // Whether to log environment changes to the console
92 QUnit
.config
.urlConfig
.push( 'mwlogenv' );
95 * Reset mw.config and others to a fresh copy of the live config for each test(),
96 * and restore it back to the live one afterwards.
97 * @param localEnv {Object} [optional]
98 * @example (see test suite at the bottom of this file)
101 QUnit
.newMwEnvironment
= ( function () {
102 var log
, liveConfig
, liveMessages
;
104 liveConfig
= mw
.config
.values
;
105 liveMessages
= mw
.messages
.values
;
107 function freshConfigCopy( custom
) {
108 // "deep=true" is important here.
109 // Otherwise we just create a new object with values referring to live config.
110 // e.g. mw.config.set( 'wgFileExtensions', [] ) would not effect liveConfig,
111 // but mw.config.get( 'wgFileExtensions' ).push( 'png' ) would as the array
112 // was passed by reference in $.extend's loop.
113 return $.extend( {}, liveConfig
, custom
, /*deep=*/true );
116 function freshMessagesCopy( custom
) {
117 return $.extend( {}, liveMessages
, custom
, /*deep=*/true );
120 log
= QUnit
.urlParams
.mwlogenv
? mw
.log : function () {};
122 return function ( localEnv
) {
123 localEnv
= $.extend( {
134 log( 'MwEnvironment> SETUP for "' + QUnit
.config
.current
.module
135 + ': ' + QUnit
.config
.current
.testName
+ '"' );
137 // Greetings, mock environment!
138 mw
.config
.values
= freshConfigCopy( localEnv
.config
);
139 mw
.messages
.values
= freshMessagesCopy( localEnv
.messages
);
144 teardown: function () {
145 log( 'MwEnvironment> TEARDOWN for "' + QUnit
.config
.current
.module
146 + ': ' + QUnit
.config
.current
.testName
+ '"' );
150 // Farewell, mock environment!
151 mw
.config
.values
= liveConfig
;
152 mw
.messages
.values
= liveMessages
;
158 // $.when stops as soon as one fails, which makes sense in most
159 // practical scenarios, but not in a unit test where we really do
160 // need to wait until all of them are finished.
161 QUnit
.whenPromisesComplete = function () {
162 var altPromises
= [];
164 $.each( arguments
, function ( i
, arg
) {
165 var alt
= $.Deferred();
166 altPromises
.push( alt
);
168 // Whether this one fails or not, forwards it to
169 // the 'done' (resolve) callback of the alternative promise.
170 arg
.always( alt
.resolve
);
173 return $.when
.apply( $, altPromises
);
177 * Add-on assertion helpers
179 // Define the add-ons
182 // Expect boolean true
183 assertTrue: function ( actual
, message
) {
184 strictEqual( actual
, true, message
);
187 // Expect boolean false
188 assertFalse: function ( actual
, message
) {
189 strictEqual( actual
, false, message
);
192 // Expect numerical value less than X
193 lt: function ( actual
, expected
, message
) {
194 QUnit
.push( actual
< expected
, actual
, 'less than ' + expected
, message
);
197 // Expect numerical value less than or equal to X
198 ltOrEq: function ( actual
, expected
, message
) {
199 QUnit
.push( actual
<= expected
, actual
, 'less than or equal to ' + expected
, message
);
202 // Expect numerical value greater than X
203 gt: function ( actual
, expected
, message
) {
204 QUnit
.push( actual
> expected
, actual
, 'greater than ' + expected
, message
);
207 // Expect numerical value greater than or equal to X
208 gtOrEq: function ( actual
, expected
, message
) {
209 QUnit
.push( actual
>= expected
, actual
, 'greater than or equal to ' + expected
, message
);
212 // Backwards compatible with new verions of QUnit
213 equals
: window
.equal
,
214 same
: window
.deepEqual
217 $.extend( QUnit
, addons
);
218 $.extend( window
, addons
);
221 * Small test suite to confirm proper functionality of the utilities and
222 * initializations in this file.
224 var envExecCount
= 0;
225 module( 'mediawiki.tests.qunit.testrunner', QUnit
.newMwEnvironment({
228 this.mwHtmlLive
= mw
.html
;
230 escape: function () {
231 return 'mocked-' + envExecCount
;
235 teardown: function () {
236 mw
.html
= this.mwHtmlLive
;
246 test( 'Setup', function () {
249 equal( mw
.html
.escape( 'foo' ), 'mocked-1', 'extra setup() callback was ran.' );
250 equal( mw
.config
.get( 'testVar' ), 'foo', 'config object applied' );
251 equal( mw
.messages
.get( 'testMsg' ), 'Foo.', 'messages object applied' );
253 mw
.config
.set( 'testVar', 'bar' );
254 mw
.messages
.set( 'testMsg', 'Bar.' );
257 test( 'Teardown', function () {
260 equal( mw
.html
.escape( 'foo' ), 'mocked-2', 'extra setup() callback was re-ran.' );
261 equal( mw
.config
.get( 'testVar' ), 'foo', 'config object restored and re-applied after test()' );
262 equal( mw
.messages
.get( 'testMsg' ), 'Foo.', 'messages object restored and re-applied after test()' );
265 module( 'mediawiki.tests.qunit.testrunner-after', QUnit
.newMwEnvironment() );
267 test( 'Teardown', function () {
270 equal( mw
.html
.escape( '<' ), '<', 'extra teardown() callback was ran.' );
271 equal( mw
.config
.get( 'testVar' ), null, 'config object restored to live in next module()' );
272 equal( mw
.messages
.get( 'testMsg' ), null, 'messages object restored to live in next module()' );
275 })( jQuery
, mediaWiki
, QUnit
);