无法使用react-native-fs lib在react-native中运行Jest单元测试

tru*_*anh 6 jestjs react-native react-native-fs

我使用最新版本创建了一个新的react-native应用程序,默认测试运行良好。\n然后我使用\'react-native-fs\'lib在我的应用程序中写入一个文件,它运行良好。\n但我尝试运行测试命令,失败。

\n\n
> TestJest@0.0.1 test D:\\test\\TestJest\n> jest\n\n FAIL  __tests__\\index.android.js\n  \xe2\x97\x8f Test suite failed to run\n\nTypeError: Cannot read property \'RNFSFileTypeRegular\' of undefined\n\n  at Object.<anonymous> (node_modules\\react-native-fs\\FS.common.js:17:36)\n  at Object.<anonymous> (index.android.js:14:20)\n\nTest Suites: 1 failed, 1 total\nTests:       0 total\nSnapshots:   0 total\nTime:        0.8s\nRan all test suites.\nnpm ERR! Test failed.  See above for more details.\n
Run Code Online (Sandbox Code Playgroud)\n\n

我已按照Jest 的手册文档进行操作,但尚未解决错误。这是我的示例存储库。您能看一下我的示例存储库并让我知道我错了什么吗?

\n\n

谢谢。

\n

Fra*_*eau 15

对于那些寻求快速解决此问题的人:

react-native-fs.js创建一个在文件夹内命名的文件__mocks__,并将其添加为其内容:

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(),
  };
});
Run Code Online (Sandbox Code Playgroud)

享受!