How to load mock data from JSON file in Angular 2 karma jasmine test?

喜欢而已 提交于 2019-12-04 02:58:23

I had the same issue!

Finally, I realized that just using the require() function directly in TypeScript works just fine. It is supported by Node and @types/node, otherwise some need to declare require types.

So to load mock data from JSON file in Angular 2 Karma Jasmine test, go for:

const data: any = require('../../assets/mock-data.json');

PS: credits to Artur Ampilogov

i'm trying to do the same, system.import works if you pass path of the JSON like this

example :

`System.import('../../assets/mock-data/mock-data.json')
.then((json) => {
console.log(json);
});`

what is really weird, is that I didn't manage to make it work with variables. example :

`let myUrl: string = '../../assets/mock-data/mock-data.json';
    System.import(myUrl)
    .then((json) => {
        console.log(json);
    });`
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!