Skip to content

Commit 6dc22fd

Browse files
authored
chore(zero-client): Fix flaky test (#3711)
By exposing the underlying persist method to tests and use that
1 parent 7d14247 commit 6dc22fd

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

packages/zero-client/src/client/test-utils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ import type {
2424
import {upstreamSchema} from '../../../zero-protocol/src/up.ts';
2525
import type {Schema} from '../../../zero-schema/src/builder/schema-builder.ts';
2626
import * as ConnectionState from './connection-state-enum.ts';
27+
import type {CustomMutatorDefs} from './custom.ts';
2728
import type {LogOptions} from './log-options.ts';
2829
import type {ZeroOptions} from './options.ts';
2930
import {
3031
type TestingContext,
3132
Zero,
3233
createLogOptionsSymbol,
3334
exposedToTestingSymbol,
35+
getInternalReplicacheImplForTesting,
3436
onSetConnectionStateSymbol,
3537
} from './zero.ts';
36-
import type {CustomMutatorDefs} from './custom.ts';
3738

3839
type ConnectionState = Enum<typeof ConnectionState>;
3940
type ErrorKind = Enum<typeof ErrorKind>;
@@ -204,6 +205,11 @@ export class TestZero<
204205
assert(TESTING);
205206
this[exposedToTestingSymbol].setReload(r);
206207
}
208+
209+
persist(): Promise<void> {
210+
assert(TESTING);
211+
return getInternalReplicacheImplForTesting(this).persist();
212+
}
207213
}
208214

209215
declare const TESTING: boolean;

packages/zero-client/src/client/zero.test.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ import {
5858
RUN_LOOP_INTERVAL_MS,
5959
} from './zero.ts';
6060

61-
let realSetTimeout: typeof setTimeout;
6261
let clock: sinon.SinonFakeTimers;
6362
const startTime = 1678829450000;
6463

@@ -68,7 +67,6 @@ let fetchStub: sinon.SinonStub<
6867
>;
6968

7069
beforeEach(() => {
71-
realSetTimeout = setTimeout;
7270
clock = sinon.useFakeTimers();
7371
clock.setSystemTime(startTime);
7472
sinon.replace(
@@ -2248,21 +2246,20 @@ test('kvStore option', async () => {
22482246
],
22492247
}),
22502248
});
2251-
const idIsAView = r.query.e.where('id', '=', 'a').materialize();
2252-
const allDataView = r.query.e.materialize();
22532249

2254-
// Firefox is flaky... it takes longer time than Chromium and WebKit.
2255-
// We therefore give it a few times to pass the expectation.
2250+
// Use persist as a way to ensure we have read the data out of IDB.
2251+
await r.persist();
22562252

2257-
await tickAFewTimes(clock);
2258-
await new Promise(resolve => realSetTimeout(resolve, 100));
2253+
const idIsAView = r.query.e.where('id', '=', 'a').materialize();
2254+
const allDataView = r.query.e.materialize();
22592255
expect(allDataView.data).deep.equal(expectedValue);
2256+
22602257
await r.mutate.e.insert({id: 'a', value: 1});
2261-
await tickAFewTimes(clock);
22622258

22632259
expect(idIsAView.data).deep.equal([{id: 'a', value: 1}]);
22642260
// Wait for persist to finish
2265-
await tickAFewTimes(clock, 2000);
2261+
await r.persist();
2262+
22662263
await r.close();
22672264
expect(spy.called).equal(expectedIDBOpenCalled, 'IDB existed!');
22682265

packages/zero-client/src/client/zero.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,11 @@ export interface ReplicacheInternalAPI {
246246

247247
const internalReplicacheImplMap = new WeakMap<object, ReplicacheImpl>();
248248

249-
export function getInternalReplicacheImplForTesting<
250-
MD extends MutatorDefs,
251-
S extends Schema,
252-
>(z: Zero<S>): ReplicacheImpl<MD> {
253-
return must(internalReplicacheImplMap.get(z)) as ReplicacheImpl<MD>;
249+
export function getInternalReplicacheImplForTesting(
250+
z: object,
251+
): ReplicacheImpl<MutatorDefs> {
252+
assert(TESTING);
253+
return must(internalReplicacheImplMap.get(z));
254254
}
255255

256256
export class Zero<

0 commit comments

Comments
 (0)