我寫了一個玩笑測試來覆蓋被調用的 onBlur 事件。我正在使用 react-testing-library 和 material-ui。下面是測試代碼:test('fires onBlur handler', async () => { const spy = jest.fn() const { getByPlaceholderText } = render( <FormEmailAsync placeholder={'Your Email'} onBlurred={data => spy(data)} /> ) const fld = getByPlaceholderText('Your Email') expect(fld) userEvent.type(fld, '[email protected]') expect(spy).not.toHaveBeenCalled() fireEvent.blur(fld) expect(spy).toHaveBeenCalledTimes(1) expect(spy).toHaveBeenCalledWith('[email protected]') })我試圖斷言的代碼 const [theState, asyncFunc] = useAsyncFn(async value => { const returnedSchema = await schema.validate(value) return returnedSchema }, []) const onBlurFunc = async (name, event) => { let returnFromAsync = await asyncFunc(event) console.log(returnFromAsync) onBlurred(returnFromAsync) }我試圖設置一個代碼沙盒,但不幸的是,這失敗了,關于將測試包裝在一個act塊中的錯誤。這不會發生在我的本地環境中。我如何才能成功完成此測試?
Jest 無法在異步函數上調用 spy,但在瀏覽器控制臺中調用了函數
Cats萌萌
2021-10-07 10:27:46