Angular objects o'fixtures

Eric Stolten

In an AngularJS project at work, I found myself dealing with large JSON fixtures in my tests. You write tests, right? right!? I wanted something that would require less module includes and allow me to group fixtures into one object.

Announcing

karma-fixture-to-angular-module-preprocessor

Check the project out on npm or Github. Comments & pull requests are welcome.

Use it

After you have configured the plugin, you can take a directory of JSON files and and return them in an object of your choosing.

$ tree
├── placeholder.js
├── AuthFailure.json
├── AuthSuccess.json
└── LoginResponse.json

With an example configuration like this:

jsonDirToJs: {
  'path/to/placeholder.js': {
    moduleName: 'my.test.fixtures.auth',
    dir: 'path/to',
    objectName: 'MockAuth'
  }
}

Now you can easily access your fixtures.

var MockAuth,
    login,
    authSuccess,
    authFail;

beforeEach(function () {
  module('my.test.fixtures.auth');
});

inject(function($injector){
  MockAuth = $injector.get('MockAuth');
  login = MockAuth.LoginResponse;
  authSuccess = MockAuth.AuthSuccess;
  authFailure = MockAuth.AuthFailure;
});


Happy coding.