expo app with testing react-native-testing-library and jest-native "has no exported member 'toHaveProp"

Viewed 1855

I'm trying to use jest-native as extra matchers and i'm having an installation problem i think ...
I am on a react-native ts app with expo and here are my versions:

  • expo:"~39.0.0"
  • @testing-library/react-native version:"^7.1.0"
  • jest-preset: 'jest-expo',
  • react-native version:" 63.4 "

I would like to use it in this way:

import { toHaveProp } from '@testing-library/jest-native';

expect.extend({ toHaveProp });

But it gives me this error:

Module '"../../../../../../node_modules/@testing-library/jest-native/extend-expect"' has no exported member 'toHaveProp'.

any idea ?

3 Answers

ok if i put it import '@testing-library/jest-native'; on the top of my test file , that works

I had the same issue and to me what worked is simply adding the following to the Jest config:

setupFilesAfterEnv: ['@testing-library/jest-native/extend-expect'],

I didn't need to write expect.extend, but simply toHaveStyle and other functions were available when I wrote dot; for example:

expect(getByTestId('containerView')).toHaveStyle;

The toHaveStyle was now simply here WITHOUT specially imported toHaveStyle above in the imports. It is worth noting that I also did have to import jest-native in my test files, as the accepted answer is suggesting:

import '@testing-library/jest-native'; 

I didn't want to import it in every test file, and my setup (using expo) was not working adding it in setupFilesAfterEnv so I did the following from the official Github.

Import @testing-library/jest-native/extend-expect once (for instance in your tests setup file) and you're good to go:

import '@testing-library/jest-native/extend-expect';
Related