te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Getting ''Invariant Violation: Native module cannot be null.'' when I run the test
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting ''Invariant Violation: Native module cannot be null.'' when I run the test

programmeradmin3浏览0评论

I have a Login ponent as below and I am writing some test cases for this ponent. 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 'ponents/Wrappers/KeyboardAvoidingWrapper'

export default class AuthScreen extends Component {
  state = {

  }

  toggleRecovery = e => {

    )
  }

  loginAttempt = data => {

  }

  recoverAttempt = data => {

  }

  ponentWillUnmount() {

  }

  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 ponent does not get imported correctly? I could not figure out the why this happening. How can I solve this issue?

I have a Login ponent as below and I am writing some test cases for this ponent. 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 'ponents/Wrappers/KeyboardAvoidingWrapper'

export default class AuthScreen extends Component {
  state = {

  }

  toggleRecovery = e => {

    )
  }

  loginAttempt = data => {

  }

  recoverAttempt = data => {

  }

  ponentWillUnmount() {

  }

  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 ponent does not get imported correctly? I could not figure out the why this happening. How can I solve this issue?

Share Improve this question edited Jan 24, 2021 at 14:42 peterh 1 asked Dec 21, 2018 at 5:40 JonnyJonny 8833 gold badges16 silver badges26 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

This problem happens when you import a native ponent in the render tree, as the test renderer do not have them. To fix this, either you need to mock the ponent (https://jestjs.io/docs/en/manual-mocks), or use shallow rendering (https://reactjs/docs/shallow-renderer.html)

For OP's particular ponent, this is the github issue to help you: https://github./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)

Note that really "native" tests, like Android's JUnit or iOS's XCTest, can run on the device or emulator or simulator.

Hence the need to "Mock" GUI is only a limitation of react-native and/or Jest, where to test modules which require "native integration" you need to learn both Android's JUnit and iOS's XCTest.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论