ZenMaster
ZenMaster

Reputation: 12746

Testing using JsTestDriver + RequireJS + QUnit

After much searching and quite a bit of trial it seems to me that the chosen combination of tools just doesn't have it. I would LOVE to be mistaken.

Well, the technological stack is as mentioned in the title. To expand,

  1. Backbone for all "classes"
  2. RequireJS to load and manage dependencies between Backbone entities
  3. QUnit (where tests are also RequireJS modules)
  4. JsTestDriver for command line automation

Setup and code snippets

jsTestDriver.conf

server: http://localhost:48080
basepath: path/to/JSTestDriver/
load:
  - lib/qunit/qunit-1.10.0.js
  - lib/qunit/equiv.js
  - lib/qunit/QUnitAdapter.js
  - lib/requirejs/require.js
test:
  - test/tests.js
serve:
  - lib/jquery/jquery-1.7.1.js
  - test/components/ComponentOneTest.js

tests.js

require({  
    baseUrl : '/test',
    shim : {
        'fixture.object' : ['jquery'],
        'fixture.string' : ['jquery'],
        'fixture.dom' : ['jquery', 'fixture.string'],
        'fixtures' : ['fixture.object', 'fixture.string', 'fixture.dom'],
        'equiv' : ['qunit'],
        'qunit.adapter' : ['qunit', 'equiv']
    },
    paths: {  
        'text' : 'lib/requirejs/text',
        'jquery' : 'lib/jquery/jquery-1.7.1',
        'backbone' : 'lib/backbone/amd/backbone',
        'underscore' : 'lib/underscore/amd/underscore',
        'fixture.dom' : 'lib/fixture/jquery.dom.fixture',
        'fixture.string' : 'lib/fixture/jquery.lang.string',
        'fixture.object' : 'lib/fixture/jquery.lang.object',
        'fixtures' : 'fixture/fixtures',
        'qunit' : 'lib/qunit/qunit-1.10.0',
        'equiv' : 'lib/qunit/equiv',
        'qunit.adapter' : 'lib/qunit/QUnitAdapter'
    }
}, [], function() {
    module('Module 1', {});

    test('test 1', 1, function() {
        ok(true, 'passed');
    });

    asyncTest('test 2', 1, function() {
        start();
        ok(true, 'passed');

    });
});

Server starting command (from the same directory JSTD JAR is in)

java -jar JsTestDriver-1.3.4.b.jar --port 48080

Tests running command (from the same directory JSTD JAR is in)

java -jar JsTestDriver-1.3.4.b.jar --runnerMode PROFILE --reset --dryRunFor all --tests all

What works?

The first, synchronous, test does.

What doesn't work?

The second, asynchronous test doesn't work and times out.

The error

Chrome console

Uncaught TypeError: Cannot read property 'all' of undefined qunit-1.10.0.js:1102
done qunit-1.10.0.js:1102
process qunit-1.10.0.js:1285
(anonymous function) qunit-1.10.0.js:383

Console

setting runnermode PROFILE
Chrome: Reset
Chrome: Reset
Chrome 22.0.1229.64: 1 tests [
Module 1 (/test/test/tests.js)
        test test 1
        test test 2]
.F
Total 2 tests (Passed: 1; Fails: 1; Errors: 0) (30025.00 ms)
  Chrome 22.0.1229.64 Windows: Run 2 tests (Passed: 1; Fails: 1; Errors 0) (30025.00 ms)
    Module 1.test test 2 failed (30023.00 ms): Error: Callback '#1' expired after 30000 ms during test step 'start()'
      Error: Callback '#1' expired after 30000 ms during test step 'start()'

Sep 23, 2012 8:36:44 PM com.google.jstestdriver.ActionRunner runActions
INFO:

Notes

I did download the latest QUnit adapter from here. I also tried a bunch of other, supposedly working, adapters to no avail.

Question

Is this even possible? If so, would someone be so kind to shed some light on the issue?

Thank you.

UPDATE (08.04.14):

Karma. That is all.

Upvotes: 3

Views: 1849

Answers (1)

Dilip
Dilip

Reputation: 21

In an async test, it does not make sense to put a simple set of statements one after the other to execute. An async test is used mainly if we do not want to stop and start the test while a time taking task is being executed within the test, for example a timeout or an ajax call. To emulate this we can change

asyncTest('test 2', 1, function() {
    start();
    ok(true, 'passed');
});

to

asyncTest('test 2', 1, function() {        
    setTimeout(function(){
        ok(true, 'passed');
        start();
    },2000);    
});

which will work like a charm!!!

Upvotes: 2

Related Questions