Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: Fix flaky test #25644

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Subject, lastValueFrom } from 'rxjs';
import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest';
import assert from 'node:assert';

import { queueBootstrapping } from './BootstrapQueue';

const instantWaitFor = (fn: () => void) => {
return vi.waitFor(fn, {
interval: 0,
});
};

describe('BootstrapQueue', () => {
beforeEach(async () => {
vi.spyOn(console, 'log').mockImplementation(() => {});
Expand All @@ -24,8 +29,10 @@ describe('BootstrapQueue', () => {
bootstrapAppFinished();
});

await vi.waitFor(() => {
assert(bootstrapApp.mock.calls.length === 1, 'bootstrapApp should have been called once');
await instantWaitFor(() => {
if (bootstrapApp.mock.calls.length !== 1) {
throw new Error('bootstrapApp should not have been called yet');
}
});

expect(bootstrapApp).toHaveBeenCalled();
Expand All @@ -34,11 +41,10 @@ describe('BootstrapQueue', () => {
pendingSubject.next();
pendingSubject.complete();

await vi.waitFor(() => {
assert(
bootstrapAppFinished.mock.calls.length === 1,
'bootstrapApp should have been called once'
);
await instantWaitFor(() => {
if (bootstrapAppFinished.mock.calls.length !== 1) {
throw new Error('bootstrapApp should have been called once');
}
});

expect(bootstrapAppFinished).toHaveBeenCalled();
Expand Down Expand Up @@ -67,8 +73,10 @@ describe('BootstrapQueue', () => {
queueBootstrapping(bootstrapApp2).then(bootstrapAppFinished2);
queueBootstrapping(bootstrapApp3).then(bootstrapAppFinished3);

await vi.waitFor(() => {
assert(bootstrapApp.mock.calls.length === 1, 'bootstrapApp should have been called once');
await instantWaitFor(() => {
if (bootstrapApp.mock.calls.length !== 1) {
throw new Error('bootstrapApp should have been called once');
}
});

expect(bootstrapApp).toHaveBeenCalled();
Expand All @@ -81,8 +89,10 @@ describe('BootstrapQueue', () => {
pendingSubject.next();
pendingSubject.complete();

await vi.waitFor(() => {
assert(bootstrapApp2.mock.calls.length === 1, 'bootstrapApp2 should have been called once');
await instantWaitFor(() => {
if (bootstrapApp2.mock.calls.length !== 1) {
throw new Error('bootstrapApp2 should have been called once');
}
});

expect(bootstrapApp).toHaveReturnedTimes(1);
Expand All @@ -95,8 +105,10 @@ describe('BootstrapQueue', () => {
pendingSubject2.next();
pendingSubject2.complete();

await vi.waitFor(() => {
assert(bootstrapApp3.mock.calls.length === 1, 'bootstrapApp3 should have been called once');
await instantWaitFor(() => {
if (bootstrapApp3.mock.calls.length !== 1) {
throw new Error('bootstrapApp3 should have been called once');
}
});

expect(bootstrapApp).toHaveReturnedTimes(1);
Expand All @@ -109,11 +121,10 @@ describe('BootstrapQueue', () => {
pendingSubject3.next();
pendingSubject3.complete();

await vi.waitFor(() => {
assert(
bootstrapAppFinished3.mock.calls.length === 1,
'bootstrapAppFinished3 should have been called once'
);
await instantWaitFor(() => {
if (bootstrapAppFinished3.mock.calls.length !== 1) {
throw new Error('bootstrapAppFinished3 should have been called once');
}
});

expect(bootstrapApp).toHaveReturnedTimes(1);
Expand Down Expand Up @@ -142,8 +153,10 @@ describe('BootstrapQueue', () => {
queueBootstrapping(bootstrapApp).then(bootstrapAppFinished).catch(bootstrapAppError);
queueBootstrapping(bootstrapApp2).then(bootstrapAppFinished2).catch(bootstrapAppError2);

await vi.waitFor(() => {
assert(bootstrapApp.mock.calls.length === 1, 'bootstrapApp should have been called once');
await instantWaitFor(() => {
if (bootstrapApp.mock.calls.length !== 1) {
throw new Error('bootstrapApp should have been called once');
}
});

expect(bootstrapApp).toHaveBeenCalledTimes(1);
Expand All @@ -152,11 +165,10 @@ describe('BootstrapQueue', () => {

pendingSubject.error(new Error('test error'));

await vi.waitFor(() => {
assert(
bootstrapAppError.mock.calls.length === 1,
'bootstrapAppError should have been called once'
);
await instantWaitFor(() => {
if (bootstrapAppError.mock.calls.length !== 1) {
throw new Error('bootstrapAppError should have been called once');
}
});

expect(bootstrapApp).toHaveBeenCalledTimes(1);
Expand All @@ -169,11 +181,10 @@ describe('BootstrapQueue', () => {
pendingSubject2.next();
pendingSubject2.complete();

await vi.waitFor(() => {
assert(
bootstrapAppFinished2.mock.calls.length === 1,
'bootstrapAppFinished2 should have been called once'
);
await instantWaitFor(() => {
if (bootstrapAppFinished2.mock.calls.length !== 1) {
throw new Error('bootstrapAppFinished2 should have been called once');
}
});

expect(bootstrapApp).toHaveBeenCalledTimes(1);
Expand Down