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 // Tests should mock all factors that directly influence the tested code.
113 // For backwards compatibility though we set mw.config to a copy of the live config
114 // and extend it with the (optionally) given custom settings for this test
115 // (instead of starting blank with only the given custmo settings).
116 // This is a shallow copy, so we don't end up with settings taking an array value
117 // extended with the custom settings - setting a config property means you override it,
119 return $.extend( {}, liveConfig
, custom
);
122 function freshMessagesCopy( custom
) {
123 return $.extend( /*deep=*/true, {}, liveMessages
, custom
);
126 log
= QUnit
.urlParams
.mwlogenv
? mw
.log : function () {};
128 return function ( localEnv
) {
129 localEnv
= $.extend( {
140 log( 'MwEnvironment> SETUP for "' + QUnit
.config
.current
.module
141 + ': ' + QUnit
.config
.current
.testName
+ '"' );
143 // Greetings, mock environment!
144 mw
.config
.values
= freshConfigCopy( localEnv
.config
);
145 mw
.messages
.values
= freshMessagesCopy( localEnv
.messages
);
150 teardown: function () {
151 log( 'MwEnvironment> TEARDOWN for "' + QUnit
.config
.current
.module
152 + ': ' + QUnit
.config
.current
.testName
+ '"' );
156 // Farewell, mock environment!
157 mw
.config
.values
= liveConfig
;
158 mw
.messages
.values
= liveMessages
;
164 // $.when stops as soon as one fails, which makes sense in most
165 // practical scenarios, but not in a unit test where we really do
166 // need to wait until all of them are finished.
167 QUnit
.whenPromisesComplete = function () {
168 var altPromises
= [];
170 $.each( arguments
, function ( i
, arg
) {
171 var alt
= $.Deferred();
172 altPromises
.push( alt
);
174 // Whether this one fails or not, forwards it to
175 // the 'done' (resolve) callback of the alternative promise.
176 arg
.always( alt
.resolve
);
179 return $.when
.apply( $, altPromises
);
183 * Add-on assertion helpers
185 // Define the add-ons
188 // Expect boolean true
189 assertTrue: function ( actual
, message
) {
190 QUnit
.push( actual
=== true, actual
, true, message
);
193 // Expect boolean false
194 assertFalse: function ( actual
, message
) {
195 QUnit
.push( actual
=== false, actual
, false, message
);
198 // Expect numerical value less than X
199 lt: function ( actual
, expected
, message
) {
200 QUnit
.push( actual
< expected
, actual
, 'less than ' + expected
, message
);
203 // Expect numerical value less than or equal to X
204 ltOrEq: function ( actual
, expected
, message
) {
205 QUnit
.push( actual
<= expected
, actual
, 'less than or equal to ' + expected
, message
);
208 // Expect numerical value greater than X
209 gt: function ( actual
, expected
, message
) {
210 QUnit
.push( actual
> expected
, actual
, 'greater than ' + expected
, message
);
213 // Expect numerical value greater than or equal to X
214 gtOrEq: function ( actual
, expected
, message
) {
215 QUnit
.push( actual
>= expected
, actual
, 'greater than or equal to ' + expected
, message
);
219 $.extend( QUnit
.assert
, addons
);
222 * Small test suite to confirm proper functionality of the utilities and
223 * initializations defined above in this file.
226 QUnit
.module( 'mediawiki.tests.qunit.testrunner', QUnit
.newMwEnvironment( {
229 this.mwHtmlLive
= mw
.html
;
231 escape: function () {
232 return 'mocked-' + envExecCount
;
236 teardown: function () {
237 mw
.html
= this.mwHtmlLive
;
247 QUnit
.test( 'Setup', 3, function ( assert
) {
248 assert
.equal( mw
.html
.escape( 'foo' ), 'mocked-1', 'extra setup() callback was ran.' );
249 assert
.equal( mw
.config
.get( 'testVar' ), 'foo', 'config object applied' );
250 assert
.equal( mw
.messages
.get( 'testMsg' ), 'Foo.', 'messages object applied' );
252 mw
.config
.set( 'testVar', 'bar' );
253 mw
.messages
.set( 'testMsg', 'Bar.' );
256 QUnit
.test( 'Teardown', 3, function ( assert
) {
257 assert
.equal( mw
.html
.escape( 'foo' ), 'mocked-2', 'extra setup() callback was re-ran.' );
258 assert
.equal( mw
.config
.get( 'testVar' ), 'foo', 'config object restored and re-applied after test()' );
259 assert
.equal( mw
.messages
.get( 'testMsg' ), 'Foo.', 'messages object restored and re-applied after test()' );
262 QUnit
.module( 'mediawiki.tests.qunit.testrunner-after', QUnit
.newMwEnvironment() );
264 QUnit
.test( 'Teardown', 3, function ( assert
) {
265 assert
.equal( mw
.html
.escape( '<' ), '<', 'extra teardown() callback was ran.' );
266 assert
.equal( mw
.config
.get( 'testVar' ), null, 'config object restored to live in next module()' );
267 assert
.equal( mw
.messages
.get( 'testMsg' ), null, 'messages object restored to live in next module()' );
270 }( jQuery
, mediaWiki
, QUnit
) );