Manual:JavaScript unit testing/en

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.

Run from the command line
One-time setup:


 * 1) Ensure environment variables   and   are set. Either each time, or from   to have them always:
 * 2) Enter an isolated environment where you have Node.js and npm. Why?
 * 3) * If using MediaWiki-Docker, consider using Fresh. Run  from the MediaWiki directory.
 * 4) *If using MediaWiki-Vagrant, simply ssh to your Vagrant VM.
 * 5) Once inside your isolated shell, run  to install or update any dependencies.

Run
This runs the QUnit tests in Headless Chrome:

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