Search code examples
javascriptunit-testingnashorn

Looking for a javascript unit-testing suite to be used with Nashorn's jjs interpreter


Nashorn's jjs interpreter allows to perform many complex tasks like creating web servers, database operations and swing/javafx interfaces. Most benefits of such approach are the quick experimentations and ability to use any java library you could think of.

I'm using Nashorn in pure javascript mode, ie :

  • I'm using exclusively the provided jjs interpreter from Nashorn.
  • I'm not calling Nashorn interpreter from a java program
  • I'm not using avatarjs or a nodejs equivalent overlay

All is well. Yet, I'm unable to make standard javascript unit-testing suites work with Nashorn's jjs.

I've looked at jasmine, qunit, mocha and many others frameworks with no valid result.. I even tried to make java junit work with pure jjs scripts.

Many of these have js test runners I found request running on a web client, and this is out of my scope.

I wish to be able to run a true agnostic javascript testsuite with Nashorn jjs interpreter in pure js mode, not in java mode.

Is there such a tool and if so how can it be used with Nashorn's jjs?


Update:

Following up on Sirko's answer, I managed to mimick the expected behavior with these 2 code snippets (warning: Nashorn's specifics inside)

qunit-nashorn.js:

load("qunit-1.18.0.js");

with(QUnit) {
  init();
  log(function(d) {
    if (!d.result) {
      var message = d.name + "\tFAIL" + d.source;
      message += " actual: " + d.actual + " <> expected: " + d.expected;
      print(message);
    }
  });
  done(function(d) {
    print("time:\t",d.runtime,"ms");
    print("total:\t",d.total);
    print("passed:\t",d.passed);
    print("failed:\t",d.failed);
  });
}

qunit_poc.js:

load("qunit-nashorn.js");

with(QUnit) {
  test("test1", function(a) { a.equal(true,true); });
  test("test2", function(a) { a.equal(false,true); });
}

QUnit.load();

And running these using pure jjs gives the following result:

> jjs qunit_poc.js

test2   FAIL    at <anonymous> (qunit_poc.js:5) actual: false <> expected: true
time:    355 ms
total:   2
passed:  1
failed:  1

Solution

  • This is an excerpt from some code of mine, which I used a while ago to have QUnit return custom output from the testruns:

    QUnit.init();
    
    // (failed) tests
    QUnit.log( function(details) {} );
    
    // module start
    QUnit.moduleStart( function( details ){} );
    
    // module summary
    QUnit.moduleDone( function( details ){} );
    
    // test begin
    QUnit.testStart( function( details ){} );
    
    // test end
    QUnit.testDone( function( details ){} );
    
    // finished all testing
    QUnit.done( function(){} );
    

    Using those functions/callbacks/event listeners I set up my custom output from the QUnit tests. The actual test were than added like this:

    // start module
    QUnit.module( 'myModuleName' );
    
    // some tests
    QUnit.test( 'some test', function( assert ) { } );
    
    // execute
    QUnit.load();
    

    This code is rather old, so QUnit might be providing an easier way to do this, but this used to work for me.