Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

JSUnit

From MoodleDocs

Note: This page is a work-in-progress. Feedback and suggested improvements are welcome. Please join the discussion on moodle.org or use the page comments.


Admin tool to wrap Javascript unit tests execution. Is based on YUI test module, taking profit of the dependencies system to avoid core modifications.

Test cases can be created like any other YUI 3 module adding a module in the yui/ folder (More info in https://docs.moodle.org/dev/How_to_create_a_YUI_3_module) Tests should include it's dependencies like any other YUI module.

Usage

  • To execute the tests from the Moodle user interface
    • The link is in the Administration tree -> Development -> JSUnit
  • To execute the tests from a CLI environment
    • A server-face Javascript tool like Yeti is needed (the PHP CLI tool not parses Javascript)
    • Install Node.js + Yeti
    • Execute "/usr/bin/php admin/tool/jsunit/jsunit_cli.php > jsunit_tests.html" from moodle root to get a file with all the JS code to test
    • Execute "yeti jsunit_tests.html" to run the tests

Creating tests

To create a new YUI 3 test module:

  • If the plugintype key is $1 and the pluginname is $2 (for example, for block_community, $1 = block, $2 = community
  • The module folder and JS file must end with 'test', for example 'commentstest', let's call that name $3
  • The name of the module must be 'moodle-$1_$2-$3'
  • The external name of the class must be 'M.$1_$2.init_$3'
  • The module must include it's dependencies with other modules along with a 'test' module dependency

The block_community comments module example can be followed, it's a simple unit test to show how to write them.

Base skeleton, replace 'myblock', 'myyuimodule' and 'MYYUIMODULE' for your block name and your yui module name YUI.add('moodle-block_myblock-myyuimoduletest', function(Y) {

   var MYYUIMODULETESTNAME = 'blocks_myblock_myyuimoduletest';
   var MYYUIMODULETEST = function() {
       MYYUIMODULETEST.superclass.constructor.apply(this);
   };
   Y.extend(MYYUIMODULETEST, Y.Base, {
       initializer : function(params) {
           M.block_myblock.init_myyuimoduletest = new Y.Test.TestCase({
               // Init & destroy
               name: "My yui module unit test",
               setUp : function () {
                   this.data = {};   // Test data, mock DOM elements creation...
               },
               tearDown : function () {
                   delete this.data; // Restoring initial state
               },
               // Tests
               test_one : function () {
                   // Test code {@link http://yuilibrary.com/yui/docs/test/#assertions}
               },
               test_two: function () {
                   // Test code {@link http://yuilibrary.com/yui/docs/test/#assertions}
               }
           });
       }
   }, {
       NAME : MYYUIMODULETESTNAME,
       ATTRS : { }
   });
   M.block_myblock = M.block_myblock || {};
   M.block_myblock.init_myyuimoduletest = function(params) {
       return new MYYUIMODULETEST(params);
   };

}, '@VERSION@', {requires:['test', 'moodle-block_myblock-myyuimodule']});

Links