Introduction
With the many tools available to aid in developing AngularJS applications, many people have the impression that it’s an extremely complicated framework, which is not at all the case. That’s one of the main reasons I started this tutorial series.
In part one we covered the basics of the AngularJS framework and started out by writing our first application. This post is designed for beginners. If you’re a more experienced AngularJS developer, you might be more interested in demystifying directives or a story of AngularJS in use at a growing startup.
In this tutorial, we’re going to set aside the application logic layer and learn how to conduct proper AngularJS project setup, including scaffolding, dependency management, and preparing it for testing (both unit and end-to-end). We’ll do this using these AngularJS tools: Yeoman, Grunt, and Bower. Then, we’ll review the process of writing and running Jasmine tests using Karma.
Karma, Jasmine, Grunt, Bower, Yeoman… What are all these tools?
If you work with JavaScript, it’s highly probable that you already know of at least some of these tools, even if you’re new to Angular. But to help ensure a common baseline, I’ll avoid making any assumptions. Let’s briefly review each of these technologies and what it’s useful for:
- Karma (previously known as Testacular) is Google’s JavaScript test runner and the natural choice for testing AngularJS. In addition to allowing you to run your tests on real browsers (including phone/tablet browsers), it is also test framework agnostic; which means that you can use it in conjunction with any test framework of your choice (such as Jasmine, Mocha, or QUnit, among others).
- Jasmine will be our test framework of choice, at least for this post. Its syntax is quite similar to that ofRSpec, if you’ve ever worked with that. (If you haven’t, don’t worry; we’ll check it out in greater detail later in this tutorial.)
- Grunt is a task runner that helps automate several repetitive tasks, such as minification, compilation (or build), testing, and setting up a preview of your AngularJS application.
- Bower is a package manager that helps you find and install all your application dependencies, such as CSS frameworks, JavaScript libraries, and so on. It runs over git, much like Rails bundler, and avoids the need to manually download and update dependencies.
- Yeoman is a toolset containing 3 core components: Grunt, Bower, and the scaffolding tool Yo. Yo generates boilerplate code with the help of generators (which are just scaffolding templates) and automatically configures Grunt and Bower for your project. You can find generators for almost any JavaScript framework (Angular, Backbone, Ember, etc.), but since we’re focusing here on Angular, we’re going to use the generator-angular project.
So, where do we start?
Well, the first thing we’ll need to do is install the tools we’re going to need.
If you don’t have git, node.js, and npm installed already, go ahead and install them.
Then we’ll go to the command line and run the following command to install the tools of Yeoman:
npm install -g yo grunt-cli bower
Oh, and don’t forget, we’re going to use the AngularJS generator so you’ll need to install it as well:
npm install -g generator-angular
OK, now we’re ready to…
Scaffold/generate our AngularJS application
Last time, we manually borrowed our boilerplate code from the angular-seed project. This time, we’ll let yo (in conjunction with generator-angular) do that for us.
All we need to do is create our new project folder, navigate to it and run:
yo angular
We’ll be presented with some options, such as whether or not to include Bootstrap and Compass. For now, let’s say no to Compass and yes to Bootstrap. Then, when prompted about which modules to include (resource, cookies, sanitize and route), we’ll select only angular-route.js
.
Our project scaffold should now be created (it may take a minute), integrated with Karma and all pre-configured.
Note: Bear in mind that we’re restricting the modules here to the ones we used in the application we built inpart one of this tutorial. When you’re doing this for your own project, it will be up to you to determine which modules you’ll need to include.
Now, since we’re going to be using Jasmine, let’s add the karma-jasmine
adapter to our project:
npm install karma-jasmine --save-dev
In case we want our tests to be executed on a Chrome instance, let’s also add the karma-chrome-launcher
:
npm install karma-chrome-launcher --save-dev
OK, if we did everything right, our project file tree now should now look like this:
Our static application code goes into the app/
directory and the test/
directory will contain (yup, you guessed it!) our tests. The files we see on the root are our project configuration files. There’s a lot to be learned about each one of them, but for now we’ll just stick with the default configuration. So let’s run our app for the first time, which we can do simply with the following command:
grunt serve
And voila! Our app should now pop up in front of us!
A little about Bower for AngularJS
Before getting into the really important part (i.e., the testing), let’s take a minute to learn a bit more aboutBower. As mentioned earlier, Bower is our package manager. Adding a lib or plugin to our project can simply be done using the bower install
command. For example, to include modernizr
, all we need to do is the following (within our project directory, of course):
bower install modernizr
Note, though, that while this does make modernizr
part of our project (it will be located in the app/bower_components
directory), we are still responsible for including it in our application (or managing when it should be included) as we would need to do with any manually added lib. One way to do this would be to simply add the following <script>
tag to our index.html
:
<script src="bower_components/modernizr/modernizr.js"></script>
Alternatively, we can use the bower.json
file to manage our dependencies. After carefully following every step until now, the bower.json
file should look like this:
{
"name": "F1FeederApp",
"version": "0.0.0",
"dependencies": {
"angular": "1.2.15",
"json3": "~3.2.6",
"es5-shim": "~2.1.0",
"jquery": "~1.11.0",
"bootstrap": "~3.0.3",
"angular-route": "1.2.15"
},
"devDependencies": {
"angular-mocks": "1.2.15",
"angular-scenario": "1.2.15"
}
}
The syntax is fairly self-explanatory, but more information is available here.
We can then add any additional new dependencies that we want, and then all we need is the following command to install them:
bower install
Comments are closed, but trackbacks and pingbacks are open.