Skip to content

Commit 8f93ccd

Browse files
committed
frontend: Storyshots: Add check for missing network request mocks
1 parent a35312e commit 8f93ccd

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

frontend/src/storybook.test.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ describe('Storybook Tests', () => {
165165
worker.events.on('request:start', onStart);
166166
worker.events.on('request:end', onEnd);
167167

168+
const unhandledRequests: Array<string> = [];
169+
170+
function onUnhandledRequest(e: { request: Request }) {
171+
unhandledRequests.push(e.request.url);
172+
}
173+
worker.events.on('request:unhandled', onUnhandledRequest);
174+
168175
act(() => {
169176
previewAnnotations.queryClient.clear();
170177
});
@@ -189,15 +196,30 @@ describe('Storybook Tests', () => {
189196
}
190197
});
191198

199+
expect(
200+
unhandledRequests,
201+
'MSW: intercepted a request without a matching request handler. Please create a request handler for it'
202+
).toEqual([]);
203+
192204
await waitFor(() => {
193205
if (previewAnnotations.queryClient.isFetching()) {
194-
throw new Error('The react-query is still fetching');
206+
const pendingQueries = previewAnnotations.queryClient
207+
.getQueryCache()
208+
.findAll({ fetchStatus: 'fetching' });
209+
210+
throw new Error(
211+
'The react-query is still fetching following queries:\n' +
212+
pendingQueries
213+
.map((it, i) => String(i + 1) + ': ' + JSON.stringify(it.queryKey))
214+
.join('\n')
215+
);
195216
}
196217
});
197218

198219
// Cleanup listeners
199220
worker.events.removeListener('request:start', onStart);
200221
worker.events.removeListener('request:end', onEnd);
222+
worker.events.removeListener('request:unhandled', onUnhandledRequest);
201223

202224
// Put snapshot next to the story
203225
const snapshotPath = path.join(

0 commit comments

Comments
 (0)