【问题标题】:Jest Unit Tests: "SyntaxError: Cannot use import statement outside a module" PushNotificationsIOS开玩笑单元测试:“SyntaxError:不能在模块外使用导入语句”PushNotificationsIOS
【发布时间】:2021-08-14 04:24:29
【问题描述】:

在为导入 PushNotificationsIOS 的组件运行快照测试时遇到此错误。

"react-native": "0.63.4",
"@react-native-community/push-notification-ios": "^1.8.0",






import {NativeEventEmitter, NativeModules} from 'react-native';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | import { Platform, AppState, Alert } from 'react-native';
    > 2 | import PushNotificationIOS from '@react-native-community/push-notification-ios';

【问题讨论】:

    标签: react-native jestjs babel-jest react-native-push-notification


    【解决方案1】:

    通过在我们的 mocks.setUpTest.js 文件中模拟 PushNotificationIOS 来解决:

    jest.mock('@react-native-community/push-notification-ios', () => ({
      checkPermissions: jest.fn(),
      requestPermissions: jest.fn(),
      addEventListener: jest.fn(),
      addNotificationRequest: jest.fn(),
      cancelLocalNotifications: jest.fn(),
    }));
    

    【讨论】:

      猜你喜欢
      • 2020-12-02
      • 2023-01-31
      • 2020-06-11
      • 1970-01-01
      • 1970-01-01
      • 2019-01-07
      • 2021-03-22
      • 2020-12-15
      • 1970-01-01
      相关资源
      最近更新 更多