Socket
Book a DemoInstallSign in
Socket

karma-json-preprocessor

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

karma-json-preprocessor

A Karma plugin. Convert JSON files into JS variables to use in your tests.

0.3.3
latest
Source
npmnpm
Version published
Maintainers
1
Created
Source

karma-json-preprocessor

Preprocessor for converting JSON files into JS variables.

Installation

Using npm:

npm install karma-json-preprocessor --save-dev

Configuration

Following code shows the default configuration:

// karma.conf.js
module.exports = function(config) {
  config.set({
    preprocessors: {
      '**/*.json': ['json']
    },

    files: [
      '**/*.js',
      '**/*.json'
    ]
  });
};

How does it work ?

This preprocessor converts JSON files into JS variables and publishes them in the global window.__json__, so that you can use these to externalize your json fixtures.

Note that your files will be validated with JSON.parse and ignored if parsing throw an error.

For instance this foo.json

{
  "id": 1,
  "name": "foo"
}

This file will be served as foo.json.js and available in global window.__json__:

console.log(window.__json__['foo.json']);
console.log(window.__json__['foo.json'].id);
console.log(window.__json__['foo.json'].name);

This preprocessor will publish javascript on a global variable: it means that if you update an object, theses updates will never be reverted. If your tests do not alter objects, you don't have to worry about this, otherwise, you should clone objects before using it.

Since version 0.3.0, you can also use the $get function to use a clone of the original object:

console.log(window.__json__.$get('foo.json'));

// Following line will display 'true'
// Objects are equals, but new instance is returned each time.
console.log(window.__json__.$get('foo.json') !== window.__json__.$get('foo.json'));

Configuration

Default global variable name is by default __json__, but you can override it with your own name in karma configuration:

// karma.conf.js
module.exports = function(config) {
  config.set({
    preprocessors: {
      '**/*.json': ['json']
    },

    files: [
      '**/*.js',
      '**/*.json'
    ],
    
    jsonPreprocessor: {
      varName: '$json'
    }
  });
};

And now in your test:

console.log(window.$json['foo.json']);
console.log(window.$json['foo.json'].id);
console.log(window.$json['foo.json'].name);

For more information on Karma see the homepage.

Keywords

karma-plugin

FAQs

Package last updated on 17 Feb 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.