2

Using @react-navigation/stack library in my project, while doing unit testing (using jest) I am getting this error

Couldn't find the header height. Are you inside a screen in Stack?

index.spec.js

  it('should have a title and subtitle', () => {
  const { getByText } = render(<Otp {...props} />);      });

Otp component

<SafeAreaView style={styles.mainContainer}>
  <KeyboardAvoidingView
    keyboardVerticalOffset={useHeaderHeight()} --> getting error at this line
  >
    <Text>...</Text>
  </KeyboardAvoidingView>
</SafeAreaView>

Also tried a solution in which I mocked a navigator but got no success.

1 Answers1

0

I came across the same issue today. What worked for me was to mock the entire module to return an object with useHederHeight. I think this is fine for unit testing and if you need to mock extra features you can do that too.

jest.mock('@react-navigation/elements', () => ({
  useHeaderHeight: jest.fn().mockImplementation(() => 200)
}))