Babel7 Jest unexpected token export

Salman Hasrat Khan

I am having issues in running jest tests in my project with Babel7. Tests used to transpile perfectly with babel6. It also compiles perfectly with webpack with Babel7 but fails to run tests with jest due to a transpilation error. What am I doing wrong?

react/node_modules/generic-redux-root/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from './source/CreateReduxRoot';
                                                                                         ^^^^^^

SyntaxError: Unexpected token export

my jest config

{
"unmockedModulePathPatterns": [
  "<rootDir>/node_modules/react",
  "<rootDir>/node_modules/react-dom",
  "<rootDir>/node_modules/react-addons-test-utils",
  "<rootDir>/node_modules/fbjs",
  "enzyme"
],
"roots": [
  "<rootDir>/__tests__"
],
"transformIgnorePatterns": [
  "node_modules/(^generic-)/i", //a module matching this is throwing an error
  "node_modules/react-infinite-scroller"
],
"setupFiles": [
  "./jestsetup.js"
],
"snapshotSerializers": [
  "enzyme-to-json/serializer"
],
"testResultsProcessor": "./jestTrxProcessor",
"verbose": true

}

My .babelrc

{
"presets": [
    [
        "@babel/preset-env",
        {
            "targets": {
                "ie": 11
            },
            "useBuiltIns": "usage"
        }
    ],
    "@babel/preset-react"
],
"plugins": [
    "@babel/plugin-transform-runtime",
    "@babel/plugin-syntax-dynamic-import",
    "@babel/plugin-syntax-import-meta",
    "@babel/plugin-proposal-class-properties",
    "@babel/plugin-proposal-json-strings",
    [
        "@babel/plugin-proposal-decorators",
        {
            "legacy": true
        }
    ],
    "@babel/plugin-proposal-function-sent",
    "@babel/plugin-proposal-export-namespace-from",
    "@babel/plugin-proposal-numeric-separator",
    "@babel/plugin-proposal-throw-expressions",
    "@babel/plugin-proposal-object-rest-spread",
    "@babel/plugin-transform-object-assign"
]

}

What am I doing wrong?

Pavel Denisjuk

This is happening because Babel 7 no longer loads your .babelrc automatically. There is a new concept of root config which should be located in the root of your project and the file must be named babel.config.js and export an object.

So to give you some steps to follow:

  1. rename your .babelrc to babel.config.js and make sure you use module.exports = {...}
  2. run jest --clearCache to clear Jest internal cache (which costed me a few hours of banging my head against the wall)
  3. At this point your babel config should be loaded correctly by Jest

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Unexpected token export with jest

Jest: SyntaxError: Unexpected token 'export'

Jest SyntaxError: Unexpected token export

Jest: unexpected token export with react-navigation

Stenciljs and Jest. Unexpected token "export"

Jest setup "SyntaxError: Unexpected token export"

JEST with Angular SyntaxError: Unexpected token 'export'

Jest encountered an unexpected token - SyntaxError: Unexpected token 'export'

Jest Unexpected token 'export' when using d3

How to fix vue-jest error - SyntaxError: Unexpected token 'export'

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

Babel 6 CLI: Unexpected token export?

Jest encountered an unexpected token (React, Typescript, Babel, Jest & Webpack setup)

jest/puppeteer - unexpected token

Jest encountered an unexpected token

Jest: "SyntaxError: Unexpected token {"

Jest - Unexpected token import

Jest: Unexpected token @ Decorator

react export Unexpected token

"SyntaxError: Unexpected token export(…)"

Unexpected token export

Webstorm Unexpected Token export

Unexpected token 'export'

SyntaxError: Invalid or unexpected token when testing react application with babel and jest

Can not run unit tests through jest framework because of SyntaxError: Unexpected token export

jest + typescript + es6 modules (yet again, 2019) - SyntaxError: Unexpected token export

jest moduleNameMapper fixes unexpected token 'export' now breaks relative module import

SyntaxError: Unexpected token import with Jest

Jest encountered an unexpected token in typescript