Manual:JavaScript unit testing

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  directory. 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


Make sure is set to.

In a browser
Unit tests are run using a special page.

Open in any browser to run the tests.

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

From the command line
Use  from the MediaWiki core directory to run QUnit tests from the command-line in you local Chrome browser.

Run tests before committing
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
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

 * TODO: Incomplete


 * The file should be named after the module (or file) it is testing. E.g.  covers the   module.


 * Inside the test suite should be one, and only one, call to  with the module name.

MediaWiki core
Core test suites need to be added to the registration of the  module. Do so in.

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

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

Tips

 * 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.