Movatterモバイル変換


[0]ホーム

URL:


Jump to content
MediaWiki
Search

Manual:JavaScript unit testing

From mediawiki.org
Translate this page
Languages:
MediaWiki's QUnit test suite.

MediaWiki usesQUnit tounit test itsJavaScript code base.

Run from a browser

[edit]
  1. Set$wgEnableJavaScriptTest totrue.
    This is already set in most developer environments, likeQuickstart orMediaWiki-Docker.
  2. VisitSpecial:JavaScriptTest in a browser to run the tests.

Run from the command line

[edit]

One-time setup

[edit]
  • Set the environment variablesMW_SERVER andMW_SCRIPT_PATH to the URL of your local wiki. You can also put these in a.bash_profile file in your home directory to automatically set load variables in new Terminal tabs.
    # MediaWiki-DockerexportMW_SERVER=http://localhost:8080exportMW_SCRIPT_PATH=/w
    # composer serveexportMW_SERVER=http://localhost:4000exportMW_SCRIPT_PATH=/
  • Navigate to the MediaWiki core directory and runnpm ci.

Run

[edit]

This runs the QUnit tests in Headless Chrome:

$npmrunqunitRunning "karma:main" (karma) task INFO [launcher]: Starting browser ChromeHeadless mediawiki.util     ✔ escapeRegExp     ✔ debounce Finished in 5.42 secs SUMMARY: ✔ 440 tests completed

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

$node_modules/grunt/bin/gruntqunit--qunit-component=GrowthExperimentsRunning "karma:main" (karma) taskINFO [launcher]: Starting browser ChromeHeadless  testrunner    ✔ Loader status    ✔ assert.htmlEqual  ext.growthExperiments.Utils.js    ✔ serializeActionData    ✔ isUserInVariant  ext.growthExperiments.Help/HelpPanelProcessDialog.js    ✔ getDefaultPanelForSuggestedEditSession for copyedit    ✔ updateEditMode for link-recommendation    ✔ updateEditMode for copyedit, isEditing  ext.growthExperiments.Homepage.SuggestedEdits/PagerWidget.js    ✔ constructor    ✔ setMessage with currentPosition < totalCount    ✔ setMessage with currentPosition === totalCount    ✔ setMessage with currentPosition > totalCount  ext.growthExperiments.NewcomerTaskLogger.js    ✔ constructor    ✔ should log impressions    ✔ should get log metadata  ext.growthExperiments.StructuredTask/addimage/AddImageUtils.js    ✔ getRenderData: target width < original width    ✔ getRenderData: the image file needs to be re-rasterized    ✔ getRenderData: vector image    ✔ getRenderData: target width > original width    ✔ getRenderData: 3x target width    ✔ getRenderData: 2.5x target width    ✔ getRenderData: vertical image with landscape viewport    ✔ getRenderData: with specified render widthFinished in 0.03 secsSUMMARY:✔ 46 tests completed

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

Security consideration

[edit]

Consider usingFresh, or if you have another isolated environment already, ensure a comparable version of Node.js and npm are installed (the before-last Node.js LTS is recommended).Why?

  • To enter the isolated environment, runfresh-node-net-env from the MediaWiki core directory. (Fresh automatically finds the MW_SERVER and MW_SCRIPT_PATH variables from MediaWiki-Docker's.env file.)
  • Once inside your isolated shell, runnpm ci to install or update any dependencies.
  • To start the tests, runnpm run qunit.

How to contribute?

[edit]

Run tests before committing

[edit]

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

Write unit tests

[edit]

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

Write a test

[edit]

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

Inside the test suite should be a call toQUnit.module with the module name.

The unit tests for MediaWiki core are located in thetests/qunit/resources/ directory.

Register a test

[edit]

MediaWiki core

[edit]

Test suites are added to the registration of thetest.MediaWiki 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 forresources/mediawiki.base/util.js can be found attests/qunit/resources/mediawiki.util/util.test.js.

Example:

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

Extensions

[edit]

Extensions register their QUnit tests via theQUnitTestModule attribute inextension.json.

See also

[edit]
Retrieved from "https://www.mediawiki.org/w/index.php?title=Manual:JavaScript_unit_testing&oldid=6327584"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp