Here is the specific code snippet causing the issue:
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));
}
};
});
};
This is the test case for it:
it('should set up the Spotify SDK and provide a SpotifyPlayer instance', 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);
});
The complete error message:
✕ should set up the Spotify SDK and provide a SpotifyPlayer instance (5003 ms)
● spotifySdkService › should set up the Spotify SDK and provide a SpotifyPlayer instance
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 set up the Spotify SDK and provide a SpotifyPlayer instance', 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)
Can anyone offer insight into what might be going wrong here?