You would be interested and also excited to know that Karma was created by Vojta
Jína, member of the AngularJS development team at Google. The tool is the result of
his Master's thesis called JavaScript Test Runner, for the Czech Technical University in
Prague.
Installation
Before installing Karma, you need to have the NodeJS already installed. It is a quite
simple process, and you just need to visit their website, download the package, and
proceed with installation.
[ 140 ]
Chapter 7
After this, you can use the Node Package Manager, also known as npm. It will
manage the installation of everything that we need to set up our environment,
as follows:
npm install –g karma
Configuration
The configuration is quite simple. It just requires a file called karma.conf.js,
located in the root application directory. The best way to create it is by following the
configuration wizard through the following command:
karma init
Karma's configurator will be shown, and you just have to follow the instructions in
order to generate your configuration file.

…

Also, it
can group the desired tasks in any sequence, thus creating a workflow.
It works over plugins, and there is a huge ecosystem with thousands of choices just
waiting to be downloaded! We also have the opportunity to create our own plugins
and share them within the community.
The warranty is that all of the required tasks of our workflow will be fulfilled!
Automating the Workflow
Installation
Just like Karma, Grunt requires NodeJS and the Node Package Manager to
be installed. To proceed with the installation, we just need to type in the
following command:
npm install -g grunt-cli grunt
After this, we are ready to configure it!
Configuration
The Gruntfile.js file is the JavaScript file that defines the configuration of each
task, plugin, and workflow. Let's take a look at its basic structure:
module.exports = function (grunt) {
// 1 – Configuring each task
grunt.initConfig({
});
// 2 - Loading the plug-ins
grunt.loadNpmTasks('plugin name');
// 3 - Creating a workflow by grouping tasks together
grunt.registerTask('taskName', ['task1','task2','task3']);
}
Also, it's really recommended that you generate the package.jsonfile file.

This approach feels nice and OOP-y, but has the disadvantage of not letting you expose much
91
92
Chapter 5 Modules
else from your module; it also tends to feel a bit awkward in the Node way of doing things. I
showed it to you here so that you can recognize it for what it is when you see it, but you will
almost never use it in this book or your projects—you will largely stick with the factory model.
npm: The Node Package Manager
Apart from writing your own modules and using those provided by Node.js, you will frequently
use code written by other people in the Node community and published on the Internet.
The most common way this is done today is by using npm, the Node Package Manager. npm is
installed with your node installation (as you saw in Chapter 1, “Getting Started”), and you can
go to the command line and type npm help to verify that it’s still there and working.
To install modules via npm, you use the npm install command. This technique requires only
the name of the module package you want to install.

In addition to all the already-powerful and functional modules that Node
provides for you, there is a huge community further developing modules that you can take
advantage of in your programs, and indeed you can even write your own to give something
back!
One of the cool things about Node is that you don’t really distinguish between modules that
you have produced and modules that you consume from external repositories, such as those
you see later in this chapter via npm, the Node Package Manager. When you write separate
classes and groups of functions in Node, you put them in basically the same format—perhaps
with a bit less dressing and documentation—as modules you download from the Internet
and use. In fact, it usually takes only an extra bit of JSON and maybe a line or two of code to
prepare your code for consumption by others!
Node ships with a large number of built-in modules, all of which are packaged in the node
executable on your system.

;
Name the file hello_world.js. Then run the file by passing the file path as first argument to the Node executable while inside a shell prompt. For example:
$ node hello_world.js Hello World!
You can quit the CLI by typing Ctrl+D or Ctrl+C.
SETTING UP AND USING NODE PACKAGE MANAGER
You can only get so far using the language features and the core functions. That’s why most programming platforms have a system in place that allows you to download, install, and manage third-party modules. In Node, you have Node Package Manager (NPM).
NPM is three things — a third-party package repository, a way to manage packages installed in your computer, and a standard to define dependencies on other packages. NPM provides a public registry service that contains all the packages that programmers publish in NPM.

At the time of this writing, the unminimizedruntime library was 11KB, while the full Handlebars library was
73KB. When targeting mobile devices, such savings can be significant.
You will need Node.js installed on your development or build machine to run precompilation. In Chapter 3,
we created a Node.js server to act as a backend to our application. If you haven’t already installed node.js, you can
download it from http://nodejs.org. Once you have node.js installed, you can use the node package manager
(NPM) to download the handlebars package.
npm install handlebars -g
All templates will need to be stored in an external file, which is passed to the precompiler on the command line.
handlebars<inputfile> -f <outputfile>
This will result in a JavaScript file being created that contains the compiled template, which can then be included
within your application.
Templating with Mustache
Mustache.js is another logic-less templating library, available from http://mustache.github.io.

…

As well as allowing you to run your tests in headless mode
(in other words, without rendering the user interface elements), PhantomJS even has the ability to take screenshots of
the browser at any particular time.
Installation instructions for PhantomJS are available on the project web site under
http://phantomjs.org/download.html, as illustrated in Figure 8-6, where you will be instructed to download the
appropriate installer for your operating system. However, if you are already using Node.js, you can use the Node
Package Manager to install PhantomJS by using the following command:
npm install phantomjs
162
Chapter 8 ■ Testing Your Backbone.js Application
Figure 8-6. PhantomJS web site at http://phantomjs.org
Now that you have PhantomJS installed, you will need to obtain the PhantomJS Runner plug-in for QUnit,
runner.js, from https://github.com/jquery/qunit/tree/master/addons/phantomjs. Unlike other scripts, we have
downloaded throughout this book, runner.js will be executed from the command line.

…

