Refactor backend-test-utils
Co-authored-by: Fredrik Adelöw <freben@users.noreply.github.com> Co-authored-by: Patrik Oldsberg <poldsberg@gmail.com> Signed-off-by: Johan Haals <johan.haals@gmail.com>
This commit is contained in:
@@ -19,12 +19,12 @@ import {
|
||||
createServiceFactory,
|
||||
createServiceRef,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
import { createTestBackend, startTestBackend } from './TestBackend';
|
||||
import { startTestBackend } from './TestBackend';
|
||||
|
||||
describe('TestBackend', () => {
|
||||
it('should get a type error if service implementation does not match', () => {
|
||||
const serviceRef = createServiceRef<{ a: string; b: string }>({ id: 'a' });
|
||||
const backend = createTestBackend({
|
||||
const backend = startTestBackend({
|
||||
services: [
|
||||
[serviceRef, { a: 'a' }],
|
||||
[serviceRef, { a: 'a', b: 'b' }],
|
||||
@@ -68,7 +68,7 @@ describe('TestBackend', () => {
|
||||
|
||||
await startTestBackend({
|
||||
services: [sf],
|
||||
registrables: [testModule({})],
|
||||
features: [testModule({})],
|
||||
});
|
||||
|
||||
expect(testFn).toBeCalledWith('winning');
|
||||
|
||||
@@ -14,31 +14,54 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { Backend, createSpecializedBackend } from '@backstage/backend-app-api';
|
||||
import { createSpecializedBackend } from '@backstage/backend-app-api';
|
||||
import {
|
||||
AnyServiceFactory,
|
||||
ServiceRef,
|
||||
createServiceFactory,
|
||||
BackendFeature,
|
||||
ExtensionPoint,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
|
||||
/** @alpha */
|
||||
export interface TestBackendOptions<TServices extends any[]> {
|
||||
services: readonly [
|
||||
export interface TestBackendOptions<
|
||||
TServices extends any[],
|
||||
TExtensionPoints extends any[],
|
||||
> {
|
||||
services?: readonly [
|
||||
...{
|
||||
[index in keyof TServices]:
|
||||
| AnyServiceFactory
|
||||
| [ServiceRef<TServices[index]>, Partial<TServices[index]>];
|
||||
},
|
||||
];
|
||||
extensionPoints?: readonly [
|
||||
...{
|
||||
[index in keyof TExtensionPoints]: [
|
||||
ExtensionPoint<TExtensionPoints[index]>,
|
||||
Partial<TExtensionPoints[index]>,
|
||||
];
|
||||
},
|
||||
];
|
||||
features?: BackendFeature[];
|
||||
}
|
||||
|
||||
/** @alpha */
|
||||
export function createTestBackend<TServices extends any[]>(
|
||||
options: TestBackendOptions<TServices>,
|
||||
): Backend {
|
||||
const factories = options.services?.map(serviceDef => {
|
||||
export async function startTestBackend<
|
||||
TServices extends any[],
|
||||
TExtensionPoints extends any[],
|
||||
>(options: TestBackendOptions<TServices, TExtensionPoints>): Promise<void> {
|
||||
const {
|
||||
services = [],
|
||||
extensionPoints = [],
|
||||
features = [],
|
||||
...otherOptions
|
||||
} = options;
|
||||
|
||||
const factories = services.map(serviceDef => {
|
||||
if (Array.isArray(serviceDef)) {
|
||||
// if type is ExtensionPoint?
|
||||
// do something differently?
|
||||
return createServiceFactory({
|
||||
service: serviceDef[0],
|
||||
deps: {},
|
||||
@@ -47,19 +70,26 @@ export function createTestBackend<TServices extends any[]>(
|
||||
}
|
||||
return serviceDef as AnyServiceFactory;
|
||||
});
|
||||
return createSpecializedBackend({ services: factories ?? [] });
|
||||
}
|
||||
|
||||
/** @alpha */
|
||||
export async function startTestBackend<TServices extends any[]>(
|
||||
options: TestBackendOptions<TServices> & {
|
||||
features?: BackendFeature[];
|
||||
},
|
||||
): Promise<void> {
|
||||
const { features = [], ...otherOptions } = options;
|
||||
const backend = createTestBackend(otherOptions);
|
||||
const backend = createSpecializedBackend({
|
||||
...otherOptions,
|
||||
services: factories,
|
||||
});
|
||||
|
||||
backend.add({
|
||||
id: `---test-extension-point-registrar`,
|
||||
register(reg) {
|
||||
for (const [ref, impl] of extensionPoints) {
|
||||
reg.registerExtensionPoint(ref, impl);
|
||||
}
|
||||
|
||||
reg.registerInit({ deps: {}, async init() {} });
|
||||
},
|
||||
});
|
||||
|
||||
for (const feature of features) {
|
||||
backend.add(feature);
|
||||
}
|
||||
|
||||
await backend.start();
|
||||
}
|
||||
|
||||
@@ -14,5 +14,5 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export { createTestBackend, startTestBackend } from './TestBackend';
|
||||
export { startTestBackend } from './TestBackend';
|
||||
export type { TestBackendOptions } from './TestBackend';
|
||||
|
||||
Reference in New Issue
Block a user