Why is Karma refusing to serve my JSON fixture (which I'd like to use in my jasmine / angularjs tests)

后端 未结 3 1791
清酒与你
清酒与你 2021-01-06 09:40

As indicated in this stackoverflow answer, it looks like Karma will serve JSON fixtures. However, I\'ve spent too many hours trying to get it to work in my environment. Re

3条回答
  •  日久生厌
    2021-01-06 09:56

    I also needed json fixtures in my karma test suite. I ended up just using the html2js preprocessor with json files as well as html.

    karma.conf.js:

    module.exports = function (config) {
      config.set({
        frameworks: ["jasmine"],
        files: [
            '**/*.js',
            '**/*.html',
            '**/*.json',
            '**/*.spec.js'
        ],
        plugins: [
            'karma-html2js-preprocessor'
        ]
        preprocessors: {
            '**/*.html': ['html2js'],
            '**/*.json': ['html2js']
        }
      });
    };
    

    Then it is just a matter of getting the json from the __html__ global.

    e.g.

    var exampleJson = __html__['example.json'];
    var jsonObj = JSON.parse(exampleJson);
    var exampleHtml = __html__['example.html'];
    document.body.innerHTML = exampleHtml;
    

提交回复
热议问题