User:Evad37/WikiUnit

WikiUnit (source) is a script that runs testcases for other userscripts and gadgets.

Installation

 * For English Wikipedia only: Add the following line to your common.js:
 * For other wikis: Add the following line to your common.js on another wiki, or to your global.js on Meta:

Usage
For scripts which have a /testcases.js subpage, this WikiUnit will run the test on that page when you edit the script and show preview or changes. Those buttons are relabeled "Show preview & tests" and "Show changes & tests". The tests will be run against the unsaved code in the editor textbox.

For running unit tests against the current (saved) code, the script adds a "Run tests" tab on both the script page and the /testcases.js subpage.

WikiUnit works for scripts in both MediaWiki namespace and User namespace. When testcases are in another user's userspace, you will be asked for confirmation before running testcases. Check that the testcases page does not contain nefarious code that could compromise your account before proceeding.

Writing tests
WikiUnit loads QUnit to run unit tests. Tests are defined on a /testcases.js subpage of a javascript page. The /testcases.js script contains s, perhaps grouped into s. See the QUnit cookbook and documentation for more details.

Testcases execute in the same scope as the script they test, and can access any functions or variables declared in the scripts outer scope. Alternatively properties can be added to the `window` object, which can be accessed by both the testcases and the script being tested – but be careful to use unique names that are unlikely to conflict with other scripts.

Gadgets which need to load ResourceLoader dependencies should specify those depenedencies in a comment in the top of the script, like this:

Notices
The following notices are suggested to be placed at (or near) the top of the script and testcases pages, to inform users about the testcases and how to run them.

For the script:
 * (For gadgets, this should placed in the initial comment, under the GLOBAL GADGET FILE warning box)

For the /testcases.js subpage:

Examples

 * User:Evad37/extra/sandbox.js/testcases.js