【问题标题】:Unexpected token 'import' error while running Jest tests?运行 Jest 测试时出现意外的令牌“导入”错误?
【发布时间】:2018-12-10 03:36:45
【问题描述】:

我意识到这个问题已经被问过好几次了,但我遇到的所有解决方案似乎都不适合我。我在尝试为 Vue 应用程序运行 Jest 测试时遇到以下错误。

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:
 • 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://facebook.github.io/jest/docs/en/configuration.html

Details:

/node_modules/vue-awesome/icons/expand.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import Icon from '../components/Icon.vue'
                                                                                         ^^^^^^

SyntaxError: Unexpected token import

> 17 | import 'vue-awesome/icons/expand'

.babelrc:

{
  "presets": [
    ["env", {
      "modules": false,
      "targets": {
        "browsers": ["> 1%", "last 2 versions", "not ie <= 8"]
      }
    }]
  ],
  "env": {
    "test": {
      "presets": [
        ["env", { "targets": { "node": "current" }}]
      ]
    }
  }
}

package.json 中的笑话配置:

"jest": {
  "moduleFileExtensions": [
    "js",
    "vue"
  ],
  "moduleNameMapper": {
    "^@/(.*)$": "<rootDir>/src/$1"
  },
  "transform": {
    "^.+\\.js$": "<rootDir>/node_modules/babel-jest",
    ".*\\.(vue)$": "<rootDir>/node_modules/vue-jest"
  },
  "snapshotSerializers": [
    "<rootDir>/node_modules/jest-serializer-vue"
  ],
  "moduleDirectories": [
    "node_modules",
    "src"
  ]
}

看起来正在为测试安装的 Vue 组件的脚本中的初始导入正在运行,但模块本身 (import Icon from '../components/Icon.vue) 中的导入无法识别。

样板回购重新创建问题:github.com/DonaldPeat/stackoverflow-jest-question

我该如何解决这个问题?

【问题讨论】:

  • 您是否使用vue-cli 创建了您的项目?如果有,是哪个版本?如果没有,您是否有指向显示问题的 GitHub 存储库的链接? FWIW,我无法使用 vue-cli 项目 (3.0.0-rc.3) 重现此问题。
  • @tony19 我没有使用vue-cli。我创建了一个样板回购来重新创建问题github.com/DonaldPeat/stackoverflow-jest-question

标签: vue.js configuration jestjs babel-jest


【解决方案1】:

您只需要确保 vue-awesome 将被 jest 转换,因此添加 按照你的笑话配置:

transformIgnorePatterns: ["/node_modules/(?!vue-awesome)"],

这意味着:“忽略 node_modules 中的所有内容,vue-awesome 除外。

这里还有可能导致此错误的其他问题的详尽列表:https://github.com/facebook/jest/issues/2081

【讨论】:

  • 感谢您的链接!我发现了我的问题。
【解决方案2】:

如果您在更新到较新的 Jest 版本后遇到此问题,请尝试清除 Jest 的内部缓存:

jest --clearCache

【讨论】:

    【解决方案3】:

    我们在另一个库中遇到了同样的问题。根本原因是我们在代码中有循环依赖。但是错误文本根本没有提到它。就像在这篇文章中一样:“Jest 遇到了一个意外的令牌......”

    【讨论】:

      【解决方案4】:

      在我的情况下,我需要 jest.config.js 文件中的 testEnvironment: "node"。当我开始对 Vue Router 进行测试时,错误就出现了。

      // jest.config.js
      module.exports = {
        preset: "@vue/cli-plugin-unit-jest/presets/typescript",
        transform: {
          "^.+\\.vue$": "vue-jest",
          ".+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$":
            "jest-transform-stub",
        },
        moduleNameMapper: {
          "^.+.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$":
            "jest-transform-stub",
        },
        testEnvironment: "node",  // It fixes my issue
      };
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-12
      • 2018-04-25
      • 2017-10-20
      • 2023-03-28
      • 2019-09-26
      • 1970-01-01
      相关资源
      最近更新 更多