MonsieurOurs
MonsieurOurs

Reputation: 41

Jest encounters an unexpected token inside dependency

I've been trying to debug the dreaded unexpected token bug for quite some time now and I'm no closer to finding a solution. Here's the stacktrace I've been getting:

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    [PROJECT]/node_modules/[LIBRARY]/node_modules/@babel/runtime/helpers/esm/taggedTemplateLiteral.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export default function _taggedTemplateLiteral(strings, raw) {
                                                                                             ^^^^^^

    SyntaxError: Unexpected token 'export'

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
      at Object.<anonymous> (node_modules/[LIBRARY]/dist/index.js:82:54)

Here's the relevant parts of my jest config:

{
  "moduleDirectories": [
    "node_modules",
    "src"
  ],
  "transformIgnorePatterns": [
    "node_modules/(?!(imask|[LIBRARY])/)"
  ]
}

And the babel config:

{
  "presets": ["@babel/preset-env", "@babel/preset-react", "@babel/preset-typescript"],
}

As far as I can tell, I'm doing everything I should be doing to have that library's files transpiled by babel. Yet, I'm still running into this error. Anyone know what I should try? Thanks in advance.

Upvotes: 0

Views: 1533

Answers (1)

MonsieurOurs
MonsieurOurs

Reputation: 41

I figured it out. Babel won't transpile the nested dependency since it applies the pattern at each level it sees a node_modules folder. I had to modify my transformIgnorePatterns entry to look like this:

transformIgnorePatterns: ["node_modules/(?!(imask|[LIBRARY]|@babel)/)"]

Upvotes: 2

Related Questions