backstage-app-api: add test for missing transitive service dependencies

Co-authored-by: Fredrik Adelöw <freben@gmail.com>
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2022-09-02 11:56:10 +02:00
parent 06b37e817e
commit 6b730da35c
@@ -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<string>({ id: 'a' });
const refB = createServiceRef<string>({ id: 'b' });
const refC = createServiceRef<string>({ id: 'c' });
const refD = createServiceRef<string>({ 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,