TV's Frank
TV's Frank

Reputation: 818

How do I run javascript unit tests in maven, with no need for running a browser or server?

I work with developing web stuff that contains javascript that to some extent holds more logic than just animations and moving values around. I would like to implement some unit tests for that kind of logic.

I want to find some way to write these tests in javascript, and have them run whenever I build the web project in maven. Since the code I want to test deals with logical stuff contained in methods or objects, I don't feel I should have to have a server running. Also, I've read about stuff like Rhino, which makes me feel like there should be no need to have a browser starting somewhere just to execute the javascript.

I'm not particularly concerned about browser differences - I rarely find I run into problems in that field, and when I do it's always about styling or rare DOM issues. I want to test that I can add one and one and end up with two.

I've googled around some, and I find many frameworks for unit testing of javascript. Also after filtering out my picky demands I can still find a few products. Those things tend to lead me to dead web pages though. I found http://code.google.com/p/javascript-test-maven-plugin/ which seems nice, but it's still in a beta version, and I'm struggling to have my maven locate the repository.

Does anyone out there have some recommendations or hints?

One reason for looking at unit tests for javascript is that the language seems perfect for it. Having worked with mocking frameworks in Java, I often find I end up with pseudo-closure stuff and notations that would just disappear if I did the same thing in javascript - you hardly need a testing framework at all besides imposing structure and integrating with maven's test phase and Jenkins.

Upvotes: 8

Views: 6438

Answers (2)

Bart
Bart

Reputation: 17361

I was looking for the same thing.

I've never used it (just started to look into javascript unit testing) but Jasmine seems to be very popular.

https://github.com/jasmine/jasmine

So naturally there is a maven plugin for it.

https://github.com/searls/jasmine-maven-plugin

Upvotes: 8

Stephan
Stephan

Reputation: 43013

Here is an alternative : junit-js

It is a plain JUnit runner taking unit tests written in javascript and running them with Nashorn or Rhino.

There's also a patch available for the Eclipse Junit plugin since this plugin "expects all test classes to be java classes."

How to use?

  • Add the following dependencies to your project:
<dependency>
    <groupId>com.belteshazzar</groupId>
    <artifactId>junit-js</artifactId>
    <version>1.0.0</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
</dependency>
  • Write your test (in javascript)
function testDummy01() {
    print("testDummy01");
    var x = "5";
    assertEquals("x should be 5", 5, x);
}

function testDummy02() {
    print("testDummy02");
    fail("not implemented");
}
  • Finally write a test suite
import org.junit.runner.RunWith;
import uk.co.benjiweber.junitjs.JSRunner;
import uk.co.benjiweber.junitjs.Tests;

@RunWith(JSRunner.class)
@Tests({
    "/uk/co/benjiweber/junitjs/examples/DummyJSTest.js",
    "SimpleJSTest.js",
    "/uk/co/benjiweber/junitjs/other/FredsJSTest.js",
    "../other/FredsJSTest2.js",
    "sub/SimpleJSTest.js"
})
public class ExampleTestSuite    {}

References:

Upvotes: 0

Related Questions