React-native-fs: JEST-test failed: Cannot read property 'RNFSFileTypeRegular' of undefined

Created on 19 Dec 2017  Â·  8Comments  Â·  Source: itinance/react-native-fs

My app runs and work correctly on Android and iOS, but when i run a jest test (at this moment without a specific test for RNFS but with a test for some screens where RNFS is used) it fails with the following error.

Test suite failed to run

    TypeError: Cannot read property 'RNFSFileTypeRegular' of undefined

      at Object.<anonymous> (node_modules/react-native-fs/FS.common.js:18:37)
      at Object.<anonymous> (app/helpers/SaveDecodeImage.js:1:711)
      at Object.<anonymous> (app/helpers/API.js:9:22)

I don'n know how to fix this issue...

Most helpful comment

@SoundBlaster

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(),
  };
});

All 8 comments

Fix for now is to mock everything. But i would like to test react-native-fs with jest, so then i can't mock it and have to use it. How can i do that?

@vdlindenmark can you please show your mock?

@SoundBlaster

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(),
  };
});

If you place the mock in the node_modules mocks directory, you'll have to use a different syntax.

// /__mocks__/react-native-fs.js
export default {
  mkdir: jest.fn(),
  moveFile: jest.fn(),
  copyFile: jest.fn(),
  …
};

@vdlindenmark Where should i place this code? If i place it inside my test(...) before expect(..) or generally anywhere in my test.js file, the error persists.

In your jest config

If you place the mock in the node_modules mocks directory, you'll have to use a different syntax.

// /__mocks__/react-native-fs.js
export default {
  mkdir: jest.fn(),
  moveFile: jest.fn(),
  copyFile: jest.fn(),
  …
};

I would recommend doing:

module.export = {
  mkdir: jest.fn(),
  ...
}

Instead, as that is how the functinos are exported in FS.common.js
But then u should be good.

Still no solution without mocking? We load translation files via rn-fs, with mocking the app won't show any text at all, and can't test changing locale.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ToJen picture ToJen  Â·  4Comments

wangfpp picture wangfpp  Â·  3Comments

MuppaAkhil picture MuppaAkhil  Â·  3Comments

PimDeWitte picture PimDeWitte  Â·  4Comments

zaheerahmad92001 picture zaheerahmad92001  Â·  3Comments