Tests unitaires JavaScript

From mediawiki.org
Jump to navigation Jump to search
This page is a translated version of the page Manual:JavaScript unit testing and the translation is 100% complete.
Other languages:
English • ‎français • ‎日本語
MediaWiki's QUnit test suite running in Chrome.

MediaWiki uses QUnit to unit test its JavaScript code base.

Exécution à partir d'un navigateur

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

Exécution à partir de la ligne de commande

One-time setup:

  1. Ensure environment variables MW_SERVER and MW_SCRIPT_PATH are set. Either each time, or from ~/.bash_profile to have them always:
    export MW_SERVER=http://default.web.mw.localhost:8080
    export MW_SCRIPT_PATH=/mediawiki
  2. Enter an isolated environment where you have Node.js and npm. Why?
    • If using MediaWiki-Docker, consider using Fresh.
      Run fresh-node -net -env from the MediaWiki directory.
    • If using MediaWiki-Vagrant, simply ssh to your Vagrant VM.
  3. Once inside your isolated shell, run npm ci to install or update any dependencies.


This runs the QUnit tests in Headless Chrome:

 $ npm run qunit
 Running "karma:main" (karma) task

 INFO [launcher]: Starting browser ChromeHeadless
     ✔ escapeRegExp
     ✔ debounce

 Finished in 5.659 secs / 5.442 secs
 ✔ 440 tests completed

Comment contribuer ?

Exécuter les tests avant de valider

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

Ecrire des tests unitaires

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.

Ecrire un test

It is the convention to name the test suite file after the module it covers. For example, mediawiki.user.test.js covers the mediawiki.user module.

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

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

Enregistrer un test

Noyau MediaWiki

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.


 'mediawiki.tests.qunit.suites' => array(
 	'scripts' => array(
+		'tests/qunit/suites/resources/mediawiki/mediawiki.example.test.js',
 	'dependencies' => array(
+		'mediawiki.example',

Les extensions

Extensions register their QUnit tests via the QUnitTestModule option in extension.json.

Voir aussi