Jasmine is a nice testing framework that allows us to write tests in a BDD manner.
Let's see how to use it in our project.
Apart from executing tests within build, we can run them during development phase.
The maven goal jasmine:bdd starts the Jetty server and under http://localhost:8234 URL we can see the results of executing test fixtures.
Reloading the page will execute the latest version of our code.
A test begins with a call to the global Jasmine function describe.
Then the specific test cases are defined by calling function it.
We're testing the sunny day scenario and border case that throws the exception.
At the beginning as a sanity check we can test if the function is defined.
Jasmine has a lot of advanced features, we can even use mocks, expectations and argument matchers.
Nothing stops us from writing tests first and going through red-green-refactor cycle.
Let's write a function that will count the number of specific elements on our page.
The behaviour of our component is defined by following spec:
Before testing the case with one element, we're adding it to the document. In more complex case we could load the HTML content.
After executing mvn test we'll see a nice output:
Whole project can be found at github.