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'
39 QUnit
.config
.requireExpects
= true;
42 * Load TestSwarm agent
44 // Only if the current url indicates that there is a TestSwarm instance watching us
45 // (TestSwarm appends swarmURL to the test suites url it loads in iframes).
46 // Otherwise this is just a simple view of Special:JavaScriptTest/qunit directly,
47 // no point in loading inject.js in that case. Also, make sure that this instance
48 // of MediaWiki has actually been configured with the required url to that inject.js
49 // script. By default it is false.
50 if ( QUnit
.urlParams
.swarmURL
&& mw
.config
.get( 'QUnitTestSwarmInjectJSPath' ) ) {
51 document
.write( '<scr' + 'ipt src="' + QUnit
.fixurl( mw
.config
.get( 'QUnitTestSwarmInjectJSPath' ) ) + '"></scr' + 'ipt>' );
57 // Adds toggle checkbox to header
58 QUnit
.config
.urlConfig
.push( {
59 id
: 'completenesstest',
60 label
: 'Run CompletenessTest',
61 tooltip
: 'Run the completeness test'
64 // Initiate when enabled
65 if ( QUnit
.urlParams
.completenesstest
) {
67 // Return true to ignore
68 mwTestIgnore = function ( val
, tester
) {
70 // Don't record methods of the properties of constructors,
71 // to avoid getting into a loop (prototype.constructor.prototype..).
72 // Since we're therefor skipping any injection for
73 // "new mw.Foo()", manually set it to true here.
74 if ( val
instanceof mw
.Map
) {
75 tester
.methodCallTracker
.Map
= true;
78 if ( val
instanceof mw
.Title
) {
79 tester
.methodCallTracker
.Title
= true;
83 // Don't record methods of the properties of a jQuery object
84 if ( val
instanceof $ ) {
91 mwTester
= new CompletenessTest( mw
, mwTestIgnore
);
95 * Test environment recommended for all QUnit test modules
97 // Whether to log environment changes to the console
98 QUnit
.config
.urlConfig
.push( 'mwlogenv' );
101 * Reset mw.config and others to a fresh copy of the live config for each test(),
102 * and restore it back to the live one afterwards.
103 * @param localEnv {Object} [optional]
104 * @example (see test suite at the bottom of this file)
107 QUnit
.newMwEnvironment
= ( function () {
108 var log
, liveConfig
, liveMessages
;
110 liveConfig
= mw
.config
.values
;
111 liveMessages
= mw
.messages
.values
;
113 function freshConfigCopy( custom
) {
114 // Tests should mock all factors that directly influence the tested code.
115 // For backwards compatibility though we set mw.config to a copy of the live config
116 // and extend it with the (optionally) given custom settings for this test
117 // (instead of starting blank with only the given custmo settings).
118 // This is a shallow copy, so we don't end up with settings taking an array value
119 // extended with the custom settings - setting a config property means you override it,
121 return $.extend( {}, liveConfig
, custom
);
124 function freshMessagesCopy( custom
) {
125 return $.extend( /*deep=*/true, {}, liveMessages
, custom
);
128 log
= QUnit
.urlParams
.mwlogenv
? mw
.log : function () {};
130 return function ( localEnv
) {
131 localEnv
= $.extend( {
142 log( 'MwEnvironment> SETUP for "' + QUnit
.config
.current
.module
143 + ': ' + QUnit
.config
.current
.testName
+ '"' );
145 // Greetings, mock environment!
146 mw
.config
.values
= freshConfigCopy( localEnv
.config
);
147 mw
.messages
.values
= freshMessagesCopy( localEnv
.messages
);
152 teardown: function () {
153 log( 'MwEnvironment> TEARDOWN for "' + QUnit
.config
.current
.module
154 + ': ' + QUnit
.config
.current
.testName
+ '"' );
158 // Farewell, mock environment!
159 mw
.config
.values
= liveConfig
;
160 mw
.messages
.values
= liveMessages
;
166 // $.when stops as soon as one fails, which makes sense in most
167 // practical scenarios, but not in a unit test where we really do
168 // need to wait until all of them are finished.
169 QUnit
.whenPromisesComplete = function () {
170 var altPromises
= [];
172 $.each( arguments
, function ( i
, arg
) {
173 var alt
= $.Deferred();
174 altPromises
.push( alt
);
176 // Whether this one fails or not, forwards it to
177 // the 'done' (resolve) callback of the alternative promise.
178 arg
.always( alt
.resolve
);
181 return $.when
.apply( $, altPromises
);
185 * Add-on assertion helpers
187 // Define the add-ons
190 // Expect boolean true
191 assertTrue: function ( actual
, message
) {
192 QUnit
.push( actual
=== true, actual
, true, message
);
195 // Expect boolean false
196 assertFalse: function ( actual
, message
) {
197 QUnit
.push( actual
=== false, actual
, false, message
);
200 // Expect numerical value less than X
201 lt: function ( actual
, expected
, message
) {
202 QUnit
.push( actual
< expected
, actual
, 'less than ' + expected
, message
);
205 // Expect numerical value less than or equal to X
206 ltOrEq: function ( actual
, expected
, message
) {
207 QUnit
.push( actual
<= expected
, actual
, 'less than or equal to ' + expected
, message
);
210 // Expect numerical value greater than X
211 gt: function ( actual
, expected
, message
) {
212 QUnit
.push( actual
> expected
, actual
, 'greater than ' + expected
, message
);
215 // Expect numerical value greater than or equal to X
216 gtOrEq: function ( actual
, expected
, message
) {
217 QUnit
.push( actual
>= expected
, actual
, 'greater than or equal to ' + expected
, message
);
221 $.extend( QUnit
.assert
, addons
);
224 * Small test suite to confirm proper functionality of the utilities and
225 * initializations defined above in this file.
228 QUnit
.module( 'mediawiki.tests.qunit.testrunner', QUnit
.newMwEnvironment( {
231 this.mwHtmlLive
= mw
.html
;
233 escape: function () {
234 return 'mocked-' + envExecCount
;
238 teardown: function () {
239 mw
.html
= this.mwHtmlLive
;
249 QUnit
.test( 'Setup', 3, function ( assert
) {
250 assert
.equal( mw
.html
.escape( 'foo' ), 'mocked-1', 'extra setup() callback was ran.' );
251 assert
.equal( mw
.config
.get( 'testVar' ), 'foo', 'config object applied' );
252 assert
.equal( mw
.messages
.get( 'testMsg' ), 'Foo.', 'messages object applied' );
254 mw
.config
.set( 'testVar', 'bar' );
255 mw
.messages
.set( 'testMsg', 'Bar.' );
258 QUnit
.test( 'Teardown', 3, function ( assert
) {
259 assert
.equal( mw
.html
.escape( 'foo' ), 'mocked-2', 'extra setup() callback was re-ran.' );
260 assert
.equal( mw
.config
.get( 'testVar' ), 'foo', 'config object restored and re-applied after test()' );
261 assert
.equal( mw
.messages
.get( 'testMsg' ), 'Foo.', 'messages object restored and re-applied after test()' );
264 QUnit
.module( 'mediawiki.tests.qunit.testrunner-after', QUnit
.newMwEnvironment() );
266 QUnit
.test( 'Teardown', 3, function ( assert
) {
267 assert
.equal( mw
.html
.escape( '<' ), '<', 'extra teardown() callback was ran.' );
268 assert
.equal( mw
.config
.get( 'testVar' ), null, 'config object restored to live in next module()' );
269 assert
.equal( mw
.messages
.get( 'testMsg' ), null, 'messages object restored to live in next module()' );
272 }( jQuery
, mediaWiki
, QUnit
) );