Import static json into test file

Hey,

Just starting with Ember.

Trying to mock some data with prepared JSON. It’s theoretically possible, according to SO to import json into JS with Babel using following syntax:

1.  import * as data from './example.json';
2.  word = data.name;
3. console.log(word); // output 'testing'

But keep getting error:

Could not find module .../tests/acceptance/example.json imported from .../tests/acceptance/page-report-test

ps. Workaround works for me:

A simple workaround from S:

config.js

export default
{
  // my json here...
}

then…

import config from '../config.js'

does not allow import of existing .json files, but does a job.

Thanks in advance

As a more robust solution to this problem I’d highly recommend using ember-cli-mirage. It supports both static (fixture) and dynamic data creation for tests and also supports making a full fake “backend” that can handle ajax requests and so on. It’s super easy to use and can really speed along test writing and even development and prototyping (you can use it in development mode too).

As to your original issue though as mentioned in the SO answers ES6 doesn’t really allow this so if you need to import fixture data I’d use the “workaround” by putting it in a js file that exports it.

1 Like

never been a fan of mirage myself, just feels like every app I’ve encountered it in it had become the bottleneck to fix. I’m finding this issue tonight literally trying to find the best way to load a json file into a mirage database (which I previously dumped) because loading it in via json takes a few milliseconds while allowing mirage to generate the same data takes a full minutes.

It’s frustrating ember-cli still doesn’t support .json imports. I can convert this to a module but it’s a much more dangerous parse time.