karma-fixtures-preprocessor

Karma plugin that puts the content of fixture files in a __FIXTURES__ global array.

Usage no npm install needed!

<script type="module">
  import karmaFixturesPreprocessor from 'https://cdn.skypack.dev/karma-fixtures-preprocessor';
</script>

README

karma-fixtures-preprocessor

Reads the content of your fixture files and constructs a global array, __FIXTURES__, that contains the content of those files.

Installation

npm install karma-fixtures-preprocessor --save

Note: Karma will automatically see and include plugins whose names start with karma-*. However, it is possible that, in your IDE or console configurations, you're running Karma as a global Node module. In that case, you'll have to install this plugin too globally, i.e npm install karma-fixtures-preprocessor -g.

Configuration

// karma.conf.js
module.exports = function(config) {
  config.set({
    files: [
      'test/fixtures/**/*.json' // The location of your fixture files in relation to this, karma.conf.js, file.
    ],

    preprocessors: {
      'test/fixtures/**/*.json': ['fixtures'] // Again, the location of your fixture files in relation to this, karma.conf.js, file.
    },
  })
}

That's all. This plugin doesn't provide any other configurable options.

How does it work?

The plugin will read the content of each of your fixture files and push its content, as-is, to a global array called __FIXTURES__. Because the content is pushed into the array as-is, you have to make sure that whatever is in your fixture files is valid JavaScript value.

We use this plugin in our own AngularJS project at AppAdvice to construct the __FIXTURES__ array from multiple JSON files. Then in the specHelper.js of our Jasmine tests, we do:

angular.forEach(__FIXTURES__, function(fixture) {
  $httpBackend['when' + fixture.httpVerb](fixture.url).respond(fixture.response)
})

LICENSE

MIT - Copyright (C) AppAdvice.