react-native-reanimated-bottom-sheet icon indicating copy to clipboard operation
react-native-reanimated-bottom-sheet copied to clipboard

Error while trying to render inside a jest test.

Open mitsest opened this issue 4 years ago • 7 comments

I 'm trying to render BottomSheet inside a test and I'm getting

    Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
    
    Check the render method of `BottomSheetBehavior`.

Any hints?

mitsest avatar Jul 29 '20 14:07 mitsest

I had to mock a bunch of things that the Bottom Sheet used. Note that other things you are using that require mocking which are added to the bottom tab content can cause this problem.

jest.mock('reanimated-bottom-sheet');
jest.mock('react-native-reanimated', () => {
  const View = require('react-native').View;
  return {
    Value: jest.fn(),
    event: jest.fn(),
    add: jest.fn(),
    eq: jest.fn(),
    set: jest.fn(),
    cond: jest.fn(),
    interpolate: jest.fn(),
    sub: jest.fn(),
    multiply: jest.fn(),
    sqrt: jest.fn(),
    max: jest.fn(),
    diff: jest.fn(),
    onChange: jest.fn(),
    View: View,
    Extrapolate: { CLAMP: jest.fn() },
    Clock: jest.fn(),
    greaterThan: jest.fn(),
    lessThan: jest.fn(),
    startClock: jest.fn(),
    stopClock: jest.fn(),
    clockRunning: jest.fn(),
    not: jest.fn(),
    or: jest.fn(),
    and: jest.fn(),
    spring: jest.fn(),
    decay: jest.fn(),
    defined: jest.fn(),
    call: jest.fn(),
    block: jest.fn(),
    abs: jest.fn(),
    greaterOrEq: jest.fn(),
    lessOrEq: jest.fn(),
    debug: jest.fn(),
    Transition: {
      Together: 'Together',
      Out: 'Out',
      In: 'In',
    },
  };
});

ankithm28 avatar Aug 03 '20 22:08 ankithm28

@Monkeyank thanks for your response.

Now the error is

  console.error node_modules/react-native/Libraries/LogBox/LogBox.js:33
    Warning: `BottomSheetBehavior` uses `getDerivedStateFromProps` but its initial state is undefined. This is not recommended. Instead, define the initial state by assigning an object to `this.state` in the constructor of `BottomSheetBehavior`. This ensures that `getDerivedStateFromProps` arguments have a consistent shape.

  console.error node_modules/react-native/Libraries/LogBox/LogBox.js:33
    Warning: BottomSheetBehavior.getDerivedStateFromProps(): A valid state object (or null) must be returned. You have returned undefined.

mitsest avatar Aug 04 '20 11:08 mitsest

@Monkeyank thanks for your response.

Now the error is

  console.error node_modules/react-native/Libraries/LogBox/LogBox.js:33
    Warning: `BottomSheetBehavior` uses `getDerivedStateFromProps` but its initial state is undefined. This is not recommended. Instead, define the initial state by assigning an object to `this.state` in the constructor of `BottomSheetBehavior`. This ensures that `getDerivedStateFromProps` arguments have a consistent shape.

  console.error node_modules/react-native/Libraries/LogBox/LogBox.js:33
    Warning: BottomSheetBehavior.getDerivedStateFromProps(): A valid state object (or null) must be returned. You have returned undefined.

Hey @mitsest have you solved this issue?

srinu6 avatar Dec 11 '20 01:12 srinu6

I'm facing the issue described in the first message. I've tried mocking the components suggested Monkeyank, but still getting the same error. Any ideas on why this happens? :/

luispinho avatar Feb 10 '21 11:02 luispinho

I believe the issue that was mentioned with log box can be solved with mocking log box. I believe this happens in rn versions >0.63

ankithm28 avatar Feb 14 '21 00:02 ankithm28

Anyone interested mocking LogBox can use the following:

jest.mock('react-native/Libraries/LogBox/LogBox');

mit4dev avatar May 05 '21 21:05 mit4dev

Hello! Faced wit the same issue as a topic starter. Jest don't want to render bottom sheet. MB someone has new ideas?

atrofimenko avatar Sep 30 '21 11:09 atrofimenko