【问题标题】:Unexpected token export with jest开玩笑的意外令牌导出
【发布时间】:2019-03-10 03:38:54
【问题描述】:

我已经看到了一些其他的答案和 GitHub 问题来描述这一点,但我无法在我的案例中找到解决方案。当我试图开玩笑时,我得到了可怕的SyntaxError: Unexpected token export

project/node_modules/@agm/core/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from './directives';

运行这些测试的代码运行良好。该错误似乎来自开玩笑本身。 似乎好像开玩笑正在转换文件,但我可能是错的。

我开玩笑的配置是

"jest": {
  "preset": "jest-preset-angular",
  "setupTestFrameworkScriptFile": "<rootDir>/setup-jest.ts",
  "transformIgnorePatterns": [
    "node_modules"
  ]
}

我尝试将transformIgnorePatterns 更新为"&lt;rootDir&gt;/node_modules/(?!@agm)""node_modules/(?!@agm)""node_modules/(?!@agm/core)",但这些似乎都没有任何区别。

如何才能正确处理从@agm/core 导入的文件?

【问题讨论】:

    标签: javascript typescript jestjs


    【解决方案1】:

    我需要进行一些更改才能使其正常工作。这应该可以解决在将 @agm/core 用于 Angular 6 应用程序与 jest 一起使用时的问题。

    yarn add --dev babel-preset-env
    

    您应该已经拥有babel-jest

    然后更新你的 jest 配置:

    "transform": {
      "^.+\\.js": "<rootDir>/node_modules/babel-jest"
    },
    "transformIgnorePatterns": [
      "<rootDir>/node_modules/(?!@agm)"
    ]
    

    如果您没有.babelrc,您还需要添加:

    {
      "presets": ["babel-preset-env"]
    }
    

    这可能适用于其他需要安装 babel 转换的库。

    【讨论】:

      【解决方案2】:

      我想为 babel 7+ 添加它,而不是 .babelrc 使用 babel.config.js

      module.exports = {
          presets: ['babel-preset-env'],
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-13
        • 1970-01-01
        • 2021-10-28
        • 2017-07-04
        • 2021-11-12
        • 2019-07-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多