类型错误:(0,_reactTestRenderer.act)不是一个函数

问题描述 投票:0回答:2

我在测试用例中使用

renderHook
时收到此错误。不知道是什么问题。

TypeError: (0 , _reactTestRenderer.act) is not a function

  13 | 
  14 |   it('allows to upload photos through the mutation', async () => {
> 15 |     const { result, nextUpdate } = renderHook(() =>
     |                                    ^
  16 |       usePhotosUpload({
  17 |         photosUploadMutation,
  18 |       }),

  at renderHook (../../../node_modules/react-hooks-testing-library/lib/index.js:151:30)
  at Object._callee$ (__test__/hooks/usePhotosUpload.test.js:15:36)
  at tryCatch (../../../node_modules/regenerator-runtime/runtime.js:62:40)
  at Generator.invoke [as _invoke] (../../../node_modules/regenerator-runtime/runtime.js:296:22)
  at Generator.prototype.(anonymous function) [as next] (../../../node_modules/regenerator-runtime/runtime.js:114:21)
  at asyncGeneratorStep (__test__/hooks/usePhotosUpload.test.js:13:103)
  at _next (__test__/hooks/usePhotosUpload.test.js:15:194)
  at __test__/hooks/usePhotosUpload.test.js:15:364
  at Object.<anonymous> (__test__/hooks/usePhotosUpload.test.js:15:97)
jestjs enzyme
2个回答
7
投票

要解决此问题,请在您的 package.json 文件中使 react-test-renderer 版本与 react

 相同。


0
投票
尝试直接在 import 语句中指定渲染器的类型:

import { renderHook, act } from '@testing-library/react-hooks/dom' // will use react-dom import { renderHook, act } from '@testing-library/react-hooks/native' // will use react-test-renderer import { renderHook, act } from '@testing-library/react-hooks/server' // will use react-dom/server
    
© www.soinside.com 2019 - 2024. All rights reserved.