【问题标题】:Babel 6->7: Error unknown: Unexpected token (spread operator)Babel 6->7:错误未知:意外令牌(扩展运算符)
【发布时间】:2019-08-20 21:37:03
【问题描述】:

我从 babel 6 迁移到 babel 7。我遇到了一些我无法纠正的错误。

未知:意外的令牌(即使我使用的是@babel/plugin-proposal-object-rest-spread)。

这个问题在我运行 jest 单元测试套件时出现。

错误日志:

 ► yarn run test:unit
yarn run v1.13.0
$ jest --clearCache && jest --coverage=false
Cleared /tmp/jest_rs
 FAIL  test/unit/store.test.js
  ● Test suite failed to run

    unknown: Unexpected token (55:10)

      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:
       • 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:
      unknown: Unexpected token (55:10)
        53 |       mainMenu: [
        54 |         {
      > 55 |           ...this.$store.state.routes.home,
           |           ^
        56 |           type: Utils.MenuRecordType.PAGE,
        57 |           name: this.$t(`main_menu.${this.$store.state.routes.home.key}`),
        58 |           children: []

我的babel.config.js 和我的jest.config.js。 (由于 stackoverflow 的限制,这里没有发布(您的帖子中的代码太多......)。

完整的代码可以在github找到。

【问题讨论】:

  • 您是在构建站点时还是在运行 Jest 时遇到这些错误?你正在运行哪个 npm 命令来启动 Jest?
  • 您似乎只需要找到正确的插件.. 试试这些:Link One, Link Two - 祝你好运!
  • 看来你根本没有开玩笑的 babel 配置。创建 .babelrc
  • @MattOestreich,您提供的链接错误。链接一用于 babel 6,没有 babel 7。第二个链接似乎与此问题无关。
  • @Aldarund,不,我已经有一个 babel 配置。由于 stackoverflow 代码限制(对于您发布的许多代码……)、check my babel.config.js 和我的jest.config.js,我不能把它放在这里

标签: vue.js jestjs babeljs nuxt.js babel-jest


【解决方案1】:

我在“意外令牌...”上有类似的问题,这是针对我的 @testing-library 的扩展运算符错误。即使在我检查了所有应用正确的插件之后,如提到的herehere,并使用 babel.config.js 而不是 .babelrc(由于babel7);错误仍然存​​在。 令人费解的是,Jenkins 管道能够成功通过相同提交的测试,而不会出现“意外的令牌错误......”。

然后我发现模式,我的其他团队成员也能够运行测试没有遇到错误...除了我。我们都在使用相同的 package-lock.json

与同行相比,唯一的区别是我使用的是旧版本的 nodejs 和 npm。

如果对于相同的源代码提交,您的其他同行能够运行测试,但您。那么这可能很有用:

  • npm cache clean --force
  • 在你的主仓库文件夹中:rm -rf node_modules/
  • 升级你的 node js(对我来说至少升级到 8.11.x 及更高版本)
  • 再次npm install
  • 再次运行 jestnpm run test(如果您在 package.json 中定义)

请注意,我不需要升级我的 npm。

【讨论】:

  • 尝试了您的解决方案。对于 8.12.X,我还有另一个错误:file.isFile is not a function。使用上述版本(12、13),我得到了同样的错误。
猜你喜欢
  • 2018-08-30
  • 2016-02-18
  • 1970-01-01
  • 2016-04-06
  • 2018-04-05
  • 2016-02-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多