Upgrade to Pro — share decks privately, control downloads, hide ads and more …

intro to javascript unit tests for client side

intro to javascript unit tests for client side


Unit tests for UI developers is an underground world and today we will explore this world a bit.

The first question you need to answer is, why unit test my code? that will give you a better understanding of why unit tests are important and useful.
Here a few points.

Reduce bugs (new and/or existing features)
Serve as documentation
Improve Software Design
Reduce fear (you can write code with no fear of breaking something else)
Unit tests are meant to be small but what I mean by that is, let's say you have a function called foo() that calls two other functions fooUno() and fooDos() each function has a specific logic/goal, let's also say that one of those functions inside foo() make an XHR request..
You don't want to make an XHR request because it's slow and the XHR might depend on something else from the system, but foo() require the data from the function that make XHR request, so what do you do? this scenario is very common for UI developers.

One of the ways to achieve isolation and small unit tests is using Spies, Stubs and Mocks understanding them will help you write better unit tests, I'm going to talk more about it later.

A good structure for unit tests is 3A's Arrange, Act, Assert (not applicable to all scenarios), where Arrange is the set up of the object/function being tested (e.g stub a dependency), Act is when you act on the object being tested (e.g call the function) and Assert where you claim the object. (e.g assert that the function was called with xyz arguments);

Michael Lancaster

November 19, 2015

More Decks by Michael Lancaster

Other Decks in Technology


  1. “Program testing can be used to show the presence of

    bugs, but never to show their absence!” Edsger Dijkstra
  2. NIST report that the relative cost to repair a defect

    found in an operational system is 470 to 880 times the amount to repair a defect found at the start of the life cycle when requirements are being formulated. *NIST (National Institute of Standards and Technology)
  3. Conclusion… • Make unit tests part of the culture •

    Make unit test processes easy • Unit test your code as you go (ideally before) • Structure unit tests in 3A (Arrange, Act, Assert) • Automate all the things