Jest 在依赖项中遇到意外标记

Jest encounters an unexpected token inside dependency

我已经尝试调试可怕的意外令牌错误已有一段时间了,但我离找到解决方案还差得很远。这是我得到的堆栈跟踪:

    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)

这是我的 jest 配置的相关部分:

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

以及 babel 配置:

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

据我所知,我正在尽我所能让 babel 转译该库的文件。然而,我仍然 运行 陷入这个错误。任何人都知道我应该尝试什么?提前致谢。

我明白了。 Babel 不会转译嵌套的依赖项,因为它在它看到 node_modules 文件夹的每个级别应用模式。我必须将 transformIgnorePatterns 条目修改为如下所示:

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