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;
151 * Add-on assertion helpers
153 // Define the add-ons
156 // Expect boolean true
157 assertTrue: function ( actual, message ) {
158 strictEqual( actual, true, message );
161 // Expect boolean false
162 assertFalse: function ( actual, message ) {
163 strictEqual( actual, false, message );
166 // Expect numerical value less than X
167 lt: function ( actual, expected, message ) {
168 QUnit.push( actual < expected, actual, 'less than ' + expected, message );
171 // Expect numerical value less than or equal to X
172 ltOrEq: function ( actual, expected, message ) {
173 QUnit.push( actual <= expected, actual, 'less than or equal to ' + expected, message );
176 // Expect numerical value greater than X
177 gt: function ( actual, expected, message ) {
178 QUnit.push( actual > expected, actual, 'greater than ' + expected, message );
181 // Expect numerical value greater than or equal to X
182 gtOrEq: function ( actual, expected, message ) {
183 QUnit.push( actual >= expected, actual, 'greater than or equal to ' + expected, message );
186 // Backwards compatible with new verions of QUnit
187 equals: window.equal,
188 same: window.deepEqual
191 $.extend( QUnit, addons );
192 $.extend( window, addons );
195 * Small test suite to confirm proper functionality of the utilities and
196 * initializations in this file.
198 var envExecCount = 0;
199 module( 'mediawiki.tests.qunit.testrunner', QUnit.newMwEnvironment({
202 this.mwHtmlLive = mw.html;
204 escape: function () {
205 return 'mocked-' + envExecCount;
209 teardown: function () {
210 mw.html = this.mwHtmlLive;
220 test( 'Setup', function () {
223 equal( mw.html.escape( 'foo' ), 'mocked-1', 'extra setup() callback was ran.' );
224 equal( mw.config.get( 'testVar' ), 'foo', 'config object applied' );
225 equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object applied' );
227 mw.config.set( 'testVar', 'bar' );
228 mw.messages.set( 'testMsg', 'Bar.' );
231 test( 'Teardown', function () {
234 equal( mw.html.escape( 'foo' ), 'mocked-2', 'extra setup() callback was re-ran.' );
235 equal( mw.config.get( 'testVar' ), 'foo', 'config object restored and re-applied after test()' );
236 equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object restored and re-applied after test()' );
239 module( 'mediawiki.tests.qunit.testrunner-after', QUnit.newMwEnvironment() );
241 test( 'Teardown', function () {
244 equal( mw.html.escape( '<' ), '<', 'extra teardown() callback was ran.' );
245 equal( mw.config.get( 'testVar' ), null, 'config object restored to live in next module()' );
246 equal( mw.messages.get( 'testMsg' ), null, 'messages object restored to live in next module()' );
249 })( jQuery, mediaWiki, QUnit );