Manual:JavaScript unit testing

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

The unit tests are located in the tests/qunit directory. Tests are organized 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.

Running the unit tests


Unit tests are ran from the browser. To do so:
 * 1) Set  to.
 * 2) Go to  on your wiki and select the framework of choise, or go to  directly
 * 3) No step 3, tests are being run now!

CompletenessTest


In order to enable completeness test plugin, tick the "completenesstest" checkbox in the header. A report like the one on the right here will appear after the test suite is ran.

Join the Swarm
Go to our [//integration.mediawiki.org/testswarm TestSwarm homepage] and join the swarm and letting it run in the background. This helps developers catch problems early on. TestSwarm automatically keeps the browser window up to date with the latest unit tests and automatically communicates back and forth reporting successes and problems.

Run tests before committing
Make it a habit to run unit tests before committing your modifications to MediaWiki SVN. Any problems that you didn't cauase? See if you can find a cause and let the committer know by leaving a comment in CodeReview (mark it FIXME as well).

Write unit tests
Always provide unit tests for new functionality. We're also looking to expand our unit test coverage of already existing modules in the MediaWiki JavaScript library. Run the CompletnessTest and find out which methods don't have unit tests yet and write one.

Write a unit test

 * TODO: Incomplete


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

QUnit

 * Inside the test suite file should be one, and one exactly, call to QUnit's . The string passed to   must match the filename (but without the .test.js part). This is to enable sectioning in the QUnit output as well as for TestSwarm to allow distributed and asynchronous testing of each module separately.

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

So if you created a test suite for, then that test suite should be stored in ./tests/qunit/suites/resources/mediawiki/mediawiki.foobar.test. Then you would add that file path to the "scripts" array. And, unless already, add the  module as a dependency (since that module must be loaded before the test suite can run).

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