Search code examples
javascriptunit-testinggithubnode.js-tape

How to set up JavaScript github test harness using tape & testling


I have a (JavaScript) repository on github which I would like to set up a test harness for.

It seems ‘tape’ and ‘testling’ should do what I want (minimal lightweight solution with pretty github badges), unless anyone has alternative solutions, but I’m struggling on how to set things up.

I've never used node.js, and it seems that tape, and testling (and all associated examples) are focused on node.js.

Can anyone advise on a minimal configuration for a simple client-side JavaScript library test harness? I guess(?) I need nodejs, npm, package.json, etc to run tape/testling locally, but I’m not clear how to incorporate my (non-node) JavaScript into the test harness.

What I currently have is along the lines of:

myobj.js:

var MyObj = {};
MyObj.a = function() { return 'a'; }
MyObj.b = function() { return 'b'; }

myobj-sub.js:

MyObj.Sub = {};
MyObj.Sub.x = function() { return 'x'; }
MyObj.Sub.y = function() { return 'y'; }

These of course normally get pulled into the HTML using

<script src="myobj.js"></script>
<script src="myobj-sub.js"></script>

For the test harness, I guess I will need something like:

var test = require('tape');
test('myobj', function(assert) {
    assert.equal('a', MyObj.a(), 'test a');
    assert.equal('x', MyObj.Sub.x(), 'test x');
    assert.end();
});

But I’m not sure where to fill in the gaps!

(ps: I’m running on Linux, if it makes any difference).

Thx.


Solution

  • There aren't too many gaps in your question. What you need to do:

    1. Install Node.js (this will also install the npm command line tool). The approach here is distribution-specific, on a Debian-based Linux distribution you would do:
      sudo apt-get install nodejs
      
    2. Set up the directory structure for your project:
      myproject/
      |- package.json
      |- myobj.js
      |- myobj-sub.js
      |- test/
          |- myobj.js
      
      You can specify lots of different parameters in package.json, the only important part however is devDependencies to make sure the test harness is installed:
      {
        "name": "myproj",
        "version": "1.0.0",
        "description": "Just some test project",
        "devDependencies": {
          "tape" : "~2.13.3"
        },
        "scripts": {
          "test": "node test/*.js"
        }
      }
      
    3. Install dependencies. Now that they are specified in package.json this is simply a matter of running from the project directory:
      npm install
      
      This downloads all dependencies and places them into the node_modules subdirectory inside your project directory.
    4. Run tests:
      npm run-script test
      

    That's it, the tests are running. The only problem is that they are failing because your script files don't use the CommonJS format. Here is what myobj.js should look like:

    exports.a = function() { return 'a'; }
    exports.b = function() { return 'b'; }
    

    And here is myobj-sub.js:

    exports.x = function() { return 'x'; }
    exports.y = function() { return 'y'; }
    

    And finally test/myobj.js:

    var test = require('tape');
    var MyObj = require('../myobj');
    MyObj.Sub = require('../myobj-sub');
    test('myobj', function(assert) {
        assert.equal('a', MyObj.a(), 'test a');
        assert.equal('x', MyObj.Sub.x(), 'test x');
        assert.end();
    });
    

    And if you re-run the tests now they succeed.

    What if you want to use your CommonJS modules from a webpage? You can use browserify for that. Install browserify:

    sudo npm install -g browserify
    

    And run it to produce a bundle:

    browserify myobj.js myobj-sub.js -o bundle.js
    

    bundle.js is now a script file that can be included by a webpage - and then the webpage will be able to do var MyObj = require('myobj'); just like the test did.