Skip to content

Commit 83fed17

Browse files
authored
Deflake getInstallationEntry tests (#2563)
1 parent 01da30b commit 83fed17

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

packages/installations/src/helpers/get-installation-entry.test.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ describe('getInstallationEntry', () => {
4444
>;
4545

4646
beforeEach(() => {
47-
clock = useFakeTimers({ now: 1_000_000, shouldAdvanceTime: true });
47+
clock = useFakeTimers({ now: 1_000_000 });
4848
appConfig = getFakeAppConfig();
4949
createInstallationRequestSpy = stub(
5050
createInstallationRequestModule,
5151
'createInstallationRequest'
5252
).callsFake(
5353
async (_, installationEntry): Promise<RegisteredInstallationEntry> => {
54-
await sleep(100); // Request would take some time
54+
await sleep(500); // Request would take some time
5555
const registeredInstallationEntry: RegisteredInstallationEntry = {
5656
// Returns new FID if client FID is invalid.
5757
fid: installationEntry.fid || FID,
@@ -69,6 +69,11 @@ describe('getInstallationEntry', () => {
6969
);
7070
});
7171

72+
afterEach(() => {
73+
// Clean up all pending requests.
74+
clock.runAll();
75+
});
76+
7277
it('saves the InstallationEntry in the database before returning it', async () => {
7378
const oldDbEntry = await get(appConfig);
7479
expect(oldDbEntry).to.be.undefined;
@@ -113,7 +118,7 @@ describe('getInstallationEntry', () => {
113118

114119
it('saves the InstallationEntry in the database when registration fails', async () => {
115120
createInstallationRequestSpy.callsFake(async () => {
116-
await sleep(100); // Request would take some time
121+
await sleep(500); // Request would take some time
117122
throw ERROR_FACTORY.create(ErrorCode.REQUEST_FAILED, {
118123
requestName: 'Create Installation',
119124
serverCode: 500,
@@ -143,7 +148,7 @@ describe('getInstallationEntry', () => {
143148

144149
it('removes the InstallationEntry from the database when registration fails with 409', async () => {
145150
createInstallationRequestSpy.callsFake(async () => {
146-
await sleep(100); // Request would take some time
151+
await sleep(500); // Request would take some time
147152
throw ERROR_FACTORY.create(ErrorCode.REQUEST_FAILED, {
148153
requestName: 'Create Installation',
149154
serverCode: 409,
@@ -259,6 +264,12 @@ describe('getInstallationEntry', () => {
259264
});
260265

261266
it('waits for the FID from the server if FID generation fails', async () => {
267+
clock.restore();
268+
clock = useFakeTimers({
269+
now: 1_000_000,
270+
shouldAdvanceTime: true /* Needed to allow the createInstallation request to complete. */
271+
});
272+
262273
// FID generation fails.
263274
generateInstallationEntrySpy.returns(generateFidModule.INVALID_FID);
264275

@@ -340,12 +351,16 @@ describe('getInstallationEntry', () => {
340351
});
341352

342353
it('updates the InstallationEntry and triggers createInstallation if the request fails', async () => {
343-
clock.now = 1_001_000; // One second after the request was initiated.
354+
clock.restore();
355+
clock = useFakeTimers({
356+
now: 1_001_000 /* One second after the request was initiated. */,
357+
shouldAdvanceTime: true /* Needed to allow the createInstallation request to complete. */
358+
});
344359

345360
const installationEntryPromise = getInstallationEntry(appConfig);
346361

347362
// The pending request fails after a while.
348-
clock.tick(500);
363+
clock.tick(3000);
349364
await set(appConfig, {
350365
fid: FID,
351366
registrationStatus: RequestStatus.NOT_STARTED
@@ -354,13 +369,13 @@ describe('getInstallationEntry', () => {
354369
const { registrationPromise } = await installationEntryPromise;
355370

356371
// Let the new getInstallationEntry process start.
357-
await sleep(50);
372+
await sleep(250);
358373

359374
const tokenDetails = (await get(
360375
appConfig
361376
)) as InProgressInstallationEntry;
362377
expect(tokenDetails.registrationTime).to.be.at.least(
363-
/* When the first pending request failed. */ 1_001_500
378+
/* When the first pending request failed. */ 1_004_000
364379
);
365380
expect(tokenDetails).to.deep.equal({
366381
fid: FID,
@@ -377,12 +392,16 @@ describe('getInstallationEntry', () => {
377392
it('updates the InstallationEntry if the request fails and the app is offline', async () => {
378393
stub(navigator, 'onLine').value(false);
379394

380-
clock.now = 1_001_000; // One second after the request was initiated.
395+
clock.restore();
396+
clock = useFakeTimers({
397+
now: 1_001_000 /* One second after the request was initiated. */,
398+
shouldAdvanceTime: true /* Needed to allow the createInstallation request to complete. */
399+
});
381400

382401
const installationEntryPromise = getInstallationEntry(appConfig);
383402

384403
// The pending request fails after a while.
385-
clock.tick(500);
404+
clock.tick(3000);
386405
await set(appConfig, {
387406
fid: FID,
388407
registrationStatus: RequestStatus.NOT_STARTED
@@ -391,7 +410,7 @@ describe('getInstallationEntry', () => {
391410
const { registrationPromise } = await installationEntryPromise;
392411

393412
// Let the new getInstallationEntry process start.
394-
await sleep(50);
413+
await sleep(250);
395414

396415
expect(await get(appConfig)).to.deep.equal({
397416
fid: FID,

0 commit comments

Comments
 (0)