Tips for Testing AngularJS SPA's with Karma and Jasmine

english mobile

When I first started unit testing with AngularJS, Karma, and Jasmine, I read everything I could get my hands on about the subject. While there’s a lot of good material out there, I wouldn’t want to write tests, let alone ask my team to do so based on the material I originally found. This post is the one I wish I’d found when I first started writing tests, less of a “just how to get it working” and more of a “how to make writing tests a sustainable part of your process.” Some of these tips seem relatively trivial, but even little things can add up to make writing tests easier for a team.

Angular.mock.dump() and karma.dump()

One thing that is hard about unit testing with Karma is it’s not that practical to set break points. When I first started doing unit testing, I didn’t even know that you could use console.log() to print out useful messages from the tests and code.

When I figured that out, it helped quite a bit. However, complex objects could get hard to read and sometimes even would be truncated. It turns out that angular.mock has a method, dump() that will format the object to be legible from the command prompt window. Karma also has a dump() function that’s basically a shortcut to console.log().

Setting flags to only print information from certain tests

One thing that’s a little scary about using angular.mock.dump() in the code under test is that you’re going to get an error when you try to run that code and it’s not from a test. In addition, if a lot of tests run a particular piece of code, it can be difficult finding the output that matters to the test you’re working on from a firehose of output statements.

I like to give my AngularJS “classes” a method that sets a flag to turn these statements on and off. I usually call it setDebug(), and then I will call that from the test I’m currently working on (or in a beforeEach, depending on how early I need it set).

Shared Data Sources

It’s pretty common for multiple Services and Controllers to need similar data sets in their tests, so the data sets are easier to use and maintain if they are not kept in each spec, which is what you’ll see in most test examples on the internet. The biggest thing that has made testing sustainable for my team is to develop a convention for our data sources, both in how they’re named and how they’re structured.

The service name will usually be “myDataTypeData”. In turn, the mocks that are mocking services that retrieve those data sets can be given a path of “myDataType” and will return the data from the myDataTypeData service by asking for it from the injector.

Each data service has a method, “getAllData,” that returns converted json from an entire XML file, including a collection of data items with some header information that pertains to the collection as a whole. It also has a “getNormalizedDataItems” method that returns the collection of data items pre-processed to remove the XML artifacts. So tests that are testing large pieces of the application and tests that are testing smaller pieces of the application can get the data they need.

Balancing DAMP with DRY

Most of us have heard of DRY (Don’t Repeat Yourself) for production code, which means that rather than duplicating code to other places that need similar functionality, you extract that code and call it from all the different places that need it. Unit tests often are running almost the same code, but with slightly different inputs. One of the purposes of tests is to document what the software should do, so developers need to be able to see at a glance what the inputs are, where they are going, and what the expected outputs are. So tests are often written to be DAMP (Descriptive and Meaningful Phrases), which can translate to a lot of repeated code.

If you’ve ever wanted to make a major change in your code and felt that sinking feeling because you knew that too many places referenced it directly, you understand why DRY is a good thing. Change things in one place and you’re done. It can be hard to make a business case for tests to non-technical managers, so the last thing you ever want to say is “yes, we can make that change but it will take a while because there are a lot of tests around that functionality.”

After being bitten by that one, I developed a strategy that works in part because of my shared data sources. Most Jasmine specs have a setup() method. The first argument will usually be the data source name. If we’re testing at the individual data item level, the second argument will be the index of the data item I want.

Here’s where it gets tricky, because the setup step is usually going to instantiate something, whether it’s $controller building a controller for me to test with the data item attached to its $scope or a Factory (not an angular factory, specifically, but the Factory pattern) that is responsible for turning that data item into something else. So customization often needs to happen in between when we retrieve the data from the service and when we construct the item.

To handle that, I use a third parameter that takes a function to perform whatever extra massaging I need to customize the data before the actual object gets made. This means that once you know the pattern, you can read setup(‘someType’, 2, function (dataItem)’bar’}), and know exactly what is going on. In most cases, it’s not critical to see all the steps to retrieve the data and make the thing, with all the $rootScope.apply() calls that keep angular happy. What you care about is what’s special about this data as an input.

The flip side of that is that most examples you’ll see on blogs, etc., have a beforeEach() method at the top of the spec that pulls absolutely everything you’ll need from the $injector and stores it in a variable. This can get kind of hard to read, especially if for whatever reason you don’t use the actual name of the service as the variable name. And of course there’s that little part of my developer soul that is slightly bothered by exposing a variable for an entire spec that is only used in one place.

