反应本机测试-错误:无效的挂钩调用

时间:2020-09-30 10:36:07

标签: javascript react-native react-hooks react-hooks-testing-library

我在测试以下代码时遇到问题:

import {useEffect} from 'react';
export const useButtonPress = () => {
  console.log('RANG', 'ccc', 1);
  useEffect(() => {
    console.log('RANG', 'ccc', 2);
    return () => {};
  });
}

我有一个简单的测试:

describe('useButtonPress', () => {
  const mockComponentId = '123';
  const mockButtonId = 'btn';
  let uut: any;
  beforeEach(() => {
    uut = require('./useNavigationButtonPress').useNavigationButtonPress
  });
  test.only('unsubscribes on unmount', async () => {
    const mockHandler = jest.fn();
    const {unmount, result} = renderHook(() =>
      uut()
    );
    console.log('RANG', 'result.error', result.error);
    unmount();
  });

RANG 2从未打印过,我得到了result.error

[Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
        1. You might have mismatching versions of React and the renderer (such as React DOM)
        2. You might be breaking the Rules of Hooks
        3. You might have more than one copy of React in the same app
        See bla.com for tips about how to debug and fix this problem.]

有帮助吗?

我检查了一下,并且有一个react@16.13.1版本

0 个答案:

没有答案