cherouvim
cherouvim

Reputation: 31903

automating javascript testing in a maven based project

I have a maven based Java project which also contains some javascript functions which I'd like to be able to test. I'm using QUnit (but could switch to another js testing framework) to write the tests, but I have to execute the index.html to see the tests running in a browser window.

Is there a current and working maven plugin that will run those tests for me and ideally present the results together with the regular server side junit tests that I already have?

I've already tried to use what is currently out there but most of the solutions are old and with broken documentation. The closest I could get was by using https://github.com/mxro/java-qunit but it runs all js tests in a single junit test and it stops on the first failure.

Upvotes: 4

Views: 1000

Answers (1)

Stanislav Bashkyrtsev
Stanislav Bashkyrtsev

Reputation: 15308

Running JS Tests from Maven

There is Frontend Maven Plugin that can invoke npm commands. Here is an example of project that uses it - this is a demo-project that shows different types of tests, so its configuration is a bit more complicated than usually. This is how you install packages:

    <plugin>
      <groupId>com.github.eirslett</groupId>
      <artifactId>frontend-maven-plugin</artifactId>
      <version>0.0.24</version>
      <executions>
        <execution>
          <id>Install NodeJS and NPM</id>
          <goals>
            <goal>install-node-and-npm</goal>
            <goal>npm</goal>
          </goals>
          <configuration>
            <nodeVersion>v0.12.7</nodeVersion>
            <npmVersion>2.11.3</npmVersion>
          </configuration>
        </execution>
      </executions>
    </plugin>

Running Karma tests:

        <execution>
          <id>UI Unit Tests</id>
          <goals>
            <goal>karma</goal>
          </goals>
          <configuration>
            <karmaConfPath>${project.basedir}/src/test/unit-js/karma.conf.js</karmaConfPath>
            <skip>${unit.ui.tests.skip}</skip>
          </configuration>
        </execution>

Running Protractor tests (via npm scripts):

        <execution>
          <id>UI Component Tests</id>
          <goals>
            <goal>npm</goal>
          </goals>
          <phase>test</phase>
          <configuration>
            <arguments>run component</arguments>
            <skip>${component.ui.tests.skip}</skip>
          </configuration>
        </execution>

Combined Report

The reporting itself is not a part of this plugin. It's your JS frameworks (like Karma, Jasmine) that are responsible for report generation. You can either use xUnit format and let CI (like Jenkins) generate single report out of that. Or use Allure tool that has adaptors for many languages and frameworks (it's relatively easy to write your own if your framework doesn't have the adaptor).

Upvotes: 3

Related Questions