Modern Tools for Unit Testing
by Marco Liberati
Why test JS code?
- It makes you confident about your code
- Helps understand the design of the code
- You want to make big changes quickly
-
Why test JS code?
- It makes you confident about your code
- Helps understand the design of the code
- You want to make big changes quickly
-
Because each browser has its own dialect*

What we want to test?
- JS/Node library
- Web Application
- Enterprise App
What we want to test?
- JS/Node library
Unit Tests, TDD
- Web Application
E2E Tests, BDD
- Enterprise App
Integration Tests
What we want to test?
- JS/Node library
Unit Tests, TDD
-
-
One problem per time...
Starting with the basics:
- Assertions:
Expect, Should, ...
- Frameworks:
Mocha, Jasmine, ...
- Test Runners:
Karma, TheIntern
- Browsers:
2N combinations...
Assertion Library
- Pretty much the same functionalities
- Most of them will work for IE9+
-
If you have to test
IE6 IE7+ there’s a single choice: expectJS
Framework
Assertion Library
- Choose an assertion style
- Choose a reporter style
- Write the tests
- Run the framework…
Test Runners
- Take your tests
- Choose a bunch of browsers
- Run them in parallel!
- See the reports
Test Runners Workflow:
- Preprocess tasks
- Add frameworks and assets
- Use launchers to spawn browsers
- Call the reporters when finished
Browsers
100+ combinations to test!
Browsers
100+ combinations to test!
... but we don't give up!
Browsers
Firefox
- To test on all platforms!
- Only few key versions to test
- Lot of Enterprises are using it now!
Browsers
Chrome
- To test on all platforms!
- People usually use the latest version
-
Default browser on Android now!

Browsers
Safari
- Test on Mac and iOS!
- Phone and iPad are very different!
- Can still use the iOS simulator but… you need a Mac for it!
Browsers
Internet Explorer
- Still a nightmare to test!
- 7 versions to test for JS... (13+ for CSS)
- Compatibility mode is not a valid option
-
Use free VMs from modern.ie !
Browsers
PhantomJS
- Nice for web crawling...
- Useful for static generation
- Can be used for quick checks
-
but IT'S NOT a real browser!
Extras!
-
ChaiJS - Assertions with superpowers!
-
Coverage- A little help while testing
-
Selenium 2 (WebDriver) - Serious testing
ChaiJS
- Multiple Assertion libraries
- Spies, Promises, Date equality
- Quite active community
Code Coverage
- Shipped as a plugin with most runners
- Useful to help writing better tests
- Note: don't let it be your only guide!
Selenium 2
- Uses the new WebDriver API!
-
Can do real user interactions
- Can test mobile devices natively