简体   繁体   English

运行测试时出现“不变违规:本机模块不能为空”。

[英]Getting ''Invariant Violation: Native module cannot be null.'' when I run the test

I have a Login component as below and I am writing some test cases for this component.我有一个登录组件,如下所示,我正在为这个组件编写一些测试用例。 When I tried to run the test I got the following error:当我尝试运行测试时,出现以下错误:

Test测试

import renderer from 'react-test-renderer'

import Login from '../Login'
let props, wrapper

beforeEach(() => {
  props = {
    loginAttempt: jest.fn(),
    recoverAttempt: jest.fn(),
    reset: jest.fn()
  }
  wrapper = shallow(<Login {...props} />)
})

describe('tests for <Login />', () => {
  test('should have a formProvider with handlesubmit atribute', () => {
    const value = wrapper.find('FormProvider')
    expect(value.length).toBe(1)
  })
})

//Snapshot test
test('Snapshot test for the Contact form', () => {
  const tree = renderer.create(<Login {...props} />).toJSON()
  expect(tree).toMatchSnapshot()
})

Component成分

import React, { Component } from 'react'
import KeyboardAvoidingWrapper from 'components/Wrappers/KeyboardAvoidingWrapper'

export default class AuthScreen extends Component {
  state = {

  }

  toggleRecovery = e => {

    )
  }

  loginAttempt = data => {

  }

  recoverAttempt = data => {

  }

  componentWillUnmount() {

  }

  render() {
    let { loginAttempt, toggleRecovery, recoverAttempt, state, props } = this
    let { recovery } = state
    let { error, fetching } = props
    return (
      <KeyboardAvoidingWrapper enabled={false} behavior="padding" fluid>

    UI GOES HERE..

      </KeyboardAvoidingWrapper>
    )
  }
}

Error错误

  ● Test suite failed to run

    Invariant Violation: Native module cannot be null.

      at invariant (node_modules/react-native/node_modules/fbjs/lib/invariant.js:40:15)
      at new NativeEventEmitter (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:36:36)
      at Object.<anonymous> (node_modules/react-native-safari-view/SafariViewManager.ios.js:12:20)
      at Object.<anonymous> (node_modules/react-native-safari-view/index.js:1:238)

Why I am getting this error?为什么我收到这个错误? Is it because the component does not get imported correctly?是不是因为组件没有正确导入? I could not figure out the why this happening.我无法弄清楚为什么会发生这种情况。 How can I solve this issue?我该如何解决这个问题?

This problem happens when you import a native component in the render tree, as the test renderer do not have them.当您在渲染树中导入本机组件时会发生此问题,因为测试渲染器没有它们。 To fix this, either you need to mock the component ( https://jestjs.io/docs/en/manual-mocks ), or use shallow rendering ( https://reactjs.org/docs/shallow-renderer.html )要解决此问题,您需要模拟组件 ( https://jestjs.io/docs/en/manual-mocks ),或使用浅层渲染 ( https://reactjs.org/docs/shallow-renderer.html )

For your particular case, this is the github issue to help you: https://github.com/naoufal/react-native-safari-view/issues/99对于您的特定情况,这是帮助您的 github 问题: https : //github.com/naoufal/react-native-safari-view/issues/99

Another solution could be using react-native-mock-render module (the most active fork of react-native-mock )另一种解决方案可能是使用react-native-mock-render模块( react-native-mock最活跃的分支)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不变违规:Native Module 不能是 Null。 错误仅出现在 iOS - Invariant Violation: Native Module cannot be Null. Error only showing up on iOS 开玩笑,反应本机和异步存储:不变违规:本机模块不能是 null - Jest, React native and async-storage : Invariant Violation: Native module cannot be null 升级React Native和依赖项时始终不变 - Invariant Violation when upgrading React Native and dependencies 我收到一个未捕获的类型错误:无法读取 null 的属性“ID”。 仅在尝试访问对象的最后两项时 - I'm getting an Uncaught TypeError: Cannot read property 'ID' of null. Only when trying to access the last two items of an Object 我为什么会遇到永久违规:永久违规:超出最大更新深度。 在setState上? - Why am I getting Invariant Violation: Invariant Violation: Maximum update depth exceeded. on setState? 使用自定义输入组件时的 React-Native Invariant Violation - React-Native Invariant Violation When Using Custom Input Component 反应本机不变违反错误 - React native invariant violation error React Native - 不变违规“RNDateTimePicker” - React Native - Invariant Violation "RNDateTimePicker" Jest 和 Enzyme,即使已经在测试中包装了路由器,也仍然违反了不变性 - Jest and Enzyme, Invariant Violation even when already wrapping a Router on the test 运行测试时出错“找不到模块” - Error when run the test "Cannot find module "
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM