karma-ng-request2js-preprocessor

A Karma plugin. Save AngularJS $http JSON requests to JavaScript


Keywords
karma-plugin, karma-preprocessor, angularjs, angular, json2js, mock
License
BSD-2-Clause
Install
npm install karma-ng-request2js-preprocessor@0.0.1

Documentation

karma-ng-request2js-preprocessor

A Karma plugin. Preprocess JSON files for loose (black-box) unit testing.

Installation

npm install karma-ng-request2js-preprocessor --save-dev

Configuration

Example:

// karma.conf.js
module.exports = function(config) {
  config.set({
    preprocessors: {
      '**/*.json': ['ng-request2js']
    },
    ngRequest2JsPreprocessor: {
      // setting this option will create only a single module that
      // contains the JSON from the files.  So you'll need to load
      // this with module('jsons')
      moduleName: 'jsons',
      stripPrefix: '',
      prependPrefix: '/'
    },
    files: [
      '*.json'
    ]
  });
};

How does it work?

The plugin preprocesses the JSON file for you.

For example.

// foo.json
{
  "bar": "baz"
}

When loaded, the plugin puts the JSON files into $httpBackend:

$httpBackend.when('GET', 'foo.json').respond({bar: "baz"});