Finding a javascript testing feature set to be used with the Nashorn jjs interpreter
Nashorn jjs interpreter allows you to do many complex tasks like building web servers, database operations, and swing / javafx interfaces. Most of the benefits of this approach are quick experimentation and being able to use any java library you could think of.
I am using Nashorn in pure javascript mode i.e.
- I am exclusively using the provided jjs translator from Nashorn.
- I am not calling the translator Nashorn from a java program
- I am not using avatars or the equivalent nodejs overlay
Things are good. However, I can't seem to make standard javascript testing unit modules to work with Nashorn jjs.
I looked at jasmine, qunit, mocha and many other frameworks with no valid result. I even tried to make java junit work with pure jjs scripts.
Many of them have js test runners. I found a request running on a web client and this is not in my scope.
I want to be able to run true agnostic javascript testuite with the Nashorn jjs interpreter in pure js mode, not java mode.
Is there such a tool, and if so, how can it be used with Nashorn jjs?
Update:
Following Sirko's answer, I was able to map the expected behavior to these two code snippets (warning: Nashorn 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 them with pure jjs will get the following output:
> 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
source to share
This is an excerpt from my code that I used a while ago to get QUnit to return custom output from test sessions:
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 these functions / callbacks / event listeners, I have customized my custom output from the QUnit tests. The actual test was added as follows:
// start module
QUnit.module( 'myModuleName' );
// some tests
QUnit.test( 'some test', function( assert ) { } );
// execute
QUnit.load();
This code is quite old, so QUnit may provide an easier way to do this, but it worked for me.
source to share