1 /*global CompletenessTest */
3 ( function ( $, mw
, QUnit
, undefined ) {
6 var mwTestIgnore
, mwTester
,
11 * Add bogus to url to prevent IE crazy caching
13 * @param value {String} a relative path (eg. 'data/foo.js'
14 * or 'data/test.php?foo=bar').
15 * @return {String} Such as 'data/foo.js?131031765087663960'
17 QUnit
.fixurl = function ( value
) {
18 return value
+ (/\?/.test( value
) ? '&' : '?')
19 + String( new Date().getTime() )
20 + String( parseInt( Math
.random() * 100000, 10 ) );
27 // When a test() indicates asynchronicity with stop(),
28 // allow 10 seconds to pass before killing the test(),
29 // and assuming failure.
30 QUnit
.config
.testTimeout
= 10 * 1000;
32 // Add a checkbox to QUnit header to toggle MediaWiki ResourceLoader debug mode.
33 QUnit
.config
.urlConfig
.push( {
35 label
: 'Enable ResourceLoaderDebug',
36 tooltip
: 'Enable debug mode in ResourceLoader'
40 * Load TestSwarm agent
42 // Only if the current url indicates that there is a TestSwarm instance watching us
43 // (TestSwarm appends swarmURL to the test suites url it loads in iframes).
44 // Otherwise this is just a simple view of Special:JavaScriptTest/qunit directly,
45 // no point in loading inject.js in that case. Also, make sure that this instance
46 // of MediaWiki has actually been configured with the required url to that inject.js
47 // script. By default it is false.
48 if ( QUnit
.urlParams
.swarmURL
&& mw
.config
.get( 'QUnitTestSwarmInjectJSPath' ) ) {
49 document
.write( '<scr' + 'ipt src="' + QUnit
.fixurl( mw
.config
.get( 'QUnitTestSwarmInjectJSPath' ) ) + '"></scr' + 'ipt>' );
55 // Adds toggle checkbox to header
56 QUnit
.config
.urlConfig
.push( {
57 id
: 'completenesstest',
58 label
: 'Run CompletenessTest',
59 tooltip
: 'Run the completeness test'
62 // Initiate when enabled
63 if ( QUnit
.urlParams
.completenesstest
) {
65 // Return true to ignore
66 mwTestIgnore = function ( val
, tester
) {
68 // Don't record methods of the properties of constructors,
69 // to avoid getting into a loop (prototype.constructor.prototype..).
70 // Since we're therefor skipping any injection for
71 // "new mw.Foo()", manually set it to true here.
72 if ( val
instanceof mw
.Map
) {
73 tester
.methodCallTracker
.Map
= true;
76 if ( val
instanceof mw
.Title
) {
77 tester
.methodCallTracker
.Title
= true;
81 // Don't record methods of the properties of a jQuery object
82 if ( val
instanceof $ ) {
89 mwTester
= new CompletenessTest( mw
, mwTestIgnore
);
93 * Test environment recommended for all QUnit test modules
95 // Whether to log environment changes to the console
96 QUnit
.config
.urlConfig
.push( 'mwlogenv' );
99 * Reset mw.config and others to a fresh copy of the live config for each test(),
100 * and restore it back to the live one afterwards.
101 * @param localEnv {Object} [optional]
102 * @example (see test suite at the bottom of this file)
105 QUnit
.newMwEnvironment
= ( function () {
106 var log
, liveConfig
, liveMessages
;
108 liveConfig
= mw
.config
.values
;
109 liveMessages
= mw
.messages
.values
;
111 function freshConfigCopy( custom
) {
112 // "deep=true" is important here.
113 // Otherwise we just create a new object with values referring to live config.
114 // e.g. mw.config.set( 'wgFileExtensions', [] ) would not effect liveConfig,
115 // but mw.config.get( 'wgFileExtensions' ).push( 'png' ) would as the array
116 // was passed by reference in $.extend's loop.
117 return $.extend( {}, liveConfig
, custom
, /*deep=*/true );
120 function freshMessagesCopy( custom
) {
121 return $.extend( {}, liveMessages
, custom
, /*deep=*/true );
124 log
= QUnit
.urlParams
.mwlogenv
? mw
.log : function () {};
126 return function ( localEnv
) {
127 localEnv
= $.extend( {
138 log( 'MwEnvironment> SETUP for "' + QUnit
.config
.current
.module
139 + ': ' + QUnit
.config
.current
.testName
+ '"' );
141 // Greetings, mock environment!
142 mw
.config
.values
= freshConfigCopy( localEnv
.config
);
143 mw
.messages
.values
= freshMessagesCopy( localEnv
.messages
);
148 teardown: function () {
149 log( 'MwEnvironment> TEARDOWN for "' + QUnit
.config
.current
.module
150 + ': ' + QUnit
.config
.current
.testName
+ '"' );
154 // Farewell, mock environment!
155 mw
.config
.values
= liveConfig
;
156 mw
.messages
.values
= liveMessages
;
162 // $.when stops as soon as one fails, which makes sense in most
163 // practical scenarios, but not in a unit test where we really do
164 // need to wait until all of them are finished.
165 QUnit
.whenPromisesComplete = function () {
166 var altPromises
= [];
168 $.each( arguments
, function ( i
, arg
) {
169 var alt
= $.Deferred();
170 altPromises
.push( alt
);
172 // Whether this one fails or not, forwards it to
173 // the 'done' (resolve) callback of the alternative promise.
174 arg
.always( alt
.resolve
);
177 return $.when
.apply( $, altPromises
);
181 * Add-on assertion helpers
183 // Define the add-ons
186 // Expect boolean true
187 assertTrue: function ( actual
, message
) {
188 QUnit
.push( actual
=== true, actual
, true, message
);
191 // Expect boolean false
192 assertFalse: function ( actual
, message
) {
193 QUnit
.push( actual
=== false, actual
, false, message
);
196 // Expect numerical value less than X
197 lt: function ( actual
, expected
, message
) {
198 QUnit
.push( actual
< expected
, actual
, 'less than ' + expected
, message
);
201 // Expect numerical value less than or equal to X
202 ltOrEq: function ( actual
, expected
, message
) {
203 QUnit
.push( actual
<= expected
, actual
, 'less than or equal to ' + expected
, message
);
206 // Expect numerical value greater than X
207 gt: function ( actual
, expected
, message
) {
208 QUnit
.push( actual
> expected
, actual
, 'greater than ' + expected
, message
);
211 // Expect numerical value greater than or equal to X
212 gtOrEq: function ( actual
, expected
, message
) {
213 QUnit
.push( actual
>= expected
, actual
, 'greater than or equal to ' + expected
, message
);
217 $.extend( QUnit
.assert
, addons
);
220 * Small test suite to confirm proper functionality of the utilities and
221 * initializations defined above in this file.
224 QUnit
.module( 'mediawiki.tests.qunit.testrunner', QUnit
.newMwEnvironment({
227 this.mwHtmlLive
= mw
.html
;
229 escape: function () {
230 return 'mocked-' + envExecCount
;
234 teardown: function () {
235 mw
.html
= this.mwHtmlLive
;
245 QUnit
.test( 'Setup', 3, function ( assert
) {
246 assert
.equal( mw
.html
.escape( 'foo' ), 'mocked-1', 'extra setup() callback was ran.' );
247 assert
.equal( mw
.config
.get( 'testVar' ), 'foo', 'config object applied' );
248 assert
.equal( mw
.messages
.get( 'testMsg' ), 'Foo.', 'messages object applied' );
250 mw
.config
.set( 'testVar', 'bar' );
251 mw
.messages
.set( 'testMsg', 'Bar.' );
254 QUnit
.test( 'Teardown', 3, function ( assert
) {
255 assert
.equal( mw
.html
.escape( 'foo' ), 'mocked-2', 'extra setup() callback was re-ran.' );
256 assert
.equal( mw
.config
.get( 'testVar' ), 'foo', 'config object restored and re-applied after test()' );
257 assert
.equal( mw
.messages
.get( 'testMsg' ), 'Foo.', 'messages object restored and re-applied after test()' );
260 QUnit
.module( 'mediawiki.tests.qunit.testrunner-after', QUnit
.newMwEnvironment() );
262 QUnit
.test( 'Teardown', 3, function ( assert
) {
263 assert
.equal( mw
.html
.escape( '<' ), '<', 'extra teardown() callback was ran.' );
264 assert
.equal( mw
.config
.get( 'testVar' ), null, 'config object restored to live in next module()' );
265 assert
.equal( mw
.messages
.get( 'testMsg' ), null, 'messages object restored to live in next module()' );
268 }( jQuery
, mediaWiki
, QUnit
) );