grunt-fixtures2js

Convert your fixtures into a JS object for use in tests.

grunt-fixtures2js

Ever wondered how to access static text-based files (e.g. JSON, HTML) from your tests? grunt-fixtures2js makes things easier for you by creating a JS file out of your fixtures, containing an object where file contents mapped to their filenames. You can then just include that file in your tests and voilá!

You can install grunt-fixtures2js via npm:

$ npm install --save-dev grunt-fixtures2js

You can configure your fixtures2js tasks as follows:

fixtures2js: {
    default: {
        files: {
            "my-fixture-file.js": "fixtures/*"
        }
    }
}
  • head (defaults to window.FIXTURES =) a string to insert before the generated JSON.
  • tail (defaults to ;) a string to insert after the generated JSON.
  • postProcessors (defaults to {}) a string to string object where keys are minimatch patterns and values are one of default, json, base64 or bytearray. If a file is not matched by any pattern, default is used.

If you have a configuration like this:

fixtures2js: {
    default: {
        options: {
            head: "processFixtures(",
            tail: ");",
            postProcessors: {
                "**/*.json": "json"
            }
        },
        files: {
            "my-fixture-file.js": "fixtures/*"
        }
    }
}

Running grunt fixtures2js:default will read the contents of the files in the fixtures/ folder, and generate a my-fixture-file.js like this:

processFixtures({
    "fixtures/one-fixture-file.txt": "The contents of the fixture file here",
    "fixtures/other-fixture-file.txt": "The contents of another fixture file here",
    "fixtures/stuff.json": {
        "foo": "bar"
    }
});