Page MenuHomePhabricator

Run QUnit tests for gadgets and user scripts during edit preview
Open, Needs TriagePublic

Description

T39230 proposes to have a standard way to create QUnit tests, and run them from a Special page. While that alone would be an improvement, it would be even better if you could run unit tests when editing a script, before saving the page.

Use cases

As an interface admin, when I am implementing an edit request for a script, I want to run unit tests against the new version of the script before saving so that I don't break existing functionality.

As a userscript author, given that a script in my userspace has a testcase that fails, when I am editing that script, I want to run the unit tests against the new version of the script before saving so that I can verify the changes fix the failing testcase (and do not cause any other tests to fail).

Concept

An extension that provides unit testing capability for gadgets/scripts, using QUnit. Unit tests are defined on a script's /testcases.js subpage.
Tests are run when editing, after previewing or showing changes (action=submit) -- or perhaps a new button "Run unit tests".
To run tests, load QUnit js and css, concatenate the editor textbox content and the content of the testcases subpages (so they are within the same outer scope), and execute the resulting text as javascript.

There may need to restrictions on where this can operate for security:

  • MediaWiki namespace should be okay - editing is restricted to interface admins
  • contentmodel:javascript pages in your userspace should be okay - editing is restricted to yourself and interface admins
  • Elsewhere could be problematic. The safest way would be to just not operate on these pages... but perhaps just having a confirmation box with a "scary" message (similar to enwiki's MediaWiki:Jswarning) would be good enough, given that it's not really any worse than using importScript on your common.js
  • Perhaps there should also be a note or warning that code entered into the textbox will be executed to run unit tests

Other considerations:

  • The interface should show appropriate messages when viewing scripts and testcases pages: "This script appears to have unit tests at [...link...]", "Unit tests for this script can be created at [...link...]", "These are the unit tests for [...link...]"
  • There should be a way to indicate ResourceLoader dependencies needed for unit tests, perhaps a comment like /* dependencies mediawiki.api,mediawiki.util,oojs-ui-core,oojs-ui-windows */ at the top of the testcases page.

A userscript version of this concept exists at https://en.wikipedia.org/wiki/User:Evad37/WikiUnit.js , see also discussion at Wikipedia:Interface administrators' noticeboard#Gadget and userscript unit testing.