Manual:JavaScript unit testing
MediaWiki uses QUnit to unit test its JavaScript code base.
The unit tests for MediaWiki core are located in the tests/qunit/suites directory[1].
Contents
Run the tests[edit | edit source]
Ensure $wgEnableJavaScriptTest
is set to true
.
In a browser[edit | edit source]
Open Special:JavaScriptTest/qunit/plain in a browser to run the tests.
Options:
- Check for Globals
- Core URL parameter of QUnit.[2]
- No try-catch
- Core URL parameter of QUnit.[2]
- Enable ResourceLoaderDebug
- Toggle ResourceLoader debug mode.
- Module
- Select a module from the list to immediately run it.[3]
From the command line[edit | edit source]
Use npm install && grunt qunit
from the MediaWiki core directory to run QUnit tests from the command-line in Chrome and aggregate the results.
How to help?[edit | edit source]
Run tests before committing[edit | edit source]
Make it a habit to run unit tests before committing and submitting your changes to Gerrit.
Write unit tests[edit | edit source]
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[edit | edit source]
- TODO: Incomplete
- The file should be named after the module (or file) it is testing. E.g.
mediawiki.user.test.js
covers themediawiki.user
module.
- Inside the test suite should be one, and only one, call to
QUnit.module
with the module name.
How to register a test suite[edit | edit source]
MediaWiki core[edit | edit source]
Test suites are added to the registration of the mediawiki.tests.qunit.suites
module in /tests/qunit/QUnitTestResources.php
.
Tests are organised into a directory structure that matches the directory structure of the code being tested. For example: The unit test for resources/mediawiki.util/mediawiki.util.js
can be found at tests/qunit/suites/resources/mediawiki.util/mediawiki.util.test.js
.
Example:
'mediawiki.tests.qunit.suites' => array(
'scripts' => array(
'tests/qunit/suites/resources/mediawiki/mediawiki.test.js',
+ 'tests/qunit/suites/resources/mediawiki/mediawiki.foobar.test.js',
'tests/qunit/suites/resources/mediawiki/mediawiki.user.test.js',
'tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js',
),
'dependencies' => array(
'mediawiki',
+ 'mediawiki.foobar',
'mediawiki.user',
'mediawiki.util',
Extensions[edit | edit source]
Extensions register their QUnit tests as a module. See Manual:Hooks/ResourceLoaderTestModules for more details.
See also[edit | edit source]
- Manual:Writing testable JavaScript
- Manual:Hooks/ResourceLoaderTestModules
- Manual:JavaScript unit testing/QUnit guidelines
Notes[edit | edit source]
- ↑ Before r88431 , unit tests were located in the resources/test directory.
- ↑ 2.0 2.1 https://qunitjs.com/cookbook/#automating-unit-testing
- ↑ https://qunitjs.com/cookbook/#efficient-development