How to import json data into tests (without native)

One of the uses for native code that I have is to enable tests to use static json data (that is my code accesses via http). An idea I found to make it available for testing was

var _user$project$Native_TestJson = function () {
    var fs = require('fs');
    var path = require('path');

    var jsonPath = path.join(__dirname, '../../../..', 'src/assets/data/primitives.json');
    var getPrimitives = JSON.parse(fs.readFileSync(jsonPath, 'utf8'));

    return {
        getPrimitives: getPrimitives
    }
}();

This works great, but I’m wondering how this need is supported in 0.19?

2 Likes

Would you be able to just use a module that contained the JSON data? E.g.

module TestJson exposing (..)

primitives : String
primitives =
    """ 
{
  "name": "tom",
  "info": {
    "age": 42,
    "height": 1.8
  }
}
"""

I tested importing this and using decodeString in the elm-repl and it works fine.

yes, that’s an option but I need to be able to load and edit about 40 such json structures. I would hope to use the webpack dev server to support basic CRUD via the app itself to load and update the test data, and the idea of do string manipulations to a giant .elm file does not excite me.

1 Like