import { renderHook, act } from '@testing-library/react-hooks'
import useRefWithSetter from '../useRefWithSetter'
describe('Test useRefWithSetter', () => {
const { result } = renderHook(() => useRefWithSetter(1))
const [valueRef, setValue] = result.current
test('you can return value through setter function', () => {
act(() => {
setValue(2)
})
expect(valueRef.current).toBe(2)
})
test('you can return callback function through setter function', async () => {
await act(async () => {
setValue((prev) => prev + 3)
return Promise.resolve()
})
expect(valueRef.current).toBe(5)
})
})
错误消息:[eslint @typescript-eslint/no-floating-promises] [E] Promise 必须适当处理或明确标记为忽略 void 运算符。(@typescript-eslint/no-floating-promises)
你好。我正在尝试测试我的自定义钩子,但我不知道如何使用 react-hooks-testing-library 中的 'act' 函数。它显示 lint 错误,所以我试图像第二次测试一样修复,但我认为这不合适,因为该钩子不是 Promise 函数。有没有办法用void
操作员明确标记为忽略?