Testing SPA’s is a huge topic and libraries and frameworks are constantly evolving. So in this entry we’ll focus in the current set of tools that will allow us to get a test suite on an Angular 1.5.
In a Angular application, test will fall into two types: unit tests and end to end test(E2E). The first type are like black-box-style test, where a piece of the application is isolated, mocked out its dependencies, and fed with some inputs, then it output or functionality is verified. On the other hand E2E tests simulates high level interaction by simulating a user interacting with components and ensuring proper behavior. For this tests we need an actual web browser that loads and execute our code.
For unit test, the common tool is Karma, a test runner which will execute our test and give us feedback whether our test pass or fail, and where in our code. In the case of E2E test there is a special framework for Angular called Protractor.
Both tools will require a *.conf.js file which will act as the test suite director when invoked by Grunt or Gulp (task runners).
The good news is that both, Karma unit test and Protractor end-to-end test use the Jasmine syntax, which is inspired by RSpec.
Depending on which came first in our task runner, each set of test will execute separately. Karma will load a headless browser, generally PhantomJS, and Protractor will load Sellenium WebDriver to set up a real browser which we can see in action in our local development environment.
We can see that testing Angular applications requires a fair amount of configuration and when they say that Angular is a framework built with testability in mind, they refer to the modularity of the code (modules, dependencies, services) which can be mocked and isolated for testing, but definitely not when we are thinking on a out-of-the-box test suite.
Maybe that’s one of the main reasons why Thoughtworks, a leading edge technological company, recommends frameworks like Ember and React and not so much Angular JS for new projects.
But there are a lot of Angular apps out there that need support and it our mission to build a healthy test suite before touching the code.
An excellent course, that covers step by step all the configuration needed is found on Tuts+