Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 609
TV's Frank Avatar asked May 15 '13 13:05

TV's Frank


People also ask

Which command of Maven is used to run all unit tests?

The Maven surefire plugin is easy to use. It has only one goal: test. Therefore, with the default configuration, we can execute all tests in the project by the command mvn test.

Can we write unit test for JavaScript?

JavaScript Unit Testing is a method where JavaScript test code is written for a web page or web application module. It is then combined with HTML as an inline event handler and executed in the browser to test if all functionalities are working as desired. These unit tests are then organized in the test suite.

How do you run test cases in Jhipster?

Those tests can be run directly in your IDE, by right-clicking on each test class, or by running ./mvnw clean verify (or ./gradlew test integrationTest if you use Gradle).


1 Answers

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

like image 99
Bart Avatar answered Nov 15 '22 18:11

Bart