Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

karma-file-fixtures-preprocessor

jhildenbiddle532MIT3.0.2

A Karma plugin that makes file content accessible within test environments

css, file, files, fixture, fixtures, html, json, karma-plugin, karma-preprocessor, karma, plugin, preprocessor, text, txt

readme

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)

changelog

Change Log

3.0.2

2024-02-06

  • Fix GitHub workflow badge

3.0.1

2019-12-03

  • Fixed check for duplicate paths in configuration

3.0.0

2019-09-12

  • Fixed handling of end-of-line (EOL) control characters in files/fixtures. This change necessitates a major version bump as multi-line fixtures will differ from the previous version.
  • Fixed warning message displayed when path specified in config.preprocessors is also specified in config.files

2.0.0

2019-02-01

  • Added unit and CI tests.
  • Removed the stripBasePath option for better cross-platform development. The base path is still removed from fixture keys, it is just no longer optional.
  • Fixed file handling to allow specifying fixture paths only in the preprocessors section. Previously these files would also have to be included in the files section of a karma configuration.

1.0.2

2019-02-01

  • Fixed removal of base path from fixture keys in Windows

1.0.1

2019-01-08

  • Update README and keywords

1.0.0

2019-01-03

  • Initial release