React-native-document-picker: Have to keep re-installing, plus jest warnings

Created on 22 Aug 2018  ·  11Comments  ·  Source: rnmods/react-native-document-picker

Occasionally (I haven't tied down the exact circumstances yet) I get compilation errors suggesting this module is not installed, so I have to install it again. It is registered like this in package.json, because I need v3:
"react-native-document-picker": "git://github.com/Elyx0/react-native-document-picker.git#v3",
(I've followed the instructions documented in https://github.com/Elyx0/react-native-document-picker/issues/94)

Also, in running jest tests I'm seeing this warning, I'm not sure if it's the same problem as above or different:
RNDocumentPicker Native module is not available, make sure you have finished the installation process and rebuilt your app

Also, running jest with --detectOpenHandles is showing this:

Jest has detected the following 1 open handle potentially keeping Jest from exiting:
  ●  TIMERWRAP
      at Object.<anonymous> (node_modules/react-native-document-picker/index.js:7:3)

Most helpful comment

In case it helps someone else: I worked around this issue by not import-ing the library, and instead require-ing it just before the code that will actually invoke it. Like this:

    const DocumentPicker = require('react-native-document-picker').default;
    /* ... */ await DocumentPicker.pick( // ...

It'd still be good for that to not be necessary.

All 11 comments

I also am seeing this

I'm also facing this warning.

Anyone cleared that out?

I am having this same problem

I released V3 into master, does this still happen?

still have this issue with version 3.2.4

Can you copy the error message?

Just have this warning with jest test. I'm using "react-native": "0.59.9" and "react-native-document-picker": "^3.2.4".
● Cannot log after tests are done. Did you forget to wait for something async in your test?
Attempted to log "RNDocumentPicker: Native module is not available, make sure you have finished the installation process and rebuilt your app".

  at BufferedConsole.warn (node_modules/@jest/console/build/BufferedConsole.js:224:10)
  at Timeout._onTimeout (node_modules/react-native-document-picker/index.js:8:13)

I'm experimenting with using this library, but if I do I get this same warning every time I run Jest. It shows up many times (once per test file?), making a lot of noise.

The warning is generated by react-native-document-picker itself. (It's wrapped in a warning from Jest, because it's under a setTimeout and happens after the test is done.) It's produced by this code in the library's index.js:

import {Platform, NativeModules} from 'react-native';
const {RNDocumentPicker} = NativeModules;

if ( !RNDocumentPicker ) {
  // Use a timeout to ensure the warning is displayed in the YellowBox
  setTimeout(() => {
    console.warn('RNDocumentPicker: Native module is not available, make sure you have finished the installation process and rebuilt your app');
  }, 0);

That won't work under Jest, because Jest runs the test code without all the native modules. That's key to why Jest runs so fast.

One good solution would be to move that warning logic inside the pick function. (Or wrap it in a function which is called from pick.) That way less is happening at import type, which is generally a good thing -- and in particular, this warning doesn't fire unless something actually tries to use the library.

In case it helps someone else: I worked around this issue by not import-ing the library, and instead require-ing it just before the code that will actually invoke it. Like this:

    const DocumentPicker = require('react-native-document-picker').default;
    /* ... */ await DocumentPicker.pick( // ...

It'd still be good for that to not be necessary.

Adding this mock in the jest.setup.js file fixed the warning for me.

jest.mock('react-native-document-picker', () => ({ default: jest.fn(), }));

Was this page helpful?
0 / 5 - 0 ratings