Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Thoughts on Unit Testing Directives in AngularJS

4/24/2016

0 Comments

 
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. 

Books

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.

Key Thoughts

- 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:
browser.get('http://localhost:3000/#login');
while a unit test is going to test a small snippet of html, only enough needed to use the directive under test:
var element = angular.element('<div first-directive></div>');
var compiledElement = compile(element)(scope);
scope.$digest();
- 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:
beforeEach(inject(function() {

}));
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:
beforeEach(function() {
     browser.get('http://localhost:3000/#/calc')
});
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Want FREE access to
    my daily stock tips 
    ​newsletter called,
    "The Triple Gainers"?

    Sign up here!

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2021