karma-file-fixtures-preprocessor

3.0.2 • Public • Published

karma-file-fixtures-preprocessor

NPM GitHub Workflow Status (master) Codacy License: MIT Sponsor this project

A Karma plugin that makes file content accessible within test environments.

Installation

npm install karma-file-fixtures-preprocessor --save-dev

Usage

Given the following directory structure:

app
├── tests
│   ├── fixtures
│   │   ├── fixture.css
│   │   ├── fixture.html
│   │   ├── fixture.json
│   │   └── fixture.txt
│   └── app.test.js
├── karma.conf.js
└── package.json

Specify fixture files using Karma's preprocessors configuration and plugin options as a fileFixtures object:

// karma.conf.js
module.exports = function(config) {
  config.set({
    // ...
    files: [
      'app/tests/*.test.js'
    ],
    preprocessors: {
      'app/tests/fixtures/*': ['file-fixtures']
    },
    fileFixtures: {
      // Options...
    }
  });
};

Fixture content is stored as a global object accessible in your test environment:

// app.test.js
console.log(window.__FIXTURES__);
// Output
{
  "tests/fixtures/fixture.css": "body { color: red; }",
  "tests/fixtures/fixture.html": "<p>Hello</p>",
  "tests/fixtures/fixture.json": "{ \"a\": 1 }",
  "tests/fixtures/fixture.txt": "Some Text"
}

Fixtures can be used directly in test assertions:

var assert = require('assert');
var txt = window.__FIXTURES__['tests/fixtures/fixture.txt'];

// Assertion
assert.strictEqual(txt, 'Some Text', 'Text content matches!');

Or injected, parsed, and modified as needed:

var css  = window.__FIXTURES__['tests/fixtures/fixture.css'];
var html = window.__FIXTURES__['tests/fixtures/fixture.html'];
var json = window.__FIXTURES__['tests/fixtures/fixture.json'];

// Injecting / Parsing
before(function() {
  // Inject CSS
  document.body.insertAdjacentHTML('beforeend', '<style>' + css + '</style>');
  console.log(getComputedStyle(document.body).color);

  // Inject HTML
  document.body.insertAdjacentHTML('beforeend', html);
  console.log(document.querySelector('p').textContent);

  // Parse JSON
  json = JSON.parse(json);
  console.log(json.a);
});
// Output
"rgb(255, 0, 0)"
"Hello"
1

Options

globalName

  • Type: string
  • Default: '__FIXTURES__'

Sets the name of the global fixtures object.

Example

// karma.conf.js
module.exports = function(config) {
  config.set({
    // ...
    fileFixtures: {
      globalName: '__FIXTURES__' // default
    }
  });
};

stripPrefix

  • Type: string
  • Default: null

Removes the specified string from each fixture key.

Example

// karma.conf.js
module.exports = function(config) {
  config.set({
    // ...
    fileFixtures: {
      stripPrefix: null // default
    }
  });
};

transformKey

  • Type: function
  • Arguments:
    1. path: The path of the current fixture file

Callback after each fixture file is loaded. Allows modifying the object key used to access the file content in the global fixtures object.

Example

// karma.conf.js
module.exports = function(config) {
  config.set({
    fileFixtures: {
      transformKey: function(path) {
        return path;
      },
    }
  });
};

transformContent

  • Type: function
  • Arguments:
    1. path: The path of the current fixture file
    2. content: The text content of the current fixture file

Callback after each fixture file is loaded. Allows modifying the file content stored in the global fixtures object.

Example

// karma.conf.js
module.exports = function(config) {
  config.set({
    fileFixtures: {
      transformContent: function(path, content) {
        return content;
      }
    }
  });
};

Sponsorship

A sponsorship is more than just a way to show appreciation for the open-source authors and projects we rely on; it can be the spark that ignites the next big idea, the inspiration to create something new, and the motivation to share so that others may benefit.

If you benefit from this project, please consider lending your support and encouraging future efforts by becoming a sponsor.

Thank you! 🙏🏻

Contact & Support

  • Follow 👨🏻‍💻 @jhildenbiddle on Twitter and GitHub for announcements
  • Create a 💬 GitHub issue for bug reports, feature requests, or questions
  • Add a ⭐️ star on GitHub and 🐦 tweet to promote the project
  • Become a 💖 sponsor to support the project and future efforts

License

This project is licensed under the MIT License. See the MIT LICENSE for details.

Copyright (c) John Hildenbiddle (@jhildenbiddle)

Package Sidebar

Install

npm i karma-file-fixtures-preprocessor

Weekly Downloads

133

Version

3.0.2

License

MIT

Unpacked Size

12.2 kB

Total Files

4

Last publish

Collaborators

  • jhildenbiddle