我正在尝试使用Jest(我第一次使用Jest)对React Native项目进行测试,并且每当我运行npm test
时都会遇到此错误。
> @ test D:\repo\foldername
> jest
path.js:39
throw new ERR_INVALID_ARG_TYPE('path', 'string', path);
^
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type object
at assertPath (path.js:39:11)
at Object.resolve (path.js:166:7)
at Object.<anonymous> (D:\repo\foldername\node_modules\jest-expo\bin\jest.js:20:30)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19)
npm ERR! Test failed. See above for more details.
我尝试在没有jest
的情况下呼叫npm test
,并且运行正常。这是我拥有的package.json
:
{
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web",
"eject": "expo eject",
"test": "jest"
},
"dependencies": {
"@react-native-community/async-storage": "^1.12.0",
"@react-native-community/masked-view": "0.1.10",
"@react-navigation/native": "^5.7.3",
"@react-navigation/stack": "^5.9.0",
"apisauce": "^1.1.2",
"bufferutil": "^4.0.1",
"canvas": "^2.6.1",
"dayjs": "^1.8.34",
"expo": "~38.0.8",
"expo-constants": "~9.1.1",
"expo-status-bar": "^1.0.2",
"lottie-react-native": "^3.5.0",
"react": "^16.11.0",
"react-dom": "~16.11.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-38.0.2.tar.gz",
"react-native-gesture-handler": "^1.6.1",
"react-native-reanimated": "~1.9.0",
"react-native-safe-area-context": "~3.0.7",
"react-native-screens": "~2.9.0",
"react-native-web": "~0.11.7",
"react-test-renderer": "^16.13.1",
"utf-8-validate": "^5.0.2"
},
"devDependencies": {
"@babel/core": "^7.8.6",
"@testing-library/react-native": "^7.0.2",
"babel-preset-expo": "~8.1.0",
"jest-expo": "^38.0.2"
},
"jest": {
"preset": "jest-expo",
"setupFiles": [
"./jestSetupFile.js"
],
"transformIgnorePatterns": [
"./node_modules/(?!(jest-)?react-native|react-clone-referenced-element|@react-native-community|expo(nent)?|@expo(nent)?/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|@sentry/.*)"
]
},
"private": true
}
您知道是什么原因造成的吗?以及如何解决这个问题?