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 or MediaWiki-Vagrant.)
 * 2) Visit Special:JavaScriptTest in a browser to run the tests.

One-time setup
Enter an isolated environment where you have Node.js and npm. Why?

If using MediaWiki-Docker:


 * Consider using Fresh
 * Run  from the MediaWiki core directory. (Fresh automatically finds the MW address in Docker's   file.)
 * Once inside your isolated shell, run  to install or update any dependencies.

If using MediaWiki-Vagrant:

If using a different setup:
 * Ensure environment variables  and   are set (consider saving this to  ):
 * Run  from the MediaWiki-Vagrant directory.
 * Once inside the Vagrant VM. navigate to the MediaWiki core directory and run.


 * Ensure environment variables  and   are set (consider saving this to  ):
 * Consider using Fresh, or if you have an isolated environment already, ensure a comparable version of Node.js and npm are installed (the before-last LTS should do).
 * Navigate to the MediaWiki core directory and run.

Run
This runs the QUnit tests in Headless Chrome:

You can run QUnit tests for specific components with the  option. Pass the name of an extension or skin, or for MediaWiki core tests.

You can use the  argument for QUnit to watch the files associated with a component, and automatically re-run tests when those files change.

Run tests before committing
Make it a habit to run unit tests before committing and submitting your changes to Gerrit.

Write unit tests
Write unit tests for new functionality, and consider fillling gaps in coverage when changing existing modules in MediaWiki core.

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 a 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 QUnitTestModule attribute in.