npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

karma-ng-json2js-preprocessor-requirejs

v0.1.4

Published

A Karma plugin. Compile JSON files to Angular modules on the fly. Includes support for require.js.

Downloads

26

Readme

Build Status NPM version

karma-ng-json2js-preprocessor-requirejs

=======================================

A Karma preprocessor for adding JSON files as values to AngularJS modules on the fly (with RequireJS support)

Installation

Run the following command from your shell

npm install karma-ng-json2js-preprocessor-requirejs --save-dev

This will add a dependency like the following to your 'package.json' file

{
  "devDependencies": {
    "karma": "~0.10.9",
    "karma-ng-json2js-preprocessor-requirejs": "~0.1.1"
  }
}

Configuration

Add the following configuration values to your Karma configuration file


    preprocessors: {
      '**/*.json' : 'ng-json2js'
    },

    // Assuming your JSON files are in a 'stubs' directory
    files: [
      {pattern: 'stubs/*.json', included: false}
    ],

    ngJson2JsPreprocessor: {

      // Specify a module name if you would like all files to be added as values to a single module
      moduleName : "stubs",

      // Enable / Disable RequireJS support (default is true)
      enableRequireJs : true,

      // strip this from the file path
      stripPrefix: 'test/fixture/',

      // prepend this to the file path
      prependPrefix: 'served/',

      /* or define a custom transform function
      cacheIdFromPath: function(filepath) {
        return cacheId;
      }
      */
    }
  });
};

How does it work ?

This preprocessor converts JSON files into Angular values. Then, it adds these values as to Angular modules. The name of the value is the full path of the file converted to camelcase.

If you specify a moduleName in your Karma config, these values are all added to one module named after the value in your Karma config file. If you do not specify a module name, all files are added to their own module named after the full path of the file.

For example, suppose you have this file: test/stubs/data.json ...

{
    prop: val
}

The configuration listed above wll result in a single Angular module created called 'stubs' with a value added to it called 'testStubsData'. The value of this Angular value will be the content of the JSON file.

Based on the configuration above, the following code is run during preprocessing for a file in test/stubs/data.json:

require(['angular'], function(angular) {
   (function (module) {
        try {
            module = angular.module('stubs');
        } catch {
            module = angular.module('stubs', []);
        }

        module.value('testStubsData', {
            prop: 'val'
        });
    })();
});

Setting the enableRequireJs property to false will result in the above wrapping require block to be removed.

Usage

Once configured, you then need to do 3 things to get the JSON values into your test:

  1. Require the module file using the path of the JSON file you need to inject
  2. Load the module using Angular mocks
  3. Read the value from your module

For example, using the above configuration, this is what a sample test would look like:

describe('Widget Service Test', function () {
    var angular = require('angular');
    var mocks = require('angular-mocks');

    // Require the module containing the service you're testing as well as the name of the file you need to inject
    var widgetModule = require('modules/widgets');
    var data = require('test/stubs/data.json');

    var sut;

    var $httpBackend;
    var $rootScope;
    var mockData;

    // Create the modules
    beforeEach(mocks.module(widgetModule.name));
    beforeEach(mocks.module('stubs'));

    // The injected name of the value will be the full path of the file in camelCase (i.e. here its testStubsData)
    beforeEach(mocks.inject(function (_$rootScope_, _$httpBackend_, _$q_, WidgetService, testStubsData) {
        sut = WidgetService;
        $httpBackend = _$httpBackend_;
        $rootScope = _$rootScope_;
        mockData = testStubsData;
    }));


    it('should set the widgets property', function () {

        $httpBackend.when('GET', 'url/of/getWidgets/request').respond(JSON.stringify(mockData));

        sut.getWidgets();

        $httpBackend.flush();
        expect(sut.widgets).toBe(mockData);
    });

});

Contributing

To continuously run tests during development, run karma start.

Travis build information can be found here

For more information on Karma see the homepage.