{"id":775,"date":"2013-08-09T18:24:43","date_gmt":"2013-08-09T15:24:43","guid":{"rendered":"http:\/\/laxstrom.name\/blag\/?p=775"},"modified":"2013-08-09T18:24:43","modified_gmt":"2013-08-09T15:24:43","slug":"first-qunit-test-for-translate-extension-with-tutorial","status":"publish","type":"post","link":"https:\/\/laxstrom.name\/blag\/2013\/08\/09\/first-qunit-test-for-translate-extension-with-tutorial\/","title":{"rendered":"First QUnit test for Translate extension \u2013 with tutorial"},"content":{"rendered":"<p>It&#8217;s about time the Translate extension gets QUnit tests: the amount of JavaScript in it has exploded in the past year. Here is a quick intro on how to add QUnit tests for a MediaWiki extension which doesn&#8217;t have any yet.<\/p>\n<p><strong>Step 1: Create a <code>tests<\/code> directory.<\/strong><\/p>\n<p>The Translate extension already has a <code>tests\/<\/code> directory with a lot of PHPUnit tests . For now I just created a <code>qunit<\/code> subdirectory under it.<\/p>\n<p><strong>Step 1: Create a test file.<\/strong><\/p>\n<p>The function I want to test is in a file at<br \/>\n<code>resources\/js\/ext.translate.parsers.js<\/code>.<br \/>\nI created a corresponding test file<br \/>\n<code>tests\/qunit\/ext.translate.parsers.test.js<\/code>.<\/p>\n<p><strong>Step 3: Register the test file.<\/strong><\/p>\n<p>In Translate, all the resource loader modules are defined in <code>Resources.php<\/code>. At the bottom of the file I register the test modules via the <code>ResourceLoaderTestModules<\/code> hook with an anonymous function.<\/p>\n<pre>$wgHooks['ResourceLoaderTestModules'][] =\r\n\t\/\/ Dependencies must be arrays here\r\n\tfunction ( array &amp;$modules ) use ( $resourcePaths ) {\r\n\t\t$modules['qunit']['ext.translate.parsers.test'] = array(\r\n\t\t\t'scripts' =&gt; array( 'tests\/qunit\/ext.translate.parsers.test.js' ),\r\n\t\t\t'dependencies' =&gt; array( 'ext.translate.parsers' ),\r\n\t\t) + $resourcePaths;\r\n\r\n\t\treturn true;\r\n\t};<\/pre>\n<p>The <code>$resourcePaths<\/code> I have defined already earlier:<\/p>\n<pre>$resourcePaths = array(\r\n\t'localBasePath' =&gt; __DIR__,\r\n\t'remoteExtPath' =&gt; 'Translate'\r\n);<\/pre>\n<p><strong>Step 4: Write the tests<\/strong><\/p>\n<p>Here is a simple example with only one test. Note how the assert is taken via function parameter to avoid using global functions.<\/p>\n<pre>\/**\r\n * Tests for ext.translate.parsers.js.\r\n *\r\n * @file\r\n * @licence GPL-2.0+\r\n *\/\r\n\r\n( function ( $, mw ) {\r\n\t'use strict';\r\n\r\n\tQUnit.module( 'ext.translate.parsers', QUnit.newMwEnvironment() );\r\n\r\n\tQUnit.test( '-- External links', 1, function ( assert ) {\r\n\t\tassert.strictEqual(\r\n\t\t\t'This page is [in English]',\r\n\t\t\tmw.translate.formatMessageGently( 'This page is [in English]' ),\r\n\t\t\t'Brackets without protocol doesn\\'t make a link'\r\n\t\t);\r\n\t} );\r\n\r\n}( jQuery, mediaWiki ) );<\/pre>\n<p><strong>Step 5: Run the tests<\/strong><\/p>\n<p>I ran the tests in <a href=\"https:\/\/dev.translatewiki.net\/wiki\/Special:JavaScriptTest\/qunit?module=ext.translate.parsers\">my development wiki<\/a>\u00a0and they passed. <a href=\"https:\/\/gerrit.wikimedia.org\/r\/69634\">The patch set<\/a> is in Gerrit. Also see the <a href=\"https:\/\/www.mediawiki.org\/wiki\/QUnit\">QUnit page in mediawiki.org<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s about time the Translate extension gets QUnit tests: the amount of JavaScript in it has exploded in the past year. Here is a quick intro on how to add QUnit tests for a MediaWiki extension which doesn&#8217;t have any yet. Step 1: Create a tests directory. The Translate extension already has a tests\/ directory [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,2],"tags":[76,25],"class_list":["post-775","post","type-post","status-publish","format-standard","hentry","category-english","category-mediawiki","tag-qunit","tag-translate"],"_links":{"self":[{"href":"https:\/\/laxstrom.name\/blag\/wp-json\/wp\/v2\/posts\/775","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/laxstrom.name\/blag\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/laxstrom.name\/blag\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/laxstrom.name\/blag\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/laxstrom.name\/blag\/wp-json\/wp\/v2\/comments?post=775"}],"version-history":[{"count":15,"href":"https:\/\/laxstrom.name\/blag\/wp-json\/wp\/v2\/posts\/775\/revisions"}],"predecessor-version":[{"id":792,"href":"https:\/\/laxstrom.name\/blag\/wp-json\/wp\/v2\/posts\/775\/revisions\/792"}],"wp:attachment":[{"href":"https:\/\/laxstrom.name\/blag\/wp-json\/wp\/v2\/media?parent=775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/laxstrom.name\/blag\/wp-json\/wp\/v2\/categories?post=775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/laxstrom.name\/blag\/wp-json\/wp\/v2\/tags?post=775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}