First QUnit test for Translate extension – with tutorial

It’s about time the Translate extension gets QUnit tests: the amount of JavaScript in it has exploded in the past year. Here is a quick intro on how to add QUnit tests for a MediaWiki extension which doesn’t have any yet.

Step 1: Create a tests directory.

The Translate extension already has a tests/ directory with a lot of PHPUnit tests . For now I just created a qunit subdirectory under it.

Step 1: Create a test file.

The function I want to test is in a file at
I created a corresponding test file

Step 3: Register the test file.

In Translate, all the resource loader modules are defined in Resources.php. At the bottom of the file I register the test modules via the ResourceLoaderTestModules hook with an anonymous function.

$wgHooks['ResourceLoaderTestModules'][] =
	// Dependencies must be arrays here
	function ( array &$modules ) use ( $resourcePaths ) {
		$modules['qunit']['ext.translate.parsers.test'] = array(
			'scripts' => array( 'tests/qunit/ext.translate.parsers.test.js' ),
			'dependencies' => array( 'ext.translate.parsers' ),
		) + $resourcePaths;

		return true;

The $resourcePaths I have defined already earlier:

$resourcePaths = array(
	'localBasePath' => __DIR__,
	'remoteExtPath' => 'Translate'

Step 4: Write the tests

Here is a simple example with only one test. Note how the assert is taken via function parameter to avoid using global functions.

 * Tests for ext.translate.parsers.js.
 * @file
 * @licence GPL-2.0+

( function ( $, mw ) {
	'use strict';

	QUnit.module( 'ext.translate.parsers', QUnit.newMwEnvironment() );

	QUnit.test( '-- External links', 1, function ( assert ) {
			'This page is [in English]',
			mw.translate.formatMessageGently( 'This page is [in English]' ),
			'Brackets without protocol doesn\'t make a link'
	} );

}( jQuery, mediaWiki ) );

Step 5: Run the tests

I ran the tests in my development wiki and they passed. The patch set is in Gerrit. Also see the QUnit page in

-- .