Jest 单元测试:“语法错误:不能在模块外使用导入语句” PushNotificationsIOS

时间:2021-05-25 22:13:13

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

在玩笑中为导入 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';

1 个答案:

答案 0 :(得分:0)

通过在我们的 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(),
}));