在运行Jest测试时出现“Unexpected token 'import'”错误?

47

我知道这个问题已经被问过很多次,但是我遇到的所有解决方案似乎都无法解决我的问题。 我在试图运行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配置:

"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 上表现出这个问题的 repo 的链接吗?顺带一提,我用 vue-cli 创建的项目 (3.0.0-rc.3) 无法重现这个问题。 - tony19
@tony19 我没有使用 vue-cli。我创建了一个样板库来重新创建这个问题 https://github.com/DonaldPeat/stackoverflow-jest-question - Don
5个回答

33

您只需要确保 vue-awesome 能够被 jest 转换,因此请将以下内容添加到您的 jest 配置中:

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

这意味着:“忽略node_modules中的所有内容,除了vue-awesome。” 此外,这里有一个详尽的列表,列出可能导致此错误的其他问题:https://github.com/facebook/jest/issues/2081

10

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

jest --clearCache

3

在我的情况下,将以下内容添加到package.json中有效(用引起问题的软件包名称替换<package_name>

"jest": {
    "transformIgnorePatterns": ["node_modules/(?!<package_name>)/"]
}

0
在我的情况下,我需要在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
};

似乎是默认选项,不应该必要。 - Juan Antonio

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接