15

Vuejs项目中,节点版本:v10.6.0

Package.json 版本:

 "devDependencies": {
    "@vue/cli-plugin-babel": "^3.0.0-rc.4",
    "@vue/cli-plugin-unit-jest": "^3.0.0-rc.4",
    "@vue/cli-service": "^3.0.0-rc.4",
    "@vue/test-utils": "^1.0.0-beta.20",
    "babel-core": "7.0.0-bridge.0",
    "babel-jest": "^23.0.1",
    "vue-template-compiler": "^2.5.16"
  },

当我使用内置任务运行它时,

"test:unit": "vue-cli-service test:unit"

但是因为我想调试,所以我使用 node 命令手动运行:

node  node_modules/.bin/jest

它给出以下错误:

 FAIL  tests/unit/HelloWorld1.spec.js
  ● Test suite failed to run

    .../tests/unit/HelloWorld1.spec.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import "core-js/modules/es6.array.iterator";
                                                                                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    SyntaxError: Unexpected string

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)

在我添加了具有以下内容的 .babelrc 之后

{"env": {
    "development": {
        "plugins": ["transform-es2015-modules-commonjs"]
    },
    "test": {
        "plugins": ["transform-es2015-modules-commonjs"]
    }
}}

事情变得更好了,它可以在没有“导入”的情况下通过测试文件,一旦有导入,它会显示一个不同的错误:

....tests/unit/HelloWorld1.spec.js:3
    import _interopRequireWildcard from "..../node_modules/@babel/runtime/helpers/builtin/es6/interopRequireWildcard";
           ^^^^^^^^^^^^^^^^^^^^^^^

    SyntaxError: Unexpected identifier

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
4

4 回答 4

15

经过几天的挣扎。最后,我得到了在 VueJs 应用程序的调试模式下运行 jest 的解决方案。

在调试到 vue-cli-service 之后@vue/cli-plugin-unit-jest,我在它产生 jest 进程之前发现了以下代码:

 process.env.VUE_CLI_BABEL_TARGET_NODE = true
 process.env.VUE_CLI_BABEL_TRANSPILE_MODULES = true

解决方案

所以解决方案非常简单。

只需在运行 jest 命令之前添加这两个环境变量。以下命令将以调试模式启动 jest:

export VUE_CLI_BABEL_TARGET_NODE=true
export VUE_CLI_BABEL_TRANSPILE_MODULES=true
./node_modules/jest/bin/jest.js --clearCache
node --inspect-brk ./node_modules/jest/bin/jest.js -i

笔记

  • 确保不要 添加“.babel.rc”,这会增加 VueJS babel。

  • 通常,您需要使用该--clearCache选项运行 jest。否则,陈旧的生成文件也会搞砸。

  • 开玩笑的选择-i也很重要。否则,测试将在不处于调试模式的单独进程中运行。

于 2018-07-18T17:32:58.833 回答
1

有同样的问题。

 FAIL  tests/unit/example.spec.js
  ● Test suite failed to run

    .../tests/unit/example.spec.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import "core-js/modules/es6.array.find";
                                                                                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    SyntaxError: Unexpected string

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)

试过上面的答案。这篇文章解决了我的问题。

解决方案:

  1. npm 卸载“@vue/cli-plugin-unit-jest”
  2. 删除了包含所有内容的测试文件夹
  3. 删除了 jest.config.js 文件
  4. vue 添加@vue/cli-plugin-unit-jest
  5. 对于 VS Code,使用下一个 launch.json

{
  "version": "0.2.0",
  "configurations": [            
    {
      "name": "vscode-jest-tests",
      "type": "node",
      "request": "launch",
      "runtimeArgs": [
        "--inspect-brk",
        "./node_modules/@vue/cli-service/bin/vue-cli-service.js",
        "test:unit",
        "--runInBand"
      ],
      "cwd": "${workspaceFolder}",
      "protocol": "inspector",
      "disableOptimisticBPs": true,
      "console": "integratedTerminal",    
      "internalConsoleOptions": "neverOpen",
      "outFiles": [ "${workspaceFolder}/src/**/*.js"],
      "port": 9229
    },
  ]
}

于 2019-08-06T11:58:09.303 回答
0

另一种解决方案是确保您的节点环境正确解析。
就我而言,它将解决为“发展”。将我的更新package.json为以下解决了该问题:

"test": "NODE_ENV=test jest"

这将确保您的 babel 配置使用测试配置。

于 2019-08-15T00:22:30.373 回答
0

您的测试套件以前是否正常工作,现在却抛出此错误?这就是发生在我身上的事情,在深入研究之前,很有可能缓存了导致这种情况的某些东西。

我使用以下方法解决了它:npm test -- -u

如果失败,您应该删除您的node_modules文件夹并重新创建npm install,然后尝试npm cache clean --force.

如果您想在没有缓存的情况下运行测试,另一个有用的命令是npm test -- --no-cache -u.

于 2019-11-25T14:33:33.903 回答