I\'m wondering how I seamlessly include a JavaScript unit testing framework into
ID: 647179 • Letter: I
Question
I'm wondering how I seamlessly include a JavaScript unit testing framework into my workflow.
The context is, we have an existing data processing algorithm + webapp to display it, with no automated testing or continuous integration.
We use Eclipse as our IDE.
For the webapp JavaScript modification, it would be nice to add some unit tests, and qUnit looks straightforward, plenty of community support etc.
Here's how I'm currently thinking the workflow would go:
Branch the JavaScript code, check it out, modify it. Refactor it to make it more testable if needed.
Write qUnit unit tests in accordance to the above tutorial.
Start Tomcat in eclipse, run the qUnit test page.
Open the browser, check that all the tests have passed.
Commit the code back to the branch.
Merge the branch back into to trunk.
At this point there might have been a merge conflict, and so unit tests would need to be run again for the trunk to check that they're still working.
The issue here, it still seems a little cumbersome to be running the unit tests, several clicks involved to run and check the results.
Now with the merge back to trunk issue, clearly a continuous integration solution like Jenkins would be good here, but given that that's out of the question for now, let's just focus on how we can make it easy to run the tests as a developer.
Do you think I'm missing something here, or is what I'm proposing reasonable sensible?
Explanation / Answer
Ideally, the workflow should be fully automated end-to-end
Use a tool like Karma. Run them minimally in PhantomJS to catch the logic errors, and ideally with every supported browser. Integrate it directly in your build. All you should need to test your app should be something like
mvn verify
(in this case if you use maven. If you use say Ant, it should be integrated in your Ant build, if you use Grunt... you get the point).
Nothing more (and I mean it). If running the test is even slightly an hassle, you will stop running them, which is bad. If you need to click, you can't run them with your CI server, which is bad. If its not part of your build, you can forget running them, which is bad.
I highly recommend that you have a IDE agnostic build system. If not, it's time you check that out, before anything else. Then integrate the testing in your build. Failing test should fail the build in an automatic matter, without any intervention. In a perfect world, it should be impossible to forget to run the test.
So in conclusion : check karma and phantomjs. This is what will help you the most. But I insist on going the extra mile : add a complete build script that check the validity of the code in one single command. And while we're at it, do you validate your javascript with a tools like JSHint? If not, check that out too (and put it in your build :) ). It can catch many mistakes.