From 6b730da35cf51a5c97b1059af47161748b6e5736 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 2 Sep 2022 11:56:10 +0200 Subject: [PATCH] backstage-app-api: add test for missing transitive service dependencies MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Fredrik Adelöw Signed-off-by: Patrik Oldsberg --- .../src/wiring/ServiceRegistry.test.ts | 37 ++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/packages/backend-app-api/src/wiring/ServiceRegistry.test.ts b/packages/backend-app-api/src/wiring/ServiceRegistry.test.ts index 2c17606a16..a00f6c0b4b 100644 --- a/packages/backend-app-api/src/wiring/ServiceRegistry.test.ts +++ b/packages/backend-app-api/src/wiring/ServiceRegistry.test.ts @@ -225,11 +225,8 @@ describe('ServiceRegistry', () => { const myFactory = createServiceFactory({ service: ref1, deps: { dep: ref2 }, - async factory({ dep }) { - return async pluginId => { - const d = await dep(pluginId); - return { x: d.x, pluginId }; - }; + async factory() { + throw new Error('ignored'); }, }); @@ -241,6 +238,36 @@ describe('ServiceRegistry', () => { ); }); + it('should throw if dependencies are not available 2', async () => { + const refA = createServiceRef({ id: 'a' }); + const refB = createServiceRef({ id: 'b' }); + const refC = createServiceRef({ id: 'c' }); + const refD = createServiceRef({ id: 'd' }); + + const factoryA = createServiceFactory({ + service: refA, + deps: { b: refB }, + async factory({ b }) { + return async pluginId => b(pluginId); + }, + }); + + const factoryB = createServiceFactory({ + service: refB, + deps: { c: refC, d: refD }, + async factory() { + throw new Error('ignored'); + }, + }); + + const registry = new ServiceRegistry([factoryA, factoryB]); + const factory = registry.get(refA)!; + + await expect(factory('catalog')).rejects.toThrow( + "Failed to instantiate service 'a' for 'catalog' because the factory function threw an error, Error: Failed to instantiate service 'b' for 'catalog' because the following dependent services are missing: 'c', 'd'", + ); + }); + it('should decorate error messages thrown by the top-level factory function', async () => { const myFactory = createServiceFactory({ service: ref1,