JavaScript Testing

Noun

Unit Testing

  • test runner

    • Karma. It runs test suite written in Jasmine, Mocha etc.

    • cucumber

  • test framework: beforeEach, describe, context, it

    • Jasmine

    • Mocha

  • assertion library: everything inside the it block: expect, equal, and exist

    • Chai:

    • power-assert "No API is the best API"

Popular tools:

  • Jest = test framework + test runner

End to End Testing

  • end to end testing framework: Protractor (run against real browser)

Jasmine Cheatsheet

Spy (for methods, can track calls or specify return value)

Matcher

Tips

  • Remember to call $scope.$apply(); when testing promise. Why?

  • Call done() to instruct the spec is done for Asynchronous tests.

  • Call the following methods when you testing $httpBackend. See doc and source code

  • Call $httpBackend.flush() or $scope.$digest(), but not both

  • Any spec that calls a promise must execute expectations within a then or catch block (depending if the promise will resolve or reject)

Jasmine + Angular 1.x

Test component:

Test Service itself: mock http

Test service in component:

How to use mock data?

Mistakes to avoid:

  • Always to remember to return a promise!

  • In test, always do a $scope.$digest() after calling a promise so that it triggers the promise chain.

Read Istanbul Report

Last updated