jest: Test suite failed to run, SyntaxError: Unexpected token import

Miha Šušteršič :

This is my jest configuration from the package.json file:

"jest": {
    "automock": false,
    "browser": true,
    "moduleNameMapper": {
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "./app/tests/mocks/FileMock.js",
      "\\.(css|less)$": "identity-obj-proxy"
    },
    "moduleFileExtensions": [
      "js",
      "jsx"
    ],
    "moduleDirectories": [
      "node_modules"
    ],
    "transform": {
      "^.+\\.jsx?$": "./node_modules/babel-jest",
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "./app/tests/mocks/FileTransformer.js"
    },
    "testEnvironment": "jsdom",
    "testPathDirs": [
      "./app/tests"
    ],
    "testRegex": ".*.test.js",
    "verbose": true
  }

And the .babelrc file located in my root folder:

{
  "plugins": ["syntax-dynamic-import", "transform-runtime"],
  "presets": [
    [
      "es2015",
      {
        "modules": false
      }
    ],
    "react",
    "stage-0"
  ],
  "env": {
    "start": {
      "presets": [
        "react-hmre"
      ]
    }
  }
}

According to the documentation found at the jest getting started page this is everything I need for babel to work it's magic.

Regardless, this test:

import React from 'react';
import {shallow} from 'enzyme';
import Landing from '../components/Landing.component';

describe('<Landing/>', () => {
  it('should render a header to the page', () => {
    const landing = shallow(<Landing/>);
    expect(landing.find('h1').text()).toBe('This is the Landing component');
  });
});

returns:

FAIL  app/tests/Landing.component.test.js   
 ● Test suite failed to run

   /Users/shooshte/PersonalProjects/surviveJS/app/tests/Landing.component.test.js:1
   ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import React from 'react';
                                                                                            ^^^^^^
   SyntaxError: Unexpected token import

     at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:320:12)

What am I doing wrong?

Miha Šušteršič :

Jest sets the env variable to test, so I had to add my presets to the env setting in .babelrc:

{
  "plugins": ["syntax-dynamic-import", "transform-runtime"],
  "presets": [
    [
      "es2015",
      {
        "modules": false
      }
    ],
    "react",
    "stage-0"
  ],
  "env": {
    "start": {
      "presets": [
        "react-hmre"
      ]
    },
    "test": {
      "presets": ["es2015", "react", "stage-0"]
    }
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Jest: Test suite failed to run, Unexpected token =

React, semantic-ui-react. Test suite failed to run: SyntaxError: Unexpected token import

● Test suite failed to run Jest encountered an unexpected token export { BsModalRef }

SyntaxError: Unexpected token import with Jest

Test Vue with Jest fail, Jest encountered an unexpected token, SyntaxError: Unexpected token import

Jest Test suite failed to run

Jest test fail: SyntaxError: Unexpected token <

React + Jest: Test suite failed to run

Jest: "SyntaxError: Unexpected token {"

[React-Native][Jest]SyntaxError: Unexpected token import

Dynamic imports in Jest produce: SyntaxError: Unexpected token import

Jest not parsing es6: SyntaxError: Unexpected token import

How to fix "Test suite failed to run" when running a test with jest

Jest: Test suite failed to run (The Expo SDK requires Expo to run)

Jest - Unexpected token import

Jest: SyntaxError: Unexpected token 'export'

Jest SyntaxError: Unexpected token export

Jest + Vue - SyntaxError: Unexpected token <

Jest error: Test suite failed to run, how to solve?

Jest Error - Test Suite Failed To Run - Unknown option: .preset

'● Test suite failed to run'

SyntaxError: Unexpected token import - Express

SyntaxError: Invalid or unexpected token @import

ava: SyntaxError: Unexpected token import

Uncaught SyntaxError:` Unexpected token import

SyntaxError: Unexpected token import - reactjs

React Jest - Unexpected token import

Jest css-modules SyntaxError: Unexpected token

Jest setup "SyntaxError: Unexpected token export"