4

I have a NodeJS project using Jest and babel. When I run the test I get the following error:

FAIL ./user.test.js ● Test suite failed to run. SyntaxError: C:\Users\...\__test__\mocks\registerUser.json: Unexpected token, expected ";" (2:8) When i change the registerUser.json to a javascript file I'm able to retrieve the content without any problem, but when I try to use a JSON file won't work. But I can't seem to find the cause of the error when using a json file.

My current structure is something like:

|-- src
|-- __test__
     |-- mocks
        |-- registerUser.json
     |-- user.test.js
|-- tmp
.babelrc
.eslintignore
.eslintrc
.gitignore
.prettierrc
jest.config.js
package.json

user.test.js

const userRegisterMock = require('./mocks/registerUser.json');

describe('User', () => {
  it('[SUCCESS] should be able to register', async () => {
    // console.log(userRegisterMock)
    expect(true).toBe(true);
  });
});

jest.config.js

    module.exports = {
      bail: 1,
      testEnvironment: 'node',
      clearMocks: true,
      collectCoverage: true,
      collectCoverageFrom: [
        'src/**',
        '!src/helpers/**',
        '!src/app.js',
        '!src/server.js',
        '!src/database/**',
        '!src/config/**'
      ],
      coverageDirectory: '__tests__/coverage',
      coverageReporters: ['text', 'lcov'],
      coverageThreshold: {
        global: {
          branches: 100,
          functions: 100,
          lines: 100,
          statements: 100
        }
      },
      moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'node'],
      resetModules: false,
      testMatch: ['**/__tests__/**/*.test.js'],
      transform: {
        '.(js|jsx|ts|tsx)': 'babel-jest'
      }
    };

.babelrc

{
  "presets": [
    ["@babel/preset-env", {
      "useBuiltIns": "usage", // or "entry"
      "corejs": 3,
      "targets": {
        "node": "current"
      }
    }]
  ],
  "plugins": [
    ["@babel/plugin-transform-modules-commonjs"],
    ["@babel/plugin-transform-runtime", {
      "regenerator": true
    }]
  ]
}

registerUser.json

{
  "name": "Test Case",
  "country": "Brazil",
  "birthdate": "25/03/1994",
  "sex": "Masculino",
  "nationality": "Brasileiro"
}
5
  • Isn't your mock directory mock not mocks? Commented May 2, 2020 at 5:47
  • It was a typing, it's mocks instead of mock. But the error still remains! Commented May 2, 2020 at 6:26
  • What version of jest you are on? If it's 24.*, json transform is not supported github.com/facebook/jest/issues/8426 Commented May 2, 2020 at 6:41
  • I'm using version 25.4.0 Commented May 2, 2020 at 13:53
  • Could you make a GitHub repository to reproduce, @bsantoss? Commented May 3, 2020 at 5:48

3 Answers 3

1

Your transform regex contains .js which matches `.json

If you add an $ in the end, you'll select only files ending with .js.

transform: {
  '.(js|jsx|ts|tsx)$': 'babel-jest'
}

An even better regex would be: '^.+\\.(js|jsx|ts|tsx)$': 'babel-jest'

Sign up to request clarification or add additional context in comments.

1 Comment

And that's just another thing that can be solved with enough $! Hahahaha
0

I think one thing you will need to do to start with is to add a semicolon to the top line of user.test.js. It should look like this:

const userRegisterMock = require('./mocks/registerUser.json');

2 Comments

This doesn't interfere in jest compilation.
This doesn't interfere in jest compilation.
0

Looks like they fixed it recently: https://github.com/jestjs/jest/pull/14048

The error was with Babel trying to transform the json file.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.