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( 'debug' );
32 * Load TestSwarm agent
34 // Only if the current url indicates that there is a TestSwarm instance watching us
35 // (TestSwarm appends swarmURL to the test suites url it loads in iframes).
36 // Otherwise this is just a simple view of Special:JavaScriptTest/qunit directly,
37 // no point in loading inject.js in that case. Also, make sure that this instance
38 // of MediaWiki has actually been configured with the required url to that inject.js
39 // script. By default it is false.
40 if ( QUnit
.urlParams
.swarmURL
&& mw
.config
.get( 'QUnitTestSwarmInjectJSPath' ) ) {
41 document
.write( "<scr" + "ipt src='" + QUnit
.fixurl( mw
.config
.get( 'QUnitTestSwarmInjectJSPath' ) ) + "'></scr" + "ipt>" );
47 // Adds toggle checkbox to header
48 QUnit
.config
.urlConfig
.push( 'completenesstest' );
50 // Initiate when enabled
51 if ( QUnit
.urlParams
.completenesstest
) {
53 // Return true to ignore
54 mwTestIgnore = function ( val
, tester
, funcPath
) {
56 // Don't record methods of the properties of constructors,
57 // to avoid getting into a loop (prototype.constructor.prototype..).
58 // Since we're therefor skipping any injection for
59 // "new mw.Foo()", manually set it to true here.
60 if ( val
instanceof mw
.Map
) {
61 tester
.methodCallTracker
.Map
= true;
64 if ( val
instanceof mw
.Title
) {
65 tester
.methodCallTracker
.Title
= true;
69 // Don't record methods of the properties of a jQuery object
70 if ( val
instanceof $ ) {
77 mwTester
= new CompletenessTest( mw
, mwTestIgnore
);
81 * Test environment recommended for all QUnit test modules
83 // Whether to log environment changes to the console
84 QUnit
.config
.urlConfig
.push( 'mwlogenv' );
87 * Reset mw.config and others to a fresh copy of the live config for each test(),
88 * and restore it back to the live one afterwards.
89 * @param localEnv {Object} [optional]
90 * @example (see test suite at the bottom of this file)
93 QUnit
.newMwEnvironment
= ( function () {
94 var log
, liveConfig
, liveMessages
;
96 liveConfig
= mw
.config
.values
;
97 liveMessages
= mw
.messages
.values
;
99 function freshConfigCopy( custom
) {
100 // "deep=true" is important here.
101 // Otherwise we just create a new object with values referring to live config.
102 // e.g. mw.config.set( 'wgFileExtensions', [] ) would not effect liveConfig,
103 // but mw.config.get( 'wgFileExtensions' ).push( 'png' ) would as the array
104 // was passed by reference in $.extend's loop.
105 return $.extend( {}, liveConfig
, custom
, /*deep=*/true );
108 function freshMessagesCopy( custom
) {
109 return $.extend( {}, liveMessages
, custom
, /*deep=*/true );
112 log
= QUnit
.urlParams
.mwlogenv
? mw
.log : function () {};
114 return function ( localEnv
) {
115 localEnv
= $.extend( {
126 log( 'MwEnvironment> SETUP for "' + QUnit
.config
.current
.module
127 + ': ' + QUnit
.config
.current
.testName
+ '"' );
129 // Greetings, mock environment!
130 mw
.config
.values
= freshConfigCopy( localEnv
.config
);
131 mw
.messages
.values
= freshMessagesCopy( localEnv
.messages
);
136 teardown: function () {
137 log( 'MwEnvironment> TEARDOWN for "' + QUnit
.config
.current
.module
138 + ': ' + QUnit
.config
.current
.testName
+ '"' );
142 // Farewell, mock environment!
143 mw
.config
.values
= liveConfig
;
144 mw
.messages
.values
= liveMessages
;
150 // $.when stops as soon as one fails, which makes sense in most
151 // practical scenarios, but not in a unit test where we really do
152 // need to wait until all of them are finished.
153 QUnit
.whenPromisesComplete = function () {
154 var altPromises
= [];
156 $.each( arguments
, function ( i
, arg
) {
157 var alt
= $.Deferred();
158 altPromises
.push( alt
);
160 // Whether this one fails or not, forwards it to
161 // the 'done' (resolve) callback of the alternative promise.
162 arg
.always( alt
.resolve
);
165 return $.when
.apply( $, altPromises
);
169 * Add-on assertion helpers
171 // Define the add-ons
174 // Expect boolean true
175 assertTrue: function ( actual
, message
) {
176 strictEqual( actual
, true, message
);
179 // Expect boolean false
180 assertFalse: function ( actual
, message
) {
181 strictEqual( actual
, false, message
);
184 // Expect numerical value less than X
185 lt: function ( actual
, expected
, message
) {
186 QUnit
.push( actual
< expected
, actual
, 'less than ' + expected
, message
);
189 // Expect numerical value less than or equal to X
190 ltOrEq: function ( actual
, expected
, message
) {
191 QUnit
.push( actual
<= expected
, actual
, 'less than or equal to ' + expected
, message
);
194 // Expect numerical value greater than X
195 gt: function ( actual
, expected
, message
) {
196 QUnit
.push( actual
> expected
, actual
, 'greater than ' + expected
, message
);
199 // Expect numerical value greater than or equal to X
200 gtOrEq: function ( actual
, expected
, message
) {
201 QUnit
.push( actual
>= expected
, actual
, 'greater than or equal to ' + expected
, message
);
204 // Backwards compatible with new verions of QUnit
205 equals
: window
.equal
,
206 same
: window
.deepEqual
209 $.extend( QUnit
, addons
);
210 $.extend( window
, addons
);
213 * Small test suite to confirm proper functionality of the utilities and
214 * initializations in this file.
216 var envExecCount
= 0;
217 module( 'mediawiki.tests.qunit.testrunner', QUnit
.newMwEnvironment({
220 this.mwHtmlLive
= mw
.html
;
222 escape: function () {
223 return 'mocked-' + envExecCount
;
227 teardown: function () {
228 mw
.html
= this.mwHtmlLive
;
238 test( 'Setup', function () {
241 equal( mw
.html
.escape( 'foo' ), 'mocked-1', 'extra setup() callback was ran.' );
242 equal( mw
.config
.get( 'testVar' ), 'foo', 'config object applied' );
243 equal( mw
.messages
.get( 'testMsg' ), 'Foo.', 'messages object applied' );
245 mw
.config
.set( 'testVar', 'bar' );
246 mw
.messages
.set( 'testMsg', 'Bar.' );
249 test( 'Teardown', function () {
252 equal( mw
.html
.escape( 'foo' ), 'mocked-2', 'extra setup() callback was re-ran.' );
253 equal( mw
.config
.get( 'testVar' ), 'foo', 'config object restored and re-applied after test()' );
254 equal( mw
.messages
.get( 'testMsg' ), 'Foo.', 'messages object restored and re-applied after test()' );
257 module( 'mediawiki.tests.qunit.testrunner-after', QUnit
.newMwEnvironment() );
259 test( 'Teardown', function () {
262 equal( mw
.html
.escape( '<' ), '<', 'extra teardown() callback was ran.' );
263 equal( mw
.config
.get( 'testVar' ), null, 'config object restored to live in next module()' );
264 equal( mw
.messages
.get( 'testMsg' ), null, 'messages object restored to live in next module()' );
267 })( jQuery
, mediaWiki
, QUnit
);