Unit testing in JavaScript? There is no such thing.” This is something most of the Java developers would say. With AngularJS coming more and more to scene and Google standing behind it, testing is starting to be core part of all AngularJS project. I would like to show how you can do unit testing in pure JavaScript (AngularJS) application (together with backend mocking…).
3. JavaScript testing problems
◦ Mixed JS/DOM
◦ Mixed backend calls into JS functions
AngularJS tries to fight itAngularJS tries to fight it
◦ DOM manipulations only in directives
◦ Business logic in services
◦ Flow of application in controllers
◦ . . .
5. Test runner
◦ launches HTTP server
◦ loads needed files
◦ runs test
All major browsers supported
npm install
karma -g
All major browsers supported
Available for testing on continuous integration
server
Configuration driven
6. Assertion library
Karma supports it via plugin
(Custom) Matchers
Setup and Teardown (beforeEach, afterEach)
Spies
npm install karma----jasmine
-g
Spies
describe("One testing suite", function() {
it("contains spec with an expectation", function() {
var javaCroAtendees = 12;
expect(javaCroAtendees).totototoBeBeBeBe(12, "number of
javaCroAtendees should be 12”);
});
14. E2E tests
How to make testing/developing more
standard/automated?standard/automated?
◦ Use Yeoman for scaffolding your app structure
◦ Use Grunt for building, deploying and automated testing
yo
angular
grunt
serve
16. Use Karma as test runner
Write your tests in Jasmine
Integrate Karma on continuous integration serverIntegrate Karma on continuous integration server
and TEST, TEST, TEST, TEST… you’ll be happier
later ☺