diff --git a/plugins/catalog-backend/src/catalog/CatalogLogic.test.ts b/plugins/catalog-backend/src/catalog/CatalogLogic.test.ts index f8e455e1de..38f1088b98 100644 --- a/plugins/catalog-backend/src/catalog/CatalogLogic.test.ts +++ b/plugins/catalog-backend/src/catalog/CatalogLogic.test.ts @@ -28,7 +28,7 @@ describe('CatalogLogic', () => { it('works with no locations added', async () => { const catalog = ({ addOrUpdateComponent: jest.fn(), - locations: jest.fn(() => []), + locations: jest.fn().mockResolvedValue([]), } as unknown) as Catalog; const locationReader = jest.fn(); @@ -41,7 +41,7 @@ describe('CatalogLogic', () => { it('can update a single location', async () => { const catalog = ({ addOrUpdateComponent: jest.fn(), - locations: jest.fn(() => [ + locations: jest.fn().mockResolvedValue([ { id: '123', type: 'some', diff --git a/plugins/catalog-backend/src/catalog/CatalogLogic.ts b/plugins/catalog-backend/src/catalog/CatalogLogic.ts index d8c4804074..ed195386d0 100644 --- a/plugins/catalog-backend/src/catalog/CatalogLogic.ts +++ b/plugins/catalog-backend/src/catalog/CatalogLogic.ts @@ -24,19 +24,27 @@ export class CatalogLogic { reader: LocationReader, logger: Logger, ): () => void { + let cancel: () => void; let cancelled = false; + const cancellationPromise = new Promise((resolve) => { + cancel = () => { + resolve(); + cancelled = true; + }; + }); const startRefresh = async () => { while (!cancelled) { await CatalogLogic.refreshLocations(catalog, reader, logger); - await new Promise((resolve) => setTimeout(resolve, 10000)); + await Promise.race([ + new Promise((resolve) => setTimeout(resolve, 10000)), + cancellationPromise, + ]); } }; startRefresh(); - return () => { - cancelled = true; - }; + return cancel!; } public static async refreshLocations( diff --git a/plugins/catalog-backend/src/catalog/DatabaseCatalog.test.ts b/plugins/catalog-backend/src/catalog/DatabaseCatalog.test.ts index 80a0377445..7a474246d9 100644 --- a/plugins/catalog-backend/src/catalog/DatabaseCatalog.test.ts +++ b/plugins/catalog-backend/src/catalog/DatabaseCatalog.test.ts @@ -53,7 +53,7 @@ describe('DatabaseCatalog', () => { await catalog.removeLocation(locations[0].id); await expect(catalog.locations()).resolves.toEqual([]); - await expect(catalog.location(locations[0].id)).rejects.toThrowError( + await expect(catalog.location(locations[0].id)).rejects.toThrow( /Found no location/, ); });