1

I want to use some dummy data in an angular unit test. The data is a large javascript array of objects. I'd rather not have this object in my unit test. Is there a way I can load this data into my unit test? In effect, I want the unit test to "include" another file which contains the data. Is this possible? Thanks

2 Answers 2

1

I created a globals.js file which I include in my karma.conf.js and it looks like this:

module.exports = function(config) {
  config.set({

    // base path, that will be used to resolve files and exclude
    basePath: "..",

    // frameworks to use
    frameworks: ["jasmine"],

...
files: [
      "test/unit/globals.js",
      "app/js/*/**",
]
...
});
};

and in my globals.js file I just create the dummy data like this:

var globals = {
var1: 'mockValue1',
var2: 'mockValue2',
...
}

And in my unit tests I just write var something = globals.var1; if I want to use it there :)

Sign up to request clarification or add additional context in comments.

Comments

0

You should use karma-ng-json2js-preprocessor.

It allows you to do exactly what you want. Have your JSONs in a separate .json file and then inject those mock jsons as angular constants into your tests.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.