Skip to content

Commit 466b165

Browse files
authored
chore: Fix timing issues for flaky tests. (#193)
1 parent 8abb48f commit 466b165

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

packages/shared/sdk-server/__tests__/LDClient.evaluation.test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { LDClientImpl } from '../src';
2-
import { LDFeatureStore } from '../src/api/subsystems';
2+
import { LDFeatureStore, LDStreamProcessor } from '../src/api/subsystems';
33
import NullUpdateProcessor from '../src/data_sources/NullUpdateProcessor';
44
import TestData from '../src/integrations/test_data/TestData';
55
import AsyncStoreFacade from '../src/store/AsyncStoreFacade';
@@ -158,6 +158,17 @@ describe('given an offline client', () => {
158158
});
159159
});
160160

161+
class InertUpdateProcessor implements LDStreamProcessor {
162+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
163+
start(fn?: ((err?: any) => void) | undefined) {
164+
// Never initialize.
165+
}
166+
167+
stop() {}
168+
169+
close() {}
170+
}
171+
161172
describe('given a client and store that are uninitialized', () => {
162173
let store: LDFeatureStore;
163174
let client: LDClientImpl;
@@ -178,7 +189,7 @@ describe('given a client and store that are uninitialized', () => {
178189
'sdk-key',
179190
basicPlatform,
180191
{
181-
updateProcessor: new NullUpdateProcessor(),
192+
updateProcessor: new InertUpdateProcessor(),
182193
sendEvents: false,
183194
featureStore: store,
184195
},

packages/shared/sdk-server/__tests__/LDClientImpl.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ it('isOffline returns true in offline mode', (done) => {
5454

5555
describe('when waiting for initialization', () => {
5656
let client: LDClientImpl;
57+
let resolve: Function;
5758

5859
beforeEach(() => {
5960
client = new LDClientImpl(
@@ -62,9 +63,7 @@ describe('when waiting for initialization', () => {
6263
{
6364
updateProcessor: {
6465
start: (fn: (err?: any) => void) => {
65-
setTimeout(() => {
66-
fn();
67-
}, 0);
66+
resolve = fn;
6867
},
6968
stop: () => {},
7069
close: () => {},
@@ -81,17 +80,20 @@ describe('when waiting for initialization', () => {
8180
});
8281

8382
it('resolves when ready', async () => {
83+
resolve();
8484
await client.waitForInitialization();
8585
});
8686

8787
it('resolves immediately if the client is already ready', async () => {
88+
resolve();
8889
await client.waitForInitialization();
8990
await client.waitForInitialization();
9091
});
9192

9293
it('creates only one Promise', async () => {
9394
const p1 = client.waitForInitialization();
9495
const p2 = client.waitForInitialization();
96+
resolve();
9597
expect(p2).toBe(p1);
9698
});
9799
});

0 commit comments

Comments
 (0)