Manual:JavaScript unit testing

From MediaWiki.org
Jump to: navigation, search

Unit testing in MediaWiki for its JavaScript code base is performed using the QUnit JavaScript Testing framework..

The unit tests in core are located in the tests/qunit directory[1]. Tests are organised into a directory structure that matches the directory structure of the code that they are testing. For example: The unit tests for file resources/mediawiki.util/mediawiki.util.js can be found in tests/qunit/suites/resources/mediawiki.util/mediawiki.util.test.js.

Extensions add their QUnit tests on the ResourceLoaderTestModules hook.

Run the tests[edit | edit source]

If all goes well, the tests will quickly run and show a green bar at the top like this.

Make sure $wgEnableJavaScriptTest is set to true.

In a browser[edit | edit source]

Unit tests are run using a special page.

Open Special:JavaScriptTest/qunit/plain in any browser to run the tests.

Options:

Check for Globals
Core URL parameter of QUnit.[2]
No try-catch
Core URL parameter of QUnit.[2]
Enable ResourceLoaderDebug
Toggle ResourceLoader debug mode.
mwlogenv
When enabled, setup and teardown of QUnit.newMwEnvironment will be logged to the console.
Module
Select a module from the list to immediately run it.[3]

From the command line[edit | edit source]

Use npm install && grunt qunit from the MediaWiki core directory to run QUnit tests from the command-line in you local Chrome browser.

How to help?[edit | edit source]

Run tests before committing[edit | edit source]

Make it a habit to run unit tests before committing and submitting your changes to Gerrit. Any problems that you didn't cause? See if you can find a cause and let the committer know by leaving a comment in Gerrit.

Write unit tests[edit | edit source]

Always write unit tests for new functionality. We're also looking to expand our unit test coverage of already existing modules in the MediaWiki JavaScript library.

Write a unit test[edit | edit source]

TODO: Incomplete
  • The file should be named after the module (or file) it is testing. E.g. mediawiki.user.test.js covers the mediawiki.user module.
  • Inside the test suite should be one, and only one, call to QUnit.module with the module name.

How to register a test suite[edit | edit source]

MediaWiki core[edit | edit source]

Core test suites need to be added to the registration of the mediawiki.tests.qunit.suites module. Do so in /tests/qunit/QUnitTestResources.php.

If you created a test suite for mediawiki.foobar, then that test suite should be in /tests/qunit/suites/resources/mediawiki/mediawiki.foobar.test, and add that file path to the "scripts" array. Ensure the mediawiki.foobar module is listed as a dependency (as that module must be loaded before the test suite).

 'mediawiki.tests.qunit.suites' => array(
 	'scripts' => array(
 		'tests/qunit/suites/resources/mediawiki/mediawiki.test.js',
+		'tests/qunit/suites/resources/mediawiki/mediawiki.foobar.test.js',
 		'tests/qunit/suites/resources/mediawiki/mediawiki.user.test.js',
 		'tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js',
 	),
 	'dependencies' => array(
 		'mediawiki',
+		'mediawiki.foobar',
 		'mediawiki.user',
 		'mediawiki.util',

Extensions[edit | edit source]

Extensions register their tests suites as a module. See Manual:Hooks/ResourceLoaderTestModules for more details.

Tips[edit | edit source]

  • The tests should work in all language environments, but sometimes may fail if $wgLanguageCode is not "en". If your wiki's language is not "en" and tests fail mysteriously, try changing $wgLanguageCode to "en" and running the tests, and then try with your language again.

See also[edit | edit source]

Notes[edit | edit source]

  1. Before r88431 , unit tests were located in the resources/test directory.
  2. 2.0 2.1 http://qunitjs.com/cookbook/#automating-unit-testing
  3. http://qunitjs.com/cookbook/#efficient-development