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

3

5

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 ?

Sate answered 4/2, 2021 at 11:31 Comment(2)
What's your @testing-library/jest-native version?Charleencharlemagne
@Charleencharlemagne the last one "^3.4.3"Sate
S
4

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

Sate answered 5/2, 2021 at 8:55 Comment(2)
Thank you! This was the last step I was missing.Bedouin
Great if that could help you ;)Sate
P
2

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';
Patricia answered 27/5, 2022 at 2:39 Comment(0)
J
0

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'; 
Junji answered 14/11, 2021 at 19:22 Comment(1)
Please pay attention to your formatting when posting an answer. I've edited your answer to help clarify the explanation and improve the readability. Take a look at my change to ensure it is consistent with your intention—there were a couple of places where I needed to interpret your meaning.Cultrate

© 2022 - 2024 — McMap. All rights reserved.