backend-defaults: tests for shared env in createBackend

Co-authored-by: blam <ben@blam.sh>
Co-authored-by: Johan Haals <johan.haals@gmail.com>
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-01-13 13:51:46 +01:00
parent a1449b94bf
commit efb97e284a
4 changed files with 99 additions and 1 deletions
+1
View File
@@ -36,6 +36,7 @@
"@backstage/backend-plugin-api": "workspace:^"
},
"devDependencies": {
"@backstage/backend-test-utils": "workspace:^",
"@backstage/cli": "workspace:^"
},
"files": [
@@ -16,10 +16,17 @@
import {
coreServices,
createBackendPlugin,
createServiceFactory,
createServiceRef,
createSharedEnvironment,
} from '@backstage/backend-plugin-api';
import { mockConfigFactory } from '@backstage/backend-test-utils';
import { createBackend } from './CreateBackend';
const fooServiceRef = createServiceRef<string>({ id: 'foo', scope: 'root' });
const barServiceRef = createServiceRef<string>({ id: 'bar', scope: 'root' });
describe('createBackend', () => {
it('should not throw when overriding a default service implementation', () => {
expect(() =>
@@ -69,4 +76,91 @@ describe('createBackend', () => {
}),
).toThrow('The core.pluginMetadata service cannot be overridden');
});
it('should throw if an unsupported InternalSharedEnvironment version is passed in', () => {
expect(() =>
createBackend({
env: {} as any,
}),
).toThrow(
"Shared environment version 'undefined' is invalid or not supported",
);
expect(() =>
createBackend({
env: { version: {} } as any,
}),
).toThrow(
"Shared environment version '[object Object]' is invalid or not supported",
);
expect(() =>
createBackend({
env: { version: 'v2' } as any,
}),
).toThrow("Shared environment version 'v2' is invalid or not supported");
});
it('should prioritize services correctly', async () => {
const backend = createBackend({
env: createSharedEnvironment({
services: [
createServiceFactory({
service: coreServices.rootHttpRouter,
deps: {},
async factory() {
return {
use() {},
};
},
}),
mockConfigFactory({
data: { root: 'root-env' },
}),
createServiceFactory({
service: fooServiceRef,
deps: {},
async factory() {
return 'foo-env';
},
}),
createServiceFactory({
service: barServiceRef,
deps: {},
async factory() {
return 'bar-env';
},
}),
],
})(),
services: [
createServiceFactory({
service: fooServiceRef,
deps: {},
factory: async () => 'foo-backend',
}),
],
});
expect.assertions(3);
backend.add(
createBackendPlugin({
id: 'test',
register(reg) {
reg.registerInit({
deps: {
config: coreServices.config,
foo: fooServiceRef,
bar: barServiceRef,
},
async init({ config, foo, bar }) {
expect(config.get('root')).toBe('root-env');
expect(foo).toBe('foo-backend');
expect(bar).toBe('bar-env');
},
});
},
})(),
);
await backend.start();
});
});
@@ -85,7 +85,9 @@ export function createBackend(options?: CreateBackendOptions): Backend {
if (options?.env) {
const env = options.env as unknown as InternalSharedBackendEnvironment;
if (env.version !== 'v1') {
throw new Error(`Unsupported shared environment version: ${env.version}`);
throw new Error(
`Shared environment version '${env.version}' is invalid or not supported`,
);
}
const environmentServices =
+1
View File
@@ -3515,6 +3515,7 @@ __metadata:
dependencies:
"@backstage/backend-app-api": "workspace:^"
"@backstage/backend-plugin-api": "workspace:^"
"@backstage/backend-test-utils": "workspace:^"
"@backstage/cli": "workspace:^"
languageName: unknown
linkType: soft