From 96fb6c2c561d0e583f5ae2d63445665e31fc34e7 Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Fri, 1 Jul 2022 11:30:51 +0200 Subject: [PATCH] Better error message --- lib/preview-web/src/PreviewWeb.mockdata.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/preview-web/src/PreviewWeb.mockdata.ts b/lib/preview-web/src/PreviewWeb.mockdata.ts index d7231a297c3d..978539e1f274 100644 --- a/lib/preview-web/src/PreviewWeb.mockdata.ts +++ b/lib/preview-web/src/PreviewWeb.mockdata.ts @@ -116,7 +116,8 @@ export const mockChannel = { export const waitForEvents = ( events: string[], - predicate: (...args: any[]) => boolean = () => true + predicate: (...args: any[]) => boolean = () => true, + debugLabel?: string ) => { // We've already emitted a render event. NOTE if you want to test a second call, // ensure you call `mockChannel.emit.mockClear()` before `waitFor...` @@ -137,7 +138,9 @@ export const waitForEvents = ( events.forEach((event) => mockChannel.on(event, listener)); // Don't wait too long - waitForQuiescence().then(() => reject(new Error('Event was not emitted in time'))); + waitForQuiescence().then(() => + reject(new Error(`Event was not emitted in time: ${debugLabel || events}`)) + ); }); }; @@ -152,8 +155,10 @@ export const waitForRender = () => STORY_MISSING, ]); -export const waitForRenderPhase = (phase: RenderPhase) => - waitForEvents([STORY_RENDER_PHASE_CHANGED], ({ newPhase }) => newPhase === phase); +export const waitForRenderPhase = (phase: RenderPhase) => { + const label = `${STORY_RENDER_PHASE_CHANGED} to ${phase}`; + return waitForEvents([STORY_RENDER_PHASE_CHANGED], ({ newPhase }) => newPhase === phase, label); +}; // A little trick to ensure that we always call the real `setTimeout` even when timers are mocked const realSetTimeout = setTimeout;