callstack / react-native-testing-library

🦉 Simple and complete React Native testing utilities that encourage good testing practices.
https://callstack.github.io/react-native-testing-library/
MIT License
3.07k stars 271 forks source link

TypeError: expect(...).toHaveDisplayValue is not a function #1562

Closed tkyr-hh closed 7 months ago

tkyr-hh commented 8 months ago

Describe the bug

As mentioned in the docs we can check the TextInput value with native matcher toHaveDisplayValue(value)

Expected behavior

The matcher is working fine.

Steps to Reproduce

Versions

npmPackages:
  @testing-library/react-native: ^12.4.3 => 12.4.3
  react: 18.2.0 => 18.2.0
  react-native: 0.71.14 => 0.71.14
  react-test-renderer: 18.2.0 => 18.2.0**
mdjastrzebski commented 8 months ago

@tkyr-hh Did you added the required import to your jest-setup.ts file? https://callstack.github.io/react-native-testing-library/docs/jest-matchers#setup

tkyr-hh commented 8 months ago

@mdjastrzebski yes, I've add it inside under the jest key in package.json as setupFilesAfterEnv.

package.json

"jest": {
    "preset": "react-native",
    "transformIgnorePatterns": [
      "node_modules/(?!(jest-)?@react-native|react-native|@react-native/.*|@sentry/react-native|@react-navigation/.*|react-native-svg|@sentry/react-native|react-native-appsflyer|@react-native-community/.*|@react-native-community/geolocation)"
    ],
    "setupFilesAfterEnv": [
      "@testing-library/jest-native/extend-expect"
    ],
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ],
    "automock": false,
    "setupFiles": [
      "./node_modules/react-native-gesture-handler/jestSetup.js",
       "./setup-jest.js"
    ],
    "transform": {
      "^.+\\.js?$": "babel-jest"
    }
  }

setup-jest.js

/* eslint-env jest */

import {NativeModules} from 'react-native';
import 'react-native-gesture-handler/jestSetup';

NativeModules.ImagePickerManager = {
  showImagePicker: jest.fn(),
  launchCamera: jest.fn(),
  launchImageLibrary: jest.fn(),
};
...
mdjastrzebski commented 7 months ago

You should be importing "@testing-library/react-native/extend-expect", and not "@testing-library/jest-native/extend-expect".

You have this in your jest entry in package.json:

    "setupFilesAfterEnv": [
      "@testing-library/jest-native/extend-expect"
    ],
tkyr-hh commented 7 months ago

Oh! sorry, I ready did not noticed this difference while I am setting up the testing environment. @mdjastrzebski Thanks a lot for your help.

Closing this issue...