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 ?

E.D
  • 831
  • 3
  • 16
  • 33

3 Answers3

4

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

E.D
  • 831
  • 3
  • 16
  • 33
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';
benmneb
  • 1,773
  • 1
  • 15
  • 26
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'; 
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • 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. – Jeremy Caney Nov 15 '21 at 00:23