Help:Using QUnit

From semantic-mediawiki.org
Writing testable code
JavaScript and jQuery
Keywords
Tests · Testable code · QUnit
Table of Contents

Semantic MediaWiki functionality relies in some part on JavaScript and jQuery and with an increasingly diverse browser environment (mobile and desktop browsers) different scopes putting constraints on provided functionality therefore testing across different platforms and browsers becomes vital to ensure similar user experiences and behaviour.

Run tests[edit]

Follow the instructions on MediaWiki's JavaScript unit testing to setup testing via browser or command line. If configuration parameter $wgEnableJavaScriptTest (MediaWiki.org) is set to true and tests have been successfully registered, those can be accessed via Special:JavaScriptTest.

To run only a specific module, you can add Special:JavaScriptTest/qunit?module=<module name> (e.g. Special:JavaScriptTest/qunit?module=ext.smw)

Available modules can be found in QUnit's web interface in the module dropdown.

Register tests[edit]

Hooks/ResourceLoaderTestModules is used to register available tests.(See also SemanticMediawiki.hooks.php and tests/qunit folder.

 
public static function registerQUnitTests( array &$testModules, ResourceLoader &$resourceLoader ){
	$testModules['qunit']['ext.semanticMediaWiki'] = array(
		'scripts' => array(
			'tests/qunit/ext.smw.test.js',
...

Ensuring a continuous integration for JavaScript and jQuery, MediaWiki provides a JavaScript unit testing environment with 1.19 using the QUnit testing suite. A how-to for SMW specific task has yet to be decided but general information can be found at SMW JavaScript Testing with QUnit.

Best practices[edit]

[a.7] http://qunitjs.com/cookbook/

[a.8] http://benalman.com/talks/unit-testing-qunit.html

Namespace[edit]

Try to avoid polluting the global $/window namespace [a.2, a.3, a.4] and instead use smw/srf.

[a.2] https://gist.github.com/hallettj/64478

[a.3] http://yuiblog.com/blog/2007/06/12/module-pattern/

[a.4] http://www.javascripttoolbox.com/bestpractices/#namespace

Module pattern[edit]

See also[edit]

  • Firebug console debugger, sometimes it is just enough to use console.profile(); or console.log(); to find miscellaneous software code (a sum-up can be found here).
  • Tutorial on methods to increase jQuery readability and performance