簡體   English   中英

我在開玩笑的測試中收到超時錯誤:拋出:超過 5000 毫秒的測試超時

[英]I am getting a timeOut error on a jest test: thrown: Exceeded timeout of 5000 ms for a test

這是發生這種情況的代碼:

    export const initializeSpotifySDK = async (token: string, trackId: string, contextUri: string, playbackStateChangeHandler: (state) => void, errorHandler: (message: string) => void): Promise<SpotifyPlayer> => {
      embedSpotifyScript();
    
      return new Promise(resolve => {
        window.onSpotifyWebPlaybackSDKReady = () => {
          try {
            // @ts-ignore
            const player = new Spotify.Player({
              name: 'Mira',
              getOAuthToken: callback => { callback(token); }
            });
    
            // Error handling - pass an error handler!!!
            player.addListener('initialization_error', ({ message }) => {
              errorHandler(message);
            });
            player.addListener('authentication_error', ({ message }) => {
              errorHandler(message);
            });
            player.addListener('account_error', ({ message }) => {
              errorHandler(message);
            });
            player.addListener('playback_error', ({ message }) => {
              errorHandler(message);
            });
    
            // Playback state handler - pass a handler as well!!!
            player.addListener('player_state_changed', state => { playbackStateChangeHandler(state); });
    
            player.addListener('ready', ({ device_id }) => {
              const spotifyPlayer = new SpotifyPlayer(player, device_id, trackId, contextUri, token, true);
              resolve(spotifyPlayer);
            });
    
            player.addListener('not_ready', ({ device_id }) => {
              const spotifyPlayer = new SpotifyPlayer(player, device_id, trackId, contextUri, token, false);
              resolve(spotifyPlayer);
            });
    
            player.connect();
          } catch (err) {
            logError(err);
            resolve(new SpotifyPlayer(null, '', '', token, '', false));
          }
        };
      });
    };

這是測試:

it('should initialize the Spotify SDK and return a SpotifyPlayer object', async () => {
    const token = 'abc123';
    const trackId = '123';
    const contextUri = 'spotify:album:456';
    const playbackStateChangeHandler = jest.fn();
    const errorHandler = jest.fn();

    const spotifyPlayer = await initializeSpotifySDK(
      token,
      trackId,
      contextUri,
      playbackStateChangeHandler,
      errorHandler
    );

    console.log({ spotifyPlayer });

    expect(spotifyPlayer).toBeInstanceOf(SpotifyPlayer);
    expect(spotifyPlayer.deviceId).toBeDefined();
    expect(spotifyPlayer.trackId).toEqual(trackId);
    expect(spotifyPlayer.contextUri).toEqual(contextUri);
    expect(spotifyPlayer.token).toEqual(token);
  });

整個錯誤

    ✕ should initialize the Spotify SDK and return a SpotifyPlayer object (5003 ms)

  ● spotifySdkService › should initialize the Spotify SDK and return a SpotifyPlayer object

    thrown: "Exceeded timeout of 5000 ms for a test.
    Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

      108 |   });
      109 |
    > 110 |   it('should initialize the Spotify SDK and return a SpotifyPlayer object', async () => {
          |   ^
      111 |     const token = 'abc123';
      112 |     const trackId = '123';
      113 |     const contextUri = 'spotify:album:456';

      at services/spotifySdkService.spec.ts:110:3
      at Object.<anonymous> (services/spotifySdkService.spec.ts:17:1)
      at TestScheduler.scheduleTests (../node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (../node_modules/@jest/core/build/runJest.js:404:19)
      at _run10000 (../node_modules/@jest/core/build/cli/index.js:320:7)
      at runCLI (../node_modules/@jest/core/build/cli/index.js:173:3)

關於我做錯了什么的任何想法?

錯誤消息准確地告訴您出了什么問題:您正在測試的代碼需要超過 5 秒才能完成,這對於發出 inte.net 請求的東西來說並不意外。

  1. 我會首先按照錯誤提示進行操作,以確保您的測試功能正常運行,即使速度非常慢:

     jest.setTimeout(60000) // a whole minute!
  2. 如果失敗,請將其縮短到 5 分鍾,然后重試(並等待)。

    如果它有效,即使它不有效,您也可以嘗試通過在最有可能花費很長時間的代碼周圍插入計時測量來隔離究竟是什么花費了這么長時間(或沒有返回):

     const start = Date.now() console.log(`starting...`) player.connect() console.log(`elapsed millis: ${Date.now() - start}`)
  3. 如果您看到starting...但即使經過漫長的等待也沒有經過毫秒,那么兩個控制台日志語句之間的代碼似乎掛在某處。

  4. 如果您甚至從未在控制台中看到starting... ,那么要么是開玩笑正在抑制控制台 output 要么是在您選擇測量的代碼之前發生的任何花費很長時間的事情。

    • 從一個簡單的腳本中調用您的 function 以消除圖片中的笑話。 這將使事情變得簡單,並會發現 jest 或你如何使用它是問題的根源(我懷疑它 0.

    • 如果你仍然沒有看到starting...然后移動這些行:

       const start = Date.now() console.log(`starting...`)

      到您的 function 的頂部。

    • 如果您仍然看不到它,那么您做錯了什么。

  5. 如果您看到經過的時間,並且它很長,即大於 5000,請通過移動計時語句來縮小導致它的行。 如果它是對您的一個函數的單個 function 調用,您可以將計時語句移動到該 function 的內部並縮小其中的源代碼。

我懷疑player.connect()是什么花了這么長時間,這可能是正常的,也可能是您的 inte.net 連接有問題,或者可能是您在發出 Spotify 請求時使用的權限/憑據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM