$ gulp build
Build project:
$ gulp test
Run tests:
Run once:
$ gulp test-dev
Watch changes:
There are six most commonly used structures and blocks:
Grouping tests by topic:
describe(`Name of group or a module`, () => {
...
});
Importing AngularJS modules and dependencies:
module('app.settings');
Executing actions before each test:
beforeEach(() => {
module('app.service');
});
Injecting services for further use in tests (underscore
annotation):
let $rootScope;
let customService;
inject((_$rootScope_, _customService_) => {
$rootScope = _$rootScope_;
customService = _customService_;
});
Test case blocks:
it(`Should show a test block example.`, () => {
...
});
Assertion of a value:
it(`Should return 'Hello world'`, () => {
let ahoy = 'Hello world';
expect(ahoy).toBe('Hello world'); // will pass
});
Available assertion matchers:
.toBe()
.toBeCloseTo()
.toBeDefined()
.toBeFalsy()
.toBeGreaterThan()
.toBeGreaterThanOrEqual()
.toBeLessThan()
.toBeLessThanOrEqual()
.toBeNaN()
.toBeNull()
.toBeTruthy()
.toBeUndefined()
.toContain()
.toEqual()
.toHaveBeenCalled()
.toHaveBeenCalledTimes()
.toHaveBeenCalledWith()
.toMatch()
.toThrow()
.toThrowError()
Example test case:
describe('Reverse filter', () => {
let $filter;
const timezones = [
'(GMT-11) Niue Time',
'(GMT+0) Azores Summer Time',
'(GMT+5) French Southern & Antarctic Lands Time',
];
beforeEach(() => {
module('app.filters');
inject((_$filter_) => {
$filter = _$filter_;
});
});
it('Wrong string value passed. Should return string', () => {
expect($filter('timezoneLabel')('Hello world')).toEqual('Hello world');
});
it('Proper value passed. Negative offset. Should return GMT +0', () => {
expect($filter('timezoneLabel')(timezones[0])).toEqual('GMT -11');
});
it('Proper value passed. Zero offset. Should return GMT +0', () => {
expect($filter('timezoneLabel')(timezones[1])).toEqual('GMT +0');
});
it('Proper value passed. Positive offset. Should return GMT +5', () => {
expect($filter('timezoneLabel')(timezones[2])).toEqual('GMT +5');
});
});
Importing fixtures:
fixture.setBase('test/mocks');
let mock = fixture.load('mock_file.json');
Service test example:
Refer to menu.srv.spec.js
$scope = rootScope.$new(); // also a simple empty object will do
let element = $compile('<div custom-directive')($scope);
it(`Should return 'Hello world'`, () => {
let scope = $rootScope.$new();
scope.value = '';
const element = $compile('<div custom-directive="value"></div>')(scope);
expect(element.text()).toBe('');
scope.value = 'Hello world';
$rootScope.$digest();
expect(element.text()).toBe('Hello world');
});
$scope = rootScope.$new(); // also a simple empty object will do
let CustomController = $controller('CustomController', { $scope });
am.controller('CustomController', function($scope, dependencyExample) => {});
const dependencyExample = { customMethod: (data) => data || [] };
let CustomController = $controller('CustomController', { $scope, dependencyExample });
$httpBackend.expectGET('/user').respond(fixture.load('user.json'));
// will return a fixture
$httpBackend.when('/user').respond(fixture.load('user.json'));
// will return success response
$httpBackend.when('/user').respond(200);
// will return error response
$httpBackend.when('/user').respond(500);
let rabbit = {
jump: (direction) => console.log(`Jump ${direction}!`);
}
spyOn(rabbit, 'jump');
rabbit.jump('up');
expect(rabbit.jump).toHaveBeenCalled();
expect(rabbit.jump).toHaveBeenCalledTimes(1);
expect(rabbit.jump).toHaveBeenCalledWith('up');
Links