运行 Jest 测试时出现意外标记 'import' 错误?

Unexpected token 'import' error while running Jest tests?

我知道这个问题已被问过好几次,但我遇到的所有解决方案似乎都不适合我。我在尝试 运行 对 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/"
  },
  "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/Whosebug-jest-question

我该如何解决这个问题?

你只需要确保vue-awesome会被jest转化,所以添加 以下是你的笑话配置:

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

意思是:“忽略 node_modules 中的所有内容,除了 vue-awesome

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

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

jest --clearCache

我们在另一个图书馆遇到了同样的问题。根本原因是我们在代码中存在循环依赖。但是错误文本根本没有提到它。就像在这个 post: "Jest encountered an unexpected token..."

在我的例子中,我需要 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
};

package.json 中添加这个对我有用(将 <package_name> 替换为导致包名称)

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