1 ( function ( $, mw
, QUnit
, undefined ) {
2 /*global CompletenessTest */
6 var mwTestIgnore
, mwTester
, addons
;
9 * Add bogus to url to prevent IE crazy caching
11 * @param value {String} a relative path (eg. 'data/foo.js'
12 * or 'data/test.php?foo=bar').
13 * @return {String} Such as 'data/foo.js?131031765087663960'
15 QUnit
.fixurl = function ( value
) {
16 return value
+ (/\?/.test( value
) ? '&' : '?')
17 + String( new Date().getTime() )
18 + String( parseInt( Math
.random() * 100000, 10 ) );
25 // When a test() indicates asynchronicity with stop(),
26 // allow 10 seconds to pass before killing the test(),
27 // and assuming failure.
28 QUnit
.config
.testTimeout
= 10 * 1000;
30 // Add a checkbox to QUnit header to toggle MediaWiki ResourceLoader debug mode.
31 QUnit
.config
.urlConfig
.push( {
33 label
: 'Enable ResourceLoaderDebug',
34 tooltip
: 'Enable debug mode in ResourceLoader'
38 * Load TestSwarm agent
40 // Only if the current url indicates that there is a TestSwarm instance watching us
41 // (TestSwarm appends swarmURL to the test suites url it loads in iframes).
42 // Otherwise this is just a simple view of Special:JavaScriptTest/qunit directly,
43 // no point in loading inject.js in that case. Also, make sure that this instance
44 // of MediaWiki has actually been configured with the required url to that inject.js
45 // script. By default it is false.
46 if ( QUnit
.urlParams
.swarmURL
&& mw
.config
.get( 'QUnitTestSwarmInjectJSPath' ) ) {
47 document
.write( "<scr" + "ipt src='" + QUnit
.fixurl( mw
.config
.get( 'QUnitTestSwarmInjectJSPath' ) ) + "'></scr" + "ipt>" );
53 // Adds toggle checkbox to header
54 QUnit
.config
.urlConfig
.push( {
55 id
: 'completenesstest',
56 label
: 'Run CompletenessTest',
57 tooltip
: 'Run the completeness test'
60 // Initiate when enabled
61 if ( QUnit
.urlParams
.completenesstest
) {
63 // Return true to ignore
64 mwTestIgnore = function ( val
, tester
, funcPath
) {
66 // Don't record methods of the properties of constructors,
67 // to avoid getting into a loop (prototype.constructor.prototype..).
68 // Since we're therefor skipping any injection for
69 // "new mw.Foo()", manually set it to true here.
70 if ( val
instanceof mw
.Map
) {
71 tester
.methodCallTracker
.Map
= true;
74 if ( val
instanceof mw
.Title
) {
75 tester
.methodCallTracker
.Title
= true;
79 // Don't record methods of the properties of a jQuery object
80 if ( val
instanceof $ ) {
87 mwTester
= new CompletenessTest( mw
, mwTestIgnore
);
91 * Test environment recommended for all QUnit test modules
93 // Whether to log environment changes to the console
94 QUnit
.config
.urlConfig
.push( 'mwlogenv' );
97 * Reset mw.config and others to a fresh copy of the live config for each test(),
98 * and restore it back to the live one afterwards.
99 * @param localEnv {Object} [optional]
100 * @example (see test suite at the bottom of this file)
103 QUnit
.newMwEnvironment
= ( function () {
104 var log
, liveConfig
, liveMessages
;
106 liveConfig
= mw
.config
.values
;
107 liveMessages
= mw
.messages
.values
;
109 function freshConfigCopy( custom
) {
110 // "deep=true" is important here.
111 // Otherwise we just create a new object with values referring to live config.
112 // e.g. mw.config.set( 'wgFileExtensions', [] ) would not effect liveConfig,
113 // but mw.config.get( 'wgFileExtensions' ).push( 'png' ) would as the array
114 // was passed by reference in $.extend's loop.
115 return $.extend( {}, liveConfig
, custom
, /*deep=*/true );
118 function freshMessagesCopy( custom
) {
119 return $.extend( {}, liveMessages
, custom
, /*deep=*/true );
122 log
= QUnit
.urlParams
.mwlogenv
? mw
.log : function () {};
124 return function ( localEnv
) {
125 localEnv
= $.extend( {
136 log( 'MwEnvironment> SETUP for "' + QUnit
.config
.current
.module
137 + ': ' + QUnit
.config
.current
.testName
+ '"' );
139 // Greetings, mock environment!
140 mw
.config
.values
= freshConfigCopy( localEnv
.config
);
141 mw
.messages
.values
= freshMessagesCopy( localEnv
.messages
);
146 teardown: function () {
147 log( 'MwEnvironment> TEARDOWN for "' + QUnit
.config
.current
.module
148 + ': ' + QUnit
.config
.current
.testName
+ '"' );
152 // Farewell, mock environment!
153 mw
.config
.values
= liveConfig
;
154 mw
.messages
.values
= liveMessages
;
160 // $.when stops as soon as one fails, which makes sense in most
161 // practical scenarios, but not in a unit test where we really do
162 // need to wait until all of them are finished.
163 QUnit
.whenPromisesComplete = function () {
164 var altPromises
= [];
166 $.each( arguments
, function ( i
, arg
) {
167 var alt
= $.Deferred();
168 altPromises
.push( alt
);
170 // Whether this one fails or not, forwards it to
171 // the 'done' (resolve) callback of the alternative promise.
172 arg
.always( alt
.resolve
);
175 return $.when
.apply( $, altPromises
);
179 * Add-on assertion helpers
181 // Define the add-ons
184 // Expect boolean true
185 assertTrue: function ( actual
, message
) {
186 QUnit
.push( actual
=== true, actual
, true, message
);
189 // Expect boolean false
190 assertFalse: function ( actual
, message
) {
191 QUnit
.push( actual
=== false, actual
, false, message
);
194 // Expect numerical value less than X
195 lt: function ( actual
, expected
, message
) {
196 QUnit
.push( actual
< expected
, actual
, 'less than ' + expected
, message
);
199 // Expect numerical value less than or equal to X
200 ltOrEq: function ( actual
, expected
, message
) {
201 QUnit
.push( actual
<= expected
, actual
, 'less than or equal to ' + expected
, message
);
204 // Expect numerical value greater than X
205 gt: function ( actual
, expected
, message
) {
206 QUnit
.push( actual
> expected
, actual
, 'greater than ' + expected
, message
);
209 // Expect numerical value greater than or equal to X
210 gtOrEq: function ( actual
, expected
, message
) {
211 QUnit
.push( actual
>= expected
, actual
, 'greater than or equal to ' + expected
, message
);
215 $.extend( QUnit
.assert
, addons
);
218 * Small test suite to confirm proper functionality of the utilities and
219 * initializations in this file.
221 var envExecCount
= 0;
222 QUnit
.module( 'mediawiki.tests.qunit.testrunner', QUnit
.newMwEnvironment({
225 this.mwHtmlLive
= mw
.html
;
227 escape: function () {
228 return 'mocked-' + envExecCount
;
232 teardown: function () {
233 mw
.html
= this.mwHtmlLive
;
243 QUnit
.test( 'Setup', 3, function ( assert
) {
244 assert
.equal( mw
.html
.escape( 'foo' ), 'mocked-1', 'extra setup() callback was ran.' );
245 assert
.equal( mw
.config
.get( 'testVar' ), 'foo', 'config object applied' );
246 assert
.equal( mw
.messages
.get( 'testMsg' ), 'Foo.', 'messages object applied' );
248 mw
.config
.set( 'testVar', 'bar' );
249 mw
.messages
.set( 'testMsg', 'Bar.' );
252 QUnit
.test( 'Teardown', 3, function ( assert
) {
253 assert
.equal( mw
.html
.escape( 'foo' ), 'mocked-2', 'extra setup() callback was re-ran.' );
254 assert
.equal( mw
.config
.get( 'testVar' ), 'foo', 'config object restored and re-applied after test()' );
255 assert
.equal( mw
.messages
.get( 'testMsg' ), 'Foo.', 'messages object restored and re-applied after test()' );
258 QUnit
.module( 'mediawiki.tests.qunit.testrunner-after', QUnit
.newMwEnvironment() );
260 QUnit
.test( 'Teardown', 3, function ( assert
) {
261 assert
.equal( mw
.html
.escape( '<' ), '<', 'extra teardown() callback was ran.' );
262 assert
.equal( mw
.config
.get( 'testVar' ), null, 'config object restored to live in next module()' );
263 assert
.equal( mw
.messages
.get( 'testMsg' ), null, 'messages object restored to live in next module()' );
266 }( jQuery
, mediaWiki
, QUnit
) );