I've been struggling to wrap my head around how to not only test directives, but how to approach teaching others how to test directives. I've been working on this github page with examples of common AngularJS things and how to test them, and how could I even pretend the list was complete without covering directives? I was looking at two resources today that opened my eyes a little bit, and I'll try to convey what I learned in this post.
I still find physical books super useful and packed with information, especially higher level information that isn't usually available online. Today I was flipping through Learning AngularJS by Brad Dayley, and I came across a section on unit testing directives. Like most other examples, he uses the $compile service in the beforeEach block to create the directive in a vaccuum. Then you expect the element's html to contain something or to have some class.
But Is It Really Unit Testing?
I felt a little bit uncomfortable with this approach at first. I thought unit testing was not supposed to touch the DOM or be concerned with classes or clicking things. This all seems like integration tests so where do we draw the line? Indeed, the syntax between Jasmine and Protractor are so similar that it is easy to confuse the two. Both unit tests and e2e tests can syntacticallly be described as, "Describe's full of it's with some beforeEach's". In Protractor we create an instance of the page, poke around at things, and then expect certain things to be true. It seems that with the $compile function we are doing this same thing in unit tests now,
Learning from the Pros
I then went home and watched this video on testing directives (I am pretty sure I had seen it once before, but for some reason I really took a lot more out of it this time around). This is a video of a presentation by Vojta Jina, of the key developers of Testacular (the test runner that is know know as Karma) and all-around wizard of software testing.
- The difference between unit tests and e2e tests is not that unit tests cannot touch DOM at all. They both do, but they do it differently. e2e tests leoad an entire page at a time with the borwser.get method:
while a unit test is going to test a small snippet of html, only enough needed to use the directive under test:
- Unit tests are all about being fast. If a button click calls out to a server, then obviously should should not actually make the request in your tests. But "clicking things" is not off limits in unit tests. You could absolutely say element.click(), then check that a mock server call was send out. Basically, if it's not asynchronous, it's fair game for unit testing.
- Dig into the functions. In his talk above Vojta briefly touched on this pain that I was experience as he says this type of testing is, "a little bit higher level than unit testing" in that it is using this compile and inspecting DOM stuff. He notes that sometimes you can look only at a directive's controller of link function without the html template at all. This is much more in line with that I would traditionally think of as unit testing; testing the public api by calling it's methods and then running assertions on the results. Vojta also mentions that I think when trying to expose bugs with unit testing is can be easier when you are in the functions and closer to it's internal properties and methods. Both testing the link function and controller's methods individually and testing the the isolated directive as a whole can both be extremely useful, and I believe there is a happy place for both approaches in the world of unit testing.
- Protractor has no "inject" method available in its beforeEach's. This is key because when unit testing you pull in all of your controllers, services, filters, etc through DI, even directive link functions. That is what makes it unit testing. When running your test in Protractor it will actually throw an error if you try to use the inject keyword. So if you try to say this:
it then Protractor's underlying WebDriver will crash with the error, "ReferenceError: inject is not defined" even though this is perfectly fine syntax in Angular with in your Karma / Jasmine tests. The point is that they are slightly different api, and it's because you should use them in different ways. Just to be clear, below is the right way to set up your tests in Protractor:
The posts on this site are written and maintained by Jim Lynch. About Jim...