亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

jest.runAllTimers() 拋出 TypeError

jest.runAllTimers() 拋出 TypeError

慕萊塢森 2023-04-20 16:58:24
我正在嘗試使用中的內置react-scripts test腳本運行以下測試create-react-app:Timer.test.jsrender(<Timer />)const pauseButton = screen.getByText('pause')const timerOutput = screen.getAllByRole('heading')[1]describe('Timer', () => {  test('renders Timer component', () => {    expect(screen.getByText(/session/i)).toBeInTheDocument()    expect(screen.getByText(/25/)).toBeInTheDocument()  })  test('counts down when unpaused', async () => {    jest.useFakeTimers()    fireEvent.click(pauseButton)    setTimeout(      fireEvent.click(pauseButton),      1125    )    jest.runAllTimers()    expect(timerOutput).toHaveTextContent('24:59')  })})Jest 似乎工作正常,直到它到達jest.runAllTimers(),當我收到以下錯誤時: TypeError: callback.apply is not a function      23 |       1125      24 |     )    > 25 |     jest.runAllTimers()         |          ^      26 |     expect(timerOutput).toHaveTextContent('24:59')      27 |   })      28 |       at node_modules/@jest/fake-timers/build/jestFakeTimers.js:524:25      at callback (node_modules/@jest/fake-timers/build/jestFakeTimers.js:516:29)      at FakeTimers._runTimerHandle (node_modules/@jest/fake-timers/build/jestFakeTimers.js:560:9)      at FakeTimers.runAllTimers (node_modules/@jest/fake-timers/build/jestFakeTimers.js:193:12)      at Object.<anonymous> (src/features/Timer.test.js:25:10)我不知道發生了什么事。為什么它不能完成運行測試?
查看完整描述

1 回答

?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

你得到的錯誤可能是因為setTimeout使用。setTimeout接受一個函數作為第一個參數,但無論fireEvent.click(pauseButton). 將代碼更改為:


test('counts down when unpaused', async () => {

    jest.useFakeTimers();

    fireEvent.click(pauseButton)

    setTimeout(() =>  fireEvent.click(pauseButton), 1125);

            // ^ NOTE HERE

    jest.runAllTimers();

    expect(timerOutput).toHaveTextContent('24:59');

  })


查看完整回答
反對 回復 2023-04-20
  • 1 回答
  • 0 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號