The situation is this.
I’m a junior developer for a small government IT project. We don’t have anything of continuous integration or a automated testing framework. But the idea is to be developing this kind of thing/improving our developing environment.
The product is a data processing algorithm that then displays information to the end user via a webapp.
So coming up is a bunch of work modifying the webapp javascript. As such we want to implement a unit testing for the javascript.
I’ve looked at this stack overflow question which outlines several different unit testing frameworks for javascript.
The question is – how do I decide between which framework to start using?
- What is a list of criteria for choosing a testing framework?
- Is it practical to try them all out?
- Spend a lot of time reading forums/etc about how they’re used?
What is a list of criteria for choosing a testing framework?
1) Syntax
As you investigate different test frameworks you will notice that syntax is a differentiating factor across all of them. This is really going to depend on what makes you feel comfortable.
For example, qUnit is more of a declarative test framework. Its API consists of functions called test, equals, strictEqual, deepEqual, etc.
Another test framework, mocha, is more like rspec in that it reads in a more sentence like structure. Its API consists of functions called describe, it, assert.equal, etc.
My personal preference is a qunit like structure.
2) Features – Stubs, Spies, Fake server, Fake clock, etc
When you start testing in Javascript you will quickly find yourself in situations where you need your test framework to help you test certain use cases. For example:
Spies – This will help you detect whether your code called a function when it should have
Stubs – Very similar to a spy except it contains predefined behavior (Example: When the stub is called, return foo)
Fake Server – If your code issues AJAX calls and you want to fake the response of the AJAX call, a Fake Server will help you more easily test.
Fake Clock – If your code reacts differently based on the date or time, then a fake clock will help you control exactly what time it is while your test runs.
My recommendation for a Javascript Framework that supports all the features above is Sinon.JS. Because I like a qunit like syntax and framework, I use qunit AND Sinon.JS. qunit handles my assertions and runs my tests. Sinon.JS gives me the ability to stub, spy, run a fake server, and fake the clock.
3) Community Support
Is your test framework supported by the community? For example, grunt (a build environment tool) supports running qunit in a headless PhantomJS server: https://github.com/gruntjs/grunt-contrib-qunit . This makes it easy to write tests and have them be automatically run everytime I save the test file.
Is it practical to try them all out?
No it is not practical. Instead investigate each framework’s website and pick one that you feel comfortable with and one that will also support you while your write tests.
Spend a lot of time reading forums/etc about how they’re used?
Grab a book on the framework you choose and skim through it as you need it.