How to import JSON file on Jest tests?

As decribed here: is there a require for json in node.js you can use:

import someObject from ('./somefile.json')

This should also work:

const testObject = require('../config/object');

However, while i was using jest for testing i got it working by saving the json with .js extension and inside it using module.exports. Then i destructured the object in my main file.

  • JSON file (object.js):

    module.exports = {
      "testObject":
      {
          "name": testName
          "surname": testSurname
      }
    }
    
  • Main File

    const { testObject } = require('./config/object');
    

Leave a Comment