Installing Grunt
If you want to use Grunt, you will need to install Node.js first. If you’ve been following the examples in this book, you
will already have Node.js installed. If you haven’t, go to http://nodejs.org and follow the installation steps for your
operating system.
180
Chapter 9 ■ Using Grunt for Your Build Process
The Grunt command-line interface (CLI) is installed through the Node Package Manager by executing the
following on the command line, using the -g flag to ensure it is installed globally. Note that you will need to execute
this with admin rights; on Mac OS X this means prefixing the command with sudo.
npm install -g grunt-cli
To check that it has successfully installed, run grunt --help from the command line.
Adding Grunt to Existing Projects
Running the grunt command from your terminal will result in an error message stating that the Gruntfile could not be
found.

In fact, you can use the browser-based CoffeeScript compiler yourself, by including this script in a page, marking up any CoffeeScript script tags with the correct type:
<script src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js" type="text/javascript" charset="utf-8"></script> <script type="text/coffeescript"> # Some CoffeeScript </script>
Obviously, in production, you don’t want to be interpreting CoffeeScript at runtime, as it’ll slow things up for your clients. Instead, CoffeeScript offers a Node.js compiler to pre-process CoffeeScript files.
To install it, first make sure you have a working copy of the latest stable version of Node.js and npm (the Node Package Manager). You can then install CoffeeScript with npm:
npm install -g coffee-script
The -g flag is important, as it tells npm to install the coffee-script package globally, rather than locally. Without it, you won’t get the coffee executable.
If you execute the coffee executable without any command line options, it’ll give you the CoffeeScript console, which you can use to quickly execute CoffeeScript statements.

To do this we are going to use
the most popular Node framework out there, Express.2 Express is easy to install and get up and
running fairly quickly, and it has a vibrant development community, which makes it a great
choice for the task at hand.
In a nice, fresh directory, let’s create a new file, app.coffee, that will be the starting point for
our Express backend. Right now, let’s just leave that file blank while we install Express.
Express ships as an NPM,3 Node Package Manager, module. We briefly mentioned NPM in
Chapter 9, “Intro to Node.js.” NPM modules are bundles of code that are packaged up and
distributed and can then be installed, included, and required into Node applications. If you
have Node installed, you should already have NPM installed.
Installing Express is a simple one liner:
> npm install express
You should see output similar to this:
express@2.5.2 .

node.js
With the node.js configuration file, the usage is different. The configuration file assumes you are using the Express/Connect framework for managing resource requests for your app. In your server-side app code, you can use the following to start the server:
var h5bp = require('h5bp'); var app = express.createServer(); app.use(h5bp.server()); app.listen(3000);
This requires you to install the h5bp package using Node Package Manager (NPM) and the express package using the same. The h5bp package has a slew of configurations that will be used when the server is started. If you wish to use only some specific configurations, you can pass them in as options to the server function, as shown in the following code snippet:
app.use(h5bp.server({ server: true, setContentType: true, removeEtag: true });
Google App Engine
Some websites are also served from Google App Engine (http://code.google.com/appengine/), which requires your website's backend to be written in Java, Python, or Go.

The tests also act as specification, and thus documentation, for future needs and changes.
We at AngularJS have found this to be true, and the entire AngularJS codebase has been developed using TDD. For an uncompiled, dynamic language like JavaScript, we strongly believe that having a good set of unit tests will reduce headaches in the future!
So how do we get this awesomeness that is Karma? Well, first ensure that NodeJS is installed on your machine. This comes with NPM (Node Package Manager), which makes it easy to manage and install the thousands of libraries available for NodeJS.
Once you have NodeJS and NPM installed, installing Karma is as easy as running:
sudo npm install -g karma
There you go. You are ready to start Karmaing (I just made that up, please don’t go about using it in real life) in three easy steps!
Getting your config file up
If you used Yeoman to create your app skeleton, then you already have a ready-made Karma config file waiting for you to use.

If you are more comfortable in setting up a REST server in another language, this is the API you need to conform to:
url HTTP Method Operation /api/books GET Get an array of all books /api/books/:id GET Get the book with id of :id /api/books POST Add a new book and return the book with an id attribute added /api/books/:id PUT Update the book with id of :id /api/books/:id DELETE Delete the book with id of :id
The outline for this section looks like this:
Install node.js, npm, and MongoDB
Install node modules
Create a simple web server
Connect to the database
Create the REST API
Install node.js, npm, and MongoDB
Download and install node.js from nodejs.org. The node package manager (npm) will be installed as well.
Download, install, and run MongoDB from mongodb.org (you need Mongo to be running to store data in a Mongo database). There are detailed installation guides on the website.
Install node modules
Create a file called package.json in the root of your project. It should look like
{ "name": "backbone-library", "version": "0.0.1", "description": "A simple library application using Backbone", "dependencies": { "express": "~3.1.0", "path": "~0.4.9", "mongoose": "~3.5.5", "body-parser": "~1.9.1" } }
Amongst other things, this file tells npm what the dependencies are for our project.

Adding single members directly to CouchDB for each line in the file can cause update thrashing, where two band member lines attempt to create/update the same band document at the same time, forcing the system to reinsert when one of them fails CouchDB’s version check.
The catch with this strategy is that you’re limited to the constraints of Redis to hold an entire dataset in RAM—though this limit could be overcome by the simple consistent-hashing cluster we saw on Day 2.
With our data file in hand, ensure you have Node.js installed as well as the Node Package Manager (npm). Once that’s all done, we need to install three NPM projects: redis, csv, and hiredis (the optional Redis C-driver we learned about yesterday that can greatly speed up Redis interactions).
​​$ npm install hiredis redis csv​​
Then, check that your Redis server is running on the default port 6379, or alter each script’s createClient function to point to your Redis port.
You can populate Redis by running the following Node.js script in the same directory as your TSV file, which we assume is named group_membership.tsv.