Javascript Jest TypeError:路径必须是字符串。收到未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46220674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:22:45  来源:igfitidea点击:

Jest TypeError: Path must be a string. Received undefined

javascriptwebpackjestjsbabel

提问by GibboK

Below settings for my package.json

下面是我的 package.json 的设置

If I run from command line npm testall jest test cases are executed properly. In case I use directly the command jestfrom command line I receive this error:

如果我从命令行运行,npm test所有 jest 测试用例都会正确执行。如果我直接从命令行使用命令jest,我会收到此错误:

Test suite failed to run

TypeError: Path must be a string. Received undefined

  at assertPath (path.js:7:11)
  at Object.relative (path.js:538:5)

测试套件无法运行

TypeError: Path must be a string. Received undefined

  at assertPath (path.js:7:11)
  at Object.relative (path.js:538:5)

This happens on any test files.

这发生在任何测试文件上。

Any idea what could be wrong and how to fix it?

知道什么可能是错的以及如何解决吗?

 "scripts": {
        "test": "standard && jest",
        "format": "standard --fix",
        "start": "webpack-dev-server --config webpack.config.dev.js",
        "build": "webpack --config webpack.config.prod.js"
    },

采纳答案by GibboK

I solved this issue using:

我使用以下方法解决了这个问题:

  • npm uninstall -g jest
  • npm install -g jest
  • npm cache clean
  • npm install
  • npm 卸载 -g 开玩笑
  • npm install -g 开玩笑
  • npm 缓存清理
  • 安装

回答by eyarmush

I am not entirely sure what the problem here is, but I was facing the same issue and what worked for me was updating the Jest version. I was using 20 but I switched to 21 and now it magically works again.

我不完全确定这里的问题是什么,但我遇到了同样的问题,对我有用的是更新 Jest 版本。我使用的是 20,但我切换到 21,现在它又神奇地工作了。

Update version in package.jsonand then run rm -rf node_modules && npm install

更新版本package.json然后运行rm -rf node_modules && npm install

回答by Robin

In my case i had to update jest to version 21.x (from 20.x).

就我而言,我不得不将 jest 更新到版本 21.x(从 20.x)。

Try running npm install jest@latest --save-dev.

尝试运行npm install jest@latest --save-dev

回答by Dominic Tracey

For me, it was the jest-cli package bumping to 21.x. So npm update jest-cli --save-dev

对我来说,是 jest-cli 包升级到 21.x。所以npm update jest-cli --save-dev

回答by Will Humphreys

For me I had to downgrade. Version 21.x worked for me.

对我来说,我不得不降级。21.x 版对我有用。

回答by Harsh Shah

I solved this issue using:

我使用以下方法解决了这个问题:

remove node_modulesremove package-lock.json

移除node_modules移除package-lock.json

remove dev dependence from package.jsonof jest npm remove jest --save-dev.

package.json开玩笑中删除开发依赖npm remove jest --save-dev.

npm cache clean

npm cache clean

then run command npm install

然后运行命令 npm install

then install npm install jest@latest --save-dev

然后安装 npm install jest@latest --save-dev

this works for me

这对我有用

回答by Chihab

npm install [email protected]

npm install [email protected]

Installing this version of react-dev-utilshelped, when sometimes the commands in the accepted answers didn't.

安装此版本的react-dev-utils帮助,有时接受的答案中的命令没有。

Good luck y'all!

祝你们好运!