48

I'm testing my application using Jest, but it's getting an error like:

SyntaxError: Unexpected token }

The line where the error is occurring is:

import { something } from "../my-json.json";

How can I import the JSON file on Jest tests?

2
  • If you using that json as mock you could also store it as object in js file and export it. Just easy solution :P Commented Jan 24, 2020 at 15:36
  • Does this answer your question? is there a require for json in node.js Commented Apr 18, 2020 at 2:56

8 Answers 8

24

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');
    
Sign up to request clarification or add additional context in comments.

1 Comment

Doesn't seem to work with XML files... SyntaxError: Unexpected token '<'
22

Works with Typescript / ts-jest 26.5 :

import * as myJson from './mock/MyJson.json';

...

const result = doAnyting(myJson);

2 Comments

japp, import myJson from './mock/MyJson.json'; returned undefined for me. Changing to import * as myJson from './mock/MyJson.json'; did the trick with Typescript and ts-node
Same here, was testing Angular service, which imports json. import * as myJson from './MyJson.json' saved me.
15

When using Typescript and Vue CLI, all I needed to do was to add resolveJsonModule to tsconfig.ts:

// tsconfig.ts

{
  "compilerOptions": {
    "resolveJsonModule": true,
  }
}

This in fact solves the loading of JSON files by Typescript in general.

Note: I am using Vue CLI so it is possible that some JSON related config is already preconfigured there.

Comments

6

You need to import json from 'filename.json'. Make sure that if you're using path alias you need to configure them on your jest.config.js file inside the ModuleNameMapper config.

Comments

2
const myJSON = require('./json_file_name.json')

Note that myJSON is already an object, no need to use JSON.parse

1 Comment

If its an object then why did you name it *JSON?
1
const someObject = require('./somefile.json')

Comments

1

Set "esModuleInterop": true, in tsconfig.spec.json compilerOptions.

Comments

0

You need to remove ',' before all the '}'. I found this solution by test and error.

2 Comments

Please be more descriptive.
The last attribute must not have a , after it

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.