unity
Unity is a library that helps you write shorter, cleaner unit tests for your Angular 1.x applications by eliminating much of the boilerplate requried to set up a test suite and by providing utility functions for common tasks.
Requirements
- Angular
>=1.3.0
. - If you run your tests in a browser-like environment (ex: Karma + PhantomJS):
- You may need an ES6 polyfill, like
babel-polyfill
. - You will need to run a module-bundler on your test files.
- If you run your tests in Jest, Unity should Just Work!
Install
yarn add --dev @collectivehealth/unity
or
npm install --save-dev @collectivehealth/unity
Example
Before
let $controller;
let $document;
let $location;
let $q;
let $scope;
describe('MyController', () => {
beforeEach(module('MyApp'));
beforeEach(inject((_$controller_, _$document_, _$location_, _$q_) => {
$controller = _$controller_;
$document = _$document_;
$location = _$location_;
$q = _$q_;
$scope = {};
}));
it('should work', () => {
let myCtrl = $controller('MyCtrl', {
$scope: $scope
});
});
});
- Multiple shared variables that are re-used across specs.
- Syntax is verbose.
- Three locations need to be updated to get access to an injectable. 😿
After
import {
controller,
get,
module
} from '@collectivehealth/unity';
describe('MyCtrl', () => {
let T;
beforeEach(() => {
module('MyApp');
T = controller('MyCtrl');
});
it('should work', () => {
});
});
- One shared variable across specs, regardless of how many injectables are being used.
- Syntax is terse and readable.
- Zero boilerplate to access an injectable, just use
get()
wherever you need it! 😺
For more information, check out the documentation. Happy testing! 🎉