E.D
E.D

Reputation: 891

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

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:

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 ?

Upvotes: 5

Views: 4161

Answers (3)

benmneb
benmneb

Reputation: 2303

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';

Upvotes: 2

Matko Milić
Matko Milić

Reputation: 13

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'; 

Upvotes: 0

E.D
E.D

Reputation: 891

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

Upvotes: 4

Related Questions