Skip to content

Commit

Permalink
Merge pull request #20282 from storybookjs/fix-failing-telemetry-test
Browse files Browse the repository at this point in the history
Telemetry: Fix flakey test issue
  • Loading branch information
shilman authored Dec 16, 2022
2 parents 1666a69 + a0d25cf commit 3fd14f3
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions code/lib/telemetry/src/telemetry.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/// <reference types="@types/jest" />;

/* eslint-disable no-plusplus */
import fetch from 'isomorphic-unfetch';

import { sendTelemetry } from './telemetry';
Expand Down Expand Up @@ -54,33 +53,36 @@ it('gives up if fetch repeatedly fails', async () => {
it('await all pending telemetry when passing in immediate = true', async () => {
let numberOfResolvedTasks = 0;

fetchMock.mockImplementation(async () => {
await Promise.resolve(null);
numberOfResolvedTasks += 1;
return { status: 200 };
});

// when we call sendTelemetry with immediate = true
// all pending tasks will be awaited
// to test this we add a few telemetry tasks that will be in the 'queue'
// we do NOT await these tasks!
sendTelemetry({
eventType: 'init',
payload: { foo: 'bar' },
}).then(() => {
numberOfResolvedTasks++;
});
sendTelemetry({
eventType: 'dev',
payload: { foo: 'bar' },
}).then(() => {
numberOfResolvedTasks++;
});

expect(fetch).toHaveBeenCalledTimes(2);
expect(numberOfResolvedTasks).toBe(0);

// here we await
await sendTelemetry(
{
eventType: 'error',
payload: { foo: 'bar' },
},
{ retryDelay: 0, immediate: true }
).then(() => {
numberOfResolvedTasks++;
});
);

expect(fetch).toHaveBeenCalledTimes(3);
expect(numberOfResolvedTasks).toBe(3);
Expand Down

0 comments on commit 3fd14f3

Please sign in to comment.