facebook / react-native

A framework for building native applications using React
https://reactnative.dev
MIT License
119.39k stars 24.36k forks source link

TypeError: _reactNative.AccessibilityInfo.announceForAccessibilityWithOptions is not a function #44014

Open Jonnboy91 opened 7 months ago

Jonnboy91 commented 7 months ago

Description

When using AccessibilityInfo.announceForAccessibility no problems come when I run jest, but when I change it to AccessibilityInfo.announceForAccessibilityWithOptions I get: TypeError: _reactNative.AccessibilityInfo.announceForAccessibilityWithOptions is not a function I checked node_modules/react-native/jest/setup.js file I can see that under AccessibilityInfo there is no announceForAccessibilityWithOptions mocking, but when I change announceForAccessibility -> announceForAccessibilityWithOptions in that file then tests run without issues (not going to obviously leave it like this, just tested if that helps).

I tried to mock announceForAccessibilityWithOptions within my own jest.setup file, but that created a lot of different issues.

Steps to reproduce

Use AccessibilityInfo.announceForAccessibilityWithOptions() and run jest and you'll get an issue TypeError: _reactNative.AccessibilityInfo.announceForAccessibilityWithOptions is not a function

React Native Version

0.73.6

Affected Platforms

Build - MacOS

Output of npx react-native info

info Fetching system and libraries information...
System:
  OS: macOS 14.2.1
  CPU: (10) arm64 Apple M2 Pro
  Memory: 613.63 MB / 32.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 20.11.0
    path: ~/.nvm/versions/node/v20.11.0/bin/node
  Yarn:
    version: 1.22.19
    path: /opt/homebrew/bin/yarn
  npm:
    version: 10.2.4
    path: ~/.nvm/versions/node/v20.11.0/bin/npm
  Watchman:
    version: 2023.12.04.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.14.2
    path: /Users/jonnesten/.asdf/shims/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.2
      - iOS 17.2
      - macOS 14.2
      - tvOS 17.2
      - visionOS 1.0
      - watchOS 10.2
  Android SDK: Not Found
IDEs:
  Android Studio: 2023.1 AI-231.9392.1.2311.11076708
  Xcode:
    version: 15.2/15C500b
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.9
    path: /usr/bin/javac
  Ruby:
    version: 3.2.2
    path: /Users/jonnesten/.asdf/shims/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.73.6
    wanted: 0.73.6
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: true
  newArchEnabled: false

Stacktrace or Logs

`TypeError: _reactNative.AccessibilityInfo.announceForAccessibilityWithOptions is not a function`

Reproducer

n/a

Screenshots and Videos

No response

github-actions[bot] commented 7 months ago
:warning: Missing Reproducible Example
:information_source: We could not detect a reproducible example in your issue report. Please provide either:
  • If your bug is UI related: a Snack
  • If your bug is build/update related: use our Reproducer Template. A reproducer needs to be in a GitHub repository under your username.
Jonnboy91 commented 7 months ago

Was able to mock it in my own jest.setup.ts file like this:

jest.mock('react-native/Libraries/Components/AccessibilityInfo/AccessibilityInfo', () => {
  return {
    _esModule: true,
    default: {
      addEventListener: jest.fn(),
      announceForAccessibilityWithOptions: jest.fn(),
      isScreenReaderEnabled: jest.fn(() => Promise.resolve(false)),
    },
  };
});
Shivangi2909 commented 7 months ago

I am also facing similar issue with react native version 0.73.5. Here how my jest.config.js looks like:

module.exports = {
  preset: 'react-native',
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
  clearMocks: true,
  transform: {
    '^.+\\.(js|jsx|ts|tsx)$': 'babel-jest'
  },
  transformIgnorePatterns: [
    'node_modules/(?!@codler|(?!(jest-)?react-native)|@react-navigation|@sentry/react-native|native-base-shoutem-theme|@react-native-community|native-base)'
  ],
  collectCoverage: true,
  collectCoverageFrom: ['src/**/*.{js,jsx}'],
  coverageReporters: ['html'], // json-summary
  setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
  coverageDirectory: './coverage',
  moduleNameMapper: {
    '\\.(jpg|jpeg|png|gif|otf|webp|svg|ttf|mp4|webm|wav|mp3|m4a|aac)$':
      'identity-obj-proxy'
  },
  moduleDirectories: ['node_modules', __dirname],
  modulePathIgnorePatterns: ['node_modules']  

babel.config.js:

module.exports = (api) => {
  const babelEnv = api.env();
  const plugins = [];
  plugins.push([
    'babel-plugin-module-resolver',
    {
      root: ['./src']
    }
  ]);
  if (babelEnv !== 'development') {
    plugins.push(['transform-remove-console']);
  }
  return {
    presets: [
      ['module:@react-native/babel-preset'],
      [
        '@babel/preset-env',
        {
          targets: {
            node: 'current'
          }
        }
      ]
    ],
    plugins
  };
};

Can someone look into this issue please or do I need to change anything in jest config?

anabeatrizzz commented 1 month ago

+1