It took me a while to realize it, but you can use inject() any place that takes a Function in a spec. So, you can put an inject() in any beforeEach, not just the first one, or as the second parameter for a describe() or it() statement. Probably the only thing stopping you from injecting at the top-level describe block and simplifying your variable declarations is the need to bootstrap the module(s) under test.


When I took over the project I am currently leading, it was basically a prototype grown out of control. It had no tests and few modules. Our first priority was to start writing tests on new code, and then we started breaking out modules based on features. But our tests all bootstrapped the full app. The tests began to take longer and longer to run, so we knew that had to stop. Our first efforts at just bootstrapping a few modules in tests broke the tests pretty hard, so we put that task on the back burner while we continued to think about it.

Finally, it occurred to me that we had made a bad assumption when we decided what to leave in the main “app” module. It seemed logical at the time to leave pieces that were used in a lot of places in the app in the app itself. That was exactly backward, because any test that tested anything that depended on any of these commonly-used services had to bootstrap the app, which bootstraps everything. So the lesson learned there is “the more places it’s used, the more it needs to be in its own module.”

We also added a “mocks” module, which contains overrides of some of the services that we don’t want to really use in tests (like the one that plays audio or the one that reads the XML into JSON). By doing it this way, we can choose the real service simply by not bootstrapping “mocks.” The exception to this is sometimes you have to bootstrap the module the service is actually in, and that module can “win.” To resolve that situation, you can register a mock service in the module where the original service was, which will overwrite it, and also register it in the mocks module.

What are your tips?

I by no means claim that these tips and tricks are the best way to do things, but they are things I have found out for myself through trial and error that I have found helpful. Maybe you’ll find them helpful. If you have some killer ways to rock your jasmine tests, please feel free to share them in the comments.

How I learned to stop hating the command line (so much)

english mobile

I'm old. So I still remember when every computer screen looked like what we now call "the command line." Screens were black and text was green or amber. Anything you needed to do on the computer took forever and depended on your ability to remember lots of commands and type them correctly. This made computing seem difficult and unapproachable. So when the first MacIntosh computers showed up in the Art Department where I was studying, I was as happy as anyone.

Finally, it was (more or less) obvious how the computer was organized, and figuring out how to do things and what you could do was easy and intuitive. When I first used a Windows 3.1 computer, I was even more delighted. And so I have been a relatively happy camper for many years. Of course there have been difficulties and frustrations, but by and large if there was any task worth doing someone had built an interface that made it easy to do things without having to remember tedious commands and type them accurately. This allowed me to reserve my head space for the problems that actually interested me and/or I had been hired to solve.

I recently started learning AngularJS, and I was dismayed to find that before you can even start with Angular, you have to do a fair amount of work in the command line. Not only that, but there's little explanation of why you need to do these steps and no explanation of whether there are alternate routes to accomplish the same thing. I get that when you're trying to write a tutorial if you try to cover every last bit of background information someone might want to know that you'd never get finished.

However, the absence of such supporting information right now means that if you want to learn AngularJS (and I suspect many other current JavaScript frameworks) you're stuck using the command line whether you like it or not. You may not be surprised to learn that I don't especially like it. I've learned a few things over the past couple of months that make me dislike it a lot less. So if you share my dislike of the command line, keep reading and maybe we can make life a little easier.

Why we need the command line

As my elementary school teachers could tell you, I've never been especially fond of just doing things without knowing why. So let's start with why it is that it seems everything needs to go through the command line. The answer is simple (but seldom articulated): nodejs. Node was designed as a server-side technology, so the only facility it has for drawing to the screen is a browser that's pointed to a site that it's serving. However, most of the tools you use during JS development aren't conceived that way and I have no idea if they could be.

You get a few things from node that nearly make its lack of a decent UI worth it.

  1. Node lets you run a webserver (or several) out of any directory on your computer. Unlike more traditional web servers such as IIS or Apache, Node lets you just fire up a server anywhere. Security considerations aside, it's pretty cool that you're now free to put your code in whatever directory makes sense for development rather than the directory that the web server can see (or that you've set up as a virtual directory). You can even have one server running the site and another server running the unit tests.
  2. Node Package Manager (npm). Npm is like a magic registry in the sky that will allow you to more or less instantly download and install most tools and libraries you'll need for doing Javascript development. It will also make sure that if those tools have dependencies that the libraries they depend on are also installed, even if the different tools depend on different versions of the same library.
  3. Many of the tools are also in node or at least depend on it/run on top of it. For example, Karma test runner launches a node server on its own port to run the tests.

So we kind of have to use it, or at least get enough benefit from it that we can grit our teeth and use the command line. I had several pain points that were driving me flat up the wall, so I'd like to share some solutions that made things easier for me.

Navigating to directories

Directories to me are only significant as a way to keep things organized and insofar as they affect whether my code runs properly or not. It's not even on my radar to be interested in constantly typing (correctly, no less) CD and the full path to the directory where my code lives. So yes, I know from a stint doing embedded development on a Linux device a few years ago that to some extent you can get the CLI to type paths for you by typing the first letter or two of each directory and pressing the tab key. But that's only slightly less tedious. After several days of this, I got really annoyed and went searching.

What I found is that on Windows you can hold down the shift key while right-clicking on a folder. This adds "Open a command prompt here" to the context menu. Note that this is in a different spot on the context menu depending if you right-click on the left pane (shown) or the right pane.

Mac/Linux people, I am sure Google can tell you similar shortcuts for your environment. The point is you don't have to type the path over and over.

Typing the same crap over and over

If I enjoyed typing the same crap over and over, I probably wouldn't have decided to be a programmer. But as I got into the red-green rhythm of testing, I found myself typing karma start (red), karma start (green), cls (if the test passed, clear the screen and on to the next test).

Again, my vague memories came to the rescue and I remembered that you can use your up and down arrow keys to rerun the same commands again. So, once I'd typed karma start once, I could run it again by pressing the up arrow key once and pressing Enter. Then, after typing cls, two up arrows goes back past cls to karma start. From there, one up arrow goes to karma start again, or a down arrow goes to cls.

Yes, Mac users, I know that you use a different command to clear your screen, but I don't know it off the top of my head. Usually I run bootcamp on any Mac I use. But the idea is the same.

Not being able to read the text

Like I said, I'm old. I also enjoy sparring in karate, so my glasses may not sit exactly where they're supposed to on my head. So the red karma uses for a failed test is completely illegible to me on the default black background. Karma has about four colors I have seen:

  • Medium gray for ordinary text
  • Green for passing test
  • Red for failing test
  • Teal for console.log

I vaguely think I remember seeing yellow, but I don't remember what they were doing with it.

All this variability means that it can be tough to find a background color that works. Let's not talk text colors, because node and karma change them anyway. In karma, you have the choice of "use colors" or "don't use colors," but there is no "use better colors." Anyway, my experience is that a white background gives the best legibility across the most frequently-used colors. It's a shame, because it seems that "real developers" use darker color schemes, but there you have it.

One problem with setting the background color is that any time you run npm install, node will helpfully change your background color to black. I haven't found any way to get it to stop doing this, but if you set your defaults, you can just close the window and reopen it using the right-click menu described above. To set the defaults, just click the icon in the upper left of the command prompt and select "Defaults."

Future plans

For those wondering why I launch karma over and over rather than just leaving it running, I'm planning an entire post on karma setup in the future.

Deja vu all over again

english mobile

When I started this blog in 2007, I had been developing in a technology called Authorware that Adobe was in the process of killing off. So I decided to concentrate on teaching myself Flex, sharing what I was learning with others who were also making that transition.

I still work in Actionscript a lot, primarily in Flash, but unfortunately I've had to come to the decision that it's not the best use of my time to blog about some of the neat things I'm still doing.

It's not fair to say I'm learning JavaScript, since I've been doing JavaScript in some form for over ten years, but I am learning about the JavaScript ecosystem as I find it in 2014, particularly surrounding AngularJS. I'm shifting focus to once again ramp up in tools that are relevant instead of ones that are becoming less relevant. In the process, hopefully I can help others who are making a similar transition or just who want to learn about the parts of the process that I delve into. I like to think that I'm willing to write about things that others may not feel are glamorous enough to devote time to but that can be incredibly helpful if you're looking for just that information.

I have decided to keep the title of the blog the same. In part this is because I have some loyal readers who have been with me the whole way. In part this is because I think that the AngularJS world will realize in time that the particular experience that former Actionscript developers, especially Flex developers, bring to the table means that we're very well-suited to do things the "Angular way." I hope you enjoy the new direction.

ActionScript, E4X, and White Space

english mobile

I've been reading XML into ActionScript 3 for many years, and I still haven't solved all the issues with it. Given that my searches for solutions to these issues turn up so little, I decided to share the solutions I have found, especially when it comes to white space.

I think at least part of the reason that these problems are so hard to solve is that the XML documentation for many of the properties and methods we need to use is frustratingly circular. So let me start by laying out what some of these do in practice.


I think ignoreWhiteSpace is the property we first try to use to correct some of the issues with E4X. This property will, in fact, fix issues that are caused by having "extra" children that you weren't expecting to be parsing when you use normal formatting, like new lines and tabs, in your XML. However, it then causes other white spaces that might have been pretty important to disappear.

An alternative to using ignoreWhiteSpace is to use xmlVar.elements() instead of xmlVar.children(). This works relatively well unless you need to access something that's not an element, like a comment, or you need to pass the XML to Adobe code (like new DataProvider()) that for some reason is not equipped to handle the vagaries of their own E4X implementation.


We're all familiar with the problem where the default XML parsing adds even more formatting than we included in the original XML file, so that

 <p>The quick brown fox jumped over the
 <b>lazy</b> dog.</p>
 <p>The quick brown fox jumped over the 

I'm sure there are some versions of Flash where setting prettyPrinting to false fixes this problem, but in the version of the Flash player we target, setting this property has no effect. The good news is that TextField, the Flash Label component, and the Flex MX Label component support the condenseWhite property, which gets rid of this issue for text you want to display. I guess if you're using a spark label, the skin will need to use something that supports condenseWhite if you need this.

Wrap in CDATA

One of the reasons XML parsing is so thorny is that we're using the XML to store external content, often HTML-formatted. CDATA does work to preserve HTML formatting, but it has its own issues. One issue is that there's no real documentation about how to get the text out of CDATA.

Funny story--I was once on a Flex project where it turned out I needed to wrap the entire XML package I was building in CDATA. Try though I might, I couldn't find anything that told me how to get at the "stuff" inside the CDATA. With deadline fast approaching, I "hacked" it by giving the text to a Spark label (which did know how to do this), then reading the text out again and casting it to XML.

Some months later, I ultimately discovered that String(myNode) will retrieve the contents of a node, whether its contents are wrapped in CDATA or not. Unless the contents are not wrapped in CDATA but contain HTML formatting anyway. This is something that is quite likely to happen on my team, as the people who produce the XML see no reason they shoulld start wrapping everything in CDATA just because we're using ActionScript 3 now.

In addition, even when things are wrapped in CDATA when they should be, you have to dump your text in one line into CDATA because it faithfully reproduces all those \n's and \t's, and you wind up with a bunch of junk nodes if you have to cast the contents back to XML. This results in XML that isn't readable, and readability is one reason to use XML in the first place.


So, if you're getting XML and you know that sometimes certain nodes will contain HTML and sometimes that HTML will be wrapped in CDATA and sometimes it won't, what do you do? One thing you can do is create a function that checks for hasComplexContent and calls toString() if it returns false and children().toXMLString() if it returns true.

This works well, but one issue with it is you have to have access to this function wherever you're parsing XML. And since you can't really extend the XML Class, due to its dependence on static methods and properties, that means you either need to use a static method yourself (not my favorite thing) or you need to violate DRY, unless your application lends itself to only parsing XML nodes in exactly one place.


This is probably my favorite method, because it gets rid of all the weird child nodes that contain "\n\t\t". This means that I can now parse formatted XML and not have to allow for weird extra junk in there. More importantly, I can pass that XML to "dumber" code, such as the Flash DataProvider constructor, and it works. It also does not take out the spaces around inline HTML tags, so you can normalize() the XML to make for easy parsing that you should get from ignoreWhiteSpace (but don't, due to the removed spaces), then you can call String(myXMLNode) on the node whether it has complex content or not, and it will work in a TextField with condenseWhite set to true. FTW!

Batch Import Images into Flash Symbols

english mobile

When I find a workflow I like, I sometimes assume that everyone has already discovered that workflow, and I'm late to the party. One of the nice things about participating in user forums is that sometimes that helps me realize that a favorite workflow isn't known to everyone. This is great, because then I have something to blog about!

Today, I want to talk about what is probably one of my all-time best productivity tricks in Flash. In a few minutes, I can:

  1. Import a series of pngs, jpgs, or whatever into my Flash movie
  2. Choose for each image to be wrapped in a symbol or not
  3. Have the symbols and images nicely organized into folders in the library, without any extra effort!
Here's how I do it, starting with a folder of images:

Step 1: Browse the folder in Bridge

First, I right-click on the folder of images and select "Browse in Adobe Bridge"
Once it opens, I select the images I want to use. For this post, I just selected everything in the directory, but Bridge has excellent features for finding just the images I want (that could be a blog post in itself). You can drag them around in Bridge to change the order, if you need to.

Step 2: Load the files into PhotoShop Layers

In the Bridge "Tools" menu, I select PhotoShop>Load Files into PhotoShop Layers.

This launches PhotoShop and does all the work to put the files into PhotoShop layers, in the order they were in in Bridge. Once that has finished running, save the result as a .psd.

Step 3: Create a Symbol in Flash to Import into

Whenever I have to create a new Symbol in Flash, I like to start with a rectangle that's the size I want the MovieClip to wind up, so the registration and transformation points fall in predictable places. So, I create a rectangle the size of my images and convert it to a Symbol (F8).

Step 4: Import the PSD

I usually use the keyboard shortcut to import to stage (Ctrl-R). The import dialogue gives me lots of options. For example, I can select all the images and choose to convert them to MovieClips.
I'll usually convert the images to MovieClips if I want to do timeline animation with them or if I want to apply a base Class to the MovieClip. I can also choose to import each to a different layer or to a different keyframe. Both options are useful, depending on the end result I need.

Once the psd has imported, the timeline looks like this.

Note that the frames will be the reverse order of the order they were in in Bridge. I usually select the frames and reverse the frames (there's a right-click menu for it, but I've set up a keyboard shortcut).

Step 5: Edit the Library Symbols

The MovieClips and Images are nicely organized, but usually there is more I want to do to them.
If I'm applying a Base Class, I might want to rename all the Symbols, for instance, to change ".png" to "_png." I like the Search and Replace extension for this.

Flash also allows you to edit more than one library symbol at a time, by selecting several symbols and clicking the Properties button (sorry, if there is a shortcut for this, I haven't found it).
From this dialogue, you could change all the MovieClips to Graphics, for example, or you could set a Base Class on all the MovieClips at once. Tip: if you set a Base Class, Export for ActionsScript will become checked and the drop-down will have "Yes" selected, and you don't have to touch it.

And that's it, my quick and easy workflow for importing a series of images to Flash.

FlashTips #6: Debug refresh issues in your swf

english mobile

Most of us know that Flash Builder makes it easy to debug a swf on a server by creating a custom debug configuration. Recently, I had a problem where I needed to be able to debug issues that happened when the browser window was refreshed.
Jeffry Houser, the brains behind, apparently had this very same problem, and resolved it by starting a new debugger session that points to about:blank and then connecting that to the swf after the window has been created.
That's a great solution, but the issue I was having was happening pretty much immediately after the window was refreshed--if I went through his steps, I'd "miss the window." So, after limping along for several hours, trying to figure out the problem without having the direct debug information, I finally figured out that the problem was amazingly simple:

  1. Create a new "refresh" debug configuration that doesn't point to a page with a swf in it (I didn't yet know that about:blank was an option).
  2. Start the original debug configuration, that points to the swf as normal.
  3. Get to the problem point in the application.
  4. Launch the "refresh" configuration.
  5. Refresh the window.
  6. Voilà, we are connected, from the very beginning of the bootstrapping process.

Reusing a Flash Builder Workspace

english mobile

I use Flash Builder and Flash Pro together, and over time I have learned to just bow to how Flash Builder wants me to do things. What this means is that each "family" of Flash files has to have its own Workspace, because Flash Builder expects the Flash file to be in a subdirectory of the workspace in a directory that's the named the same thing as the Flash file. If that's not where it is, you either have to remember to tell Flash Builder not to use its default or things go very wrong, very fast.

In a team environment, the process has to be as simple as possible, without a bunch of fiddly steps that people have to remember to do at exactly the right moment, so it's just easier to go with what Flash Builder wants us to do. However, this adds other fiddly steps that people need to do each time they start a new workspace, such as importing snippets and laying out all the panels just so.

So I thought I'd hit on a brilliant plan, which is just to check the workspace settings into version control that live in the .metadata folder. This way, everyone would get a workspace that's already set up when they export the "base" Flash project and they could just start working. And this does work for the intended purpose, but I found that workspace relative paths would break (paths that refer to the ${DOCUMENTS} variable). These would unaccountably point to the original base project directory.

On Windows, for whatever reason a file contents search didn't turn up the offending string. I suspect this has to do with the weird way the .metadata folder starts folder and file names with "." So I was off to Google to try to find out how other people transfer their settings. I found one post that suggested you just copy the .metadata/plugins/org.eclipse.core.runtime/.settings directory. The result I had from that was that the site relative root now pointed to the project I'd copied the directory from. At least now I had it narrowed down to a large handful of files that I could then open and look at individually.

To save you the bother, I'll tell you that the file that contains the location of the workspace for workspace-relative links in the above directory is called org.eclipse.core.resources.prefs, and that file contains the pathVariable.DOCUMENTS string that tells Flash Builder/Eclipse where the workspace thinks it is. I haven't had a chance to play with it to see if simply not putting that file in version control makes things better or borks them, but even if everyone has to manually update that variable when setting up a project it's much better than having to set up every workspace from scratch when we start a new project.