admin管理员组文章数量:1289422
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 / 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 remendations 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.
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./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 remendations 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.
Share Improve this question asked May 15, 2013 at 13:45 TV's FrankTV's Frank 8189 silver badges21 bronze badges2 Answers
Reset to default 8I 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./jasmine/jasmine
So naturally there is a maven plugin for it.
https://github./searls/jasmine-maven-plugin
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>.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:
- junit-js original project
- junit-js fork with JUnit eclipse plugin patch
- JavaScript tests with JUnit
本文标签:
版权声明:本文标题:How do I run javascript unit tests in maven, with no need for running a browser or server? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741477625a2380980.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论