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,