Unit testing JavaScript
bbq includes a suite of unit testing tools for JavaScript. Place your unit tests in src/test/javascript - see the project structure page for more.
+- src/
+- test/
+- javascript/
Setup
Declare the bbq-test-maven-plugin in the build section of your pom.xml
<plugin>
<groupId>org.bbqjs</groupId>
<artifactId>bbq-test-maven-plugin</artifactId>
<version>${currentVersion}</version>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>javascript/libs/unittest-1.8.0.js</include>
</includes>
</configuration>
</plugin>
bbq supports headless JavaScript unit tests intended to be run as part of your CI build.
Including libraries in the test environment
You can include arbitrary JavaScript files and/or libraries in your tests using the configuration > includes > include elements.
N.B. you must include a unit test framework.
An example test
By default bbq uses the unit testing framework supplied with Scriptaculous. A sample test would look like:
include(com.myapp.MyClass);
test = new Test.Unit.Runner({
subject: null,
setup: function() {
with (this) {
this.subject = new com.myapp.MyClass();
}
},
teardown: function() {
with (this) {
}
},
testSomething: function() {
with (this) {
var expected = "foo";
this.assertEqual(expected, this.subject.bar());
}
}
});
Tests are run headlessly under Mozilla Rhino and include a pretty good DOM implementation courtesty of env.js.
Each test file is compiled before it is run so you you can use
include()
in your tests in the same way as your main JavaScript classes.
Running individual tests
To run an individual test, specify the fully qualified test class name as the bbq.test environmental parameter. ie.
mvn bbq-test:test -Dbbq.test=com.myapp.MyClassTest
Mocking the server
See the bbq.ajax.MockAJAXRequest JSDoc page for details on how to mock the server receiving AJAX requests in your unit tests.
Using your own test runner
To run with bbq-js a JavaScript unit test file should have a global variable named "test" which has a method named "summary" This method should return an object like this:
{
// The number of tests run
tests: 0,
// How many failed
failed: 0,
// How many errors occured
errors: 0,
// Any error messages
messages: [
"A list of error messages",
"Another error message"
]
}
This is the format supported by Scriptaculous' test runner. If you wish to use something different it should follow the rules above.