Using qunit from grunt
12 Oct 2013When poking around in the jquery and jquery ui codebases I noticed an extensive use of qunit from grunt. I get it that the jquery guys also did qunit - so this makes sense. But why grunt? I remember the earlier versions just used makefiles.
Grunt and npm replaces make, autoconf and apt for javascript projects
I remember Makefiles as simple - just as long as you got the use of tabs right and kept them simple. It was just “the other tools” that made the experience bad for me. Remember automake and m4 scripting? Not to mention cmake. Tools like Make or ant are simple tools in themselves - they started getting complicated when the tradition to use associated tools arose - like Makefiles tend to assume that dependencies are handled using automake and m4 scripts and ant buildfiles retrieve dependencies with tools like ivy. When I consider the complexity I know from existing build toolchains on linux, then grunt starts to look simple. The simplicity of grunt comes from how easy it is to combine grunt plugins as compared to how complex the build situation using other tools has become. Using grunt it is now possible to build using only javascript based tools.
Installing grunt and npm.
To be able to use grunt you will need nodejs and npm. You can find a nodejs installer for most platforms at nodejs.org - this will include npm. When you install you should make sure that the “node” and “npm” commands are available on your commandline via your “PATH” environment variable. (For my less commandline-savvy friends there are some detailed instructions for windows 7 here)
You can install grunt globally from the commandline like this:
To take your newly installed grunt for a spin, you could try it out with building jquery-ui. To checkout jquery-ui you could do this:
This should take you through an example of using grunt on an existing project. If all goes well then all the tests should pass and a new jquery-ui build should be avaible to you inside the “dist” folder.
Scaffolding a grunt project that supports qunit
The major strength of grunt is its strong tradition for plugins - but when starting up it can also be a major drawback. you need to setup some plugins to be able to start working. It does not help that the grunt-init command has been separated out in a plugin in version 0.4 (most of the existing blog entries just refers to it as being inside grunt) . See The Project scaffolding section in docs for more information.
To be able to run grunt-init you need to install the grunt-init plugin and grab a working template to start working:
Note that my username is “jacob” and that I am on windows here. You will probably have to insert another directory.
Now I could run “grunt-init gruntfile” and answer a couple of questions:
After hitting Enter three times I got this file “Gruntfile.js”:
To be able to run this Gruntfile you need to install the necessary plugins. If you append “–save-dev” to the installation commands then installation info will inserted into package.json:
This will retrieve the plugins and place them inside the node_modules and add them inside the devDependencies section of package.json . As you might have noticed the Grunt files are a bit verbose but they support easy composition of plugins - e.g. if you would like to another target , using another plugin , then you could do this pretty easily. The grunt files are written in javascript, so if you wish to insert custom logic in the build files, then it should be pretty easy to do so (Without worrying about tabs and spaces ).
Note that once you have created package.json describing your dependencies, then you can simple write “npm install” to install your dependencies. There is no need to store the “node_modules” folder in your version control system.
Qunit replaces junit and phpunit on the frontend.
After installing grunt-contrib-qunit and enabling it your gruntfile you now have the option of writing automated qunit tests that can be run directly from grunt. grunt-contrib-qunit uses phantomjs behind the scenes to enable you run your tests directly from grunt (without opening a browser). This should make it easier to automate your tests.
I think that best way to learn qunit is to look at existing tests. The button core test in jquery ui is a good place to start.
The essential functionality in qunit is :
- ok (truthy [,message])
- assert ( value, expected [,message])
- expect ( number of assertions)
Combining these let’s you write a test like the check for #7534 in jquery ui:
Here we expect 1 assertion to be run.
There are more advanced options in qunit that you can explore - feel free to take a look at the documentation on qunitjs.com - or be inspired by the existing tests in jquery-ui