首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用react-native-fs lib在react-native中运行Jest单元测试

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

Stack Overflow用户
提问于 2017-04-05 16:55:39
回答 1查看 2K关注 0票数 4

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

代码语言:javascript
复制
> 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。你可以看一下我的示例存储库,让我知道我错了什么吗?

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-07-01 03:25:08

对于那些正在寻找这个问题的快速解决方案的人:

__mocks__文件夹中创建一个名为react-native-fs.js的文件,并添加以下内容:

代码语言:javascript
复制
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(),
  };
});

享受吧!

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43226413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档