Skip to content

Commit c9b0a1d

Browse files
committed
Fix common tests.
1 parent f449f1d commit c9b0a1d

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

packages/shared/common/__tests__/contextDeduplicator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { Context, subsystem } from '@common';
1+
import { subsystem } from '../src/api';
2+
import Context from '../src/Context';
23

34
export default class ContextDeduplicator implements subsystem.LDContextDeduplicator {
45
flushInterval?: number | undefined = 0.1;

packages/shared/common/__tests__/createBasicPlatform.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { PlatformData, SdkData } from '../src/api';
2+
import { setupCrypto } from './setupCrypto';
23

34
const setupInfo = () => ({
45
platformData: jest.fn(

packages/shared/common/__tests__/internal/events/EventSender.test.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import { createBasicPlatform } from '../../createBasicPlatform';
66

77
let mockPlatform: ReturnType<typeof createBasicPlatform>;
88

9+
function runWithTimers<T>(fn: () => Promise<T>) {
10+
const promise = fn();
11+
return jest.runAllTimersAsync().then(() => promise);
12+
}
13+
914
beforeEach(() => {
1015
mockPlatform = createBasicPlatform();
1116
});
1217

13-
jest.mock('../../utils', () => {
14-
const actual = jest.requireActual('../../utils');
15-
return { ...actual, sleep: jest.fn() };
16-
});
17-
1818
const basicConfig = {
1919
tags: new ApplicationTags({ application: { id: 'testApplication1', version: '1.0.0' } }),
2020
serviceEndpoints: {
@@ -119,9 +119,8 @@ describe('given an event sender', () => {
119119
},
120120
);
121121

122-
eventSenderResult = await eventSender.sendEventData(
123-
LDEventType.AnalyticsEvents,
124-
testEventData1,
122+
eventSenderResult = await runWithTimers(() =>
123+
eventSender.sendEventData(LDEventType.AnalyticsEvents, testEventData1),
125124
);
126125
});
127126

@@ -141,9 +140,8 @@ describe('given an event sender', () => {
141140

142141
it('includes the payload', async () => {
143142
const { status: status1 } = eventSenderResult;
144-
const { status: status2 } = await eventSender.sendEventData(
145-
LDEventType.DiagnosticEvent,
146-
testEventData2,
143+
const { status: status2 } = await runWithTimers(() =>
144+
eventSender.sendEventData(LDEventType.DiagnosticEvent, testEventData2),
147145
);
148146

149147
expect(status1).toEqual(LDDeliveryStatus.Succeeded);
@@ -167,7 +165,9 @@ describe('given an event sender', () => {
167165

168166
it('sends a unique payload for analytics events', async () => {
169167
// send the same request again to assert unique uuids
170-
await eventSender.sendEventData(LDEventType.AnalyticsEvents, testEventData1);
168+
await runWithTimers(() =>
169+
eventSender.sendEventData(LDEventType.AnalyticsEvents, testEventData1),
170+
);
171171

172172
expect(mockFetch).toHaveBeenCalledTimes(2);
173173
expect(mockFetch).toHaveBeenNthCalledWith(
@@ -189,9 +189,8 @@ describe('given an event sender', () => {
189189
describe.each([400, 408, 429, 503])('given recoverable errors', (responseStatusCode) => {
190190
beforeEach(async () => {
191191
setupMockFetch(responseStatusCode);
192-
eventSenderResult = await eventSender.sendEventData(
193-
LDEventType.AnalyticsEvents,
194-
testEventData1,
192+
eventSenderResult = await runWithTimers(() =>
193+
eventSender.sendEventData(LDEventType.AnalyticsEvents, testEventData1),
195194
);
196195
});
197196

@@ -209,9 +208,8 @@ describe('given an event sender', () => {
209208

210209
it('given a result for too large of a payload', async () => {
211210
setupMockFetch(413);
212-
eventSenderResult = await eventSender.sendEventData(
213-
LDEventType.AnalyticsEvents,
214-
testEventData1,
211+
eventSenderResult = await runWithTimers(() =>
212+
eventSender.sendEventData(LDEventType.AnalyticsEvents, testEventData1),
215213
);
216214

217215
const errorMessage = `Received error 413 for event posting - giving up permanently`;
@@ -227,9 +225,8 @@ describe('given an event sender', () => {
227225
describe.each([401, 403])('given unrecoverable errors', (responseStatusCode) => {
228226
beforeEach(async () => {
229227
setupMockFetch(responseStatusCode);
230-
eventSenderResult = await eventSender.sendEventData(
231-
LDEventType.AnalyticsEvents,
232-
testEventData1,
228+
eventSenderResult = await runWithTimers(() =>
229+
eventSender.sendEventData(LDEventType.AnalyticsEvents, testEventData1),
233230
);
234231
});
235232

0 commit comments

Comments
 (0)