我用最新版本创建了一个新的react-native应用程序,默认测试运行良好。然后,我使用'react-native-fs‘lib在我的应用程序中写入一个文件,它工作得很好。但是我尝试运行测试命令,它失败了。
> TestJest@0.0.1 test D:\test\TestJest
> jest
FAIL __tests__\index.android.js
● Test suite failed to run
TypeError: Cannot read property 'RNFSFileTypeRegular' of undefined
at Object.<anonymous> (node_modules\react-native-fs\FS.common.js:17:36)
at Object.<anonymous> (index.android.js:14:20)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.8s
Ran all test suites.
npm ERR! Test failed. See above for more details.我已经关注了the manual document of Jest,但我还没有解决错误。这是my example repo。你可以看一下我的示例存储库,让我知道我错了什么吗?
谢谢。
发布于 2020-07-01 03:25:08
对于那些正在寻找这个问题的快速解决方案的人:
在__mocks__文件夹中创建一个名为react-native-fs.js的文件,并添加以下内容:
jest.mock('react-native-fs', () => {
return {
mkdir: jest.fn(),
moveFile: jest.fn(),
copyFile: jest.fn(),
pathForBundle: jest.fn(),
pathForGroup: jest.fn(),
getFSInfo: jest.fn(),
getAllExternalFilesDirs: jest.fn(),
unlink: jest.fn(),
exists: jest.fn(),
stopDownload: jest.fn(),
resumeDownload: jest.fn(),
isResumable: jest.fn(),
stopUpload: jest.fn(),
completeHandlerIOS: jest.fn(),
readDir: jest.fn(),
readDirAssets: jest.fn(),
existsAssets: jest.fn(),
readdir: jest.fn(),
setReadable: jest.fn(),
stat: jest.fn(),
readFile: jest.fn(),
read: jest.fn(),
readFileAssets: jest.fn(),
hash: jest.fn(),
copyFileAssets: jest.fn(),
copyFileAssetsIOS: jest.fn(),
copyAssetsVideoIOS: jest.fn(),
writeFile: jest.fn(),
appendFile: jest.fn(),
write: jest.fn(),
downloadFile: jest.fn(),
uploadFiles: jest.fn(),
touch: jest.fn(),
MainBundlePath: jest.fn(),
CachesDirectoryPath: jest.fn(),
DocumentDirectoryPath: jest.fn(),
ExternalDirectoryPath: jest.fn(),
ExternalStorageDirectoryPath: jest.fn(),
TemporaryDirectoryPath: jest.fn(),
LibraryDirectoryPath: jest.fn(),
PicturesDirectoryPath: jest.fn(),
};
});享受吧!
https://stackoverflow.com/questions/43226413
复制相似问题