Manual:JavaScript unit testing

MediaWiki uses QUnit to unit test its JavaScript code base.

Run from a browser

 * 1) Set   to  .  (This is already set if you use MediaWiki-Docker-Dev or MediaWiki-Vagrant.)
 * 2) Visit Special:JavaScriptTest in a browser to run the tests.

Getting started

 * 1) Enter an isolated environment where you have Node.js and npm installed. Why?
 * 2) * For MediaWiki-Vagrant, ssh to your Vagrant VM.
 * 3) * For MDD or other, consider Fresh. Run.
 * 4) Run  from the MediaWiki directory.

Run
This runs the QUnit tests in Headless Chrome and prints the results. $ npm run qunit Running "karma:main" (karma) task START: INFO [launcher]: Starting browser ChromeHeadless … mediawiki.util ✔ escapeRegExp ✔ debounce … Finished in 5.659 secs / 5.442 secs SUMMARY: ✔ 440 tests completed Done.

Run tests before committing
Make it a habit to run unit tests before committing and submitting your changes to 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 test
It is the convention to name the test suite file after the module it covers. For example,  covers the   module.

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

The unit tests for MediaWiki core are located in the  directory.

MediaWiki core
Test suites are added to the registration of the  module in.

Tests are organised into a directory structure that matches the directory structure of the code being tested. For example: The unit test for  can be found at.

Example:

Extensions
Extensions register their QUnit tests via the ResourceLoaderTestModules hook.