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)
// Spy on existing methodbeforeEach(function() { foo = {setBar:function(value) { bar = value; } };// Spy methodspyOn(foo,'setBar');// Return specified valuespyOn(foo,"getBar").and.returnValue(745);// Throw errorspyOn(foo,"setBar").and.throwError("404");});// Use createSpy to stub a methodbeforeEach(function() { foo = { setBar:jasmine.createSpy('setBar') };// Testexpect(foo.setBar).toHaveBeenCalled();});// Use createSpy to create bare methodbeforeEach(function() {let foo =jasmine.createSpy('foo');// Testfoo('you can pass any','args');expect(foo).toHaveBeenCalledWith('you can pass any','args');});// Use createSpyObj to create an object containing multiple methods// Useful for mocking Angular servicebeforeEach(function() {let $window =jasmine.createSpyObj<angular.IWindowService>('$window', ['open','alert']); (<jasmine.Spy>$window.alert).and.returnValue({}); // Test $window.open(); expect($window.open).toHaveBeenCalled();});// Use spy to create an object containing both methods and property// You cannot use createSpyObj as it's for methods onlybeforeEach(function() { foo = { bar: {}, setBar:jasmine.createSpy('setBar') };// Testfoo.setBar();expect(foo.bar).toEqual({});expect(foo.setBar).toHaveBeenCalled();});
Matcher
// Match with typeexpect({}).toEqual(jasmine.any(Object));expect(123).toEqual(jasmine.any(Number));// Match function, useful to test callback functionexpect(service.registerCallback).toHaveBeenCalledWith('key',jasmine.any(Function));// Match partial objectexpect(foo).toEqual(jasmine.objectContaining({ bar:"baz", fooFunc:jasmine.any(Function)}));
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