@collectivehealth/unity

A library to make unit testing Angular 1 applications suck less.

Stats

StarsIssuesVersionUpdatedCreatedSize
@collectivehealth/unity
101.3.04 years ago5 years agoMinified + gzip package size for @collectivehealth/unity in KB

Readme

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', () => {
    // T.$scope is the controller's scope.
    // T.MyCtrl is the controller instance.
    // Use get('$document') and get('$location') to interact with other injectables.
  });
});
  • 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! 🎉

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.