UNPKG

@backstage/backend-test-utils

Version:

Test helpers library for Backstage backends

62 lines (58 loc) 1.88 kB
'use strict'; var TestBackend = require('./TestBackend.cjs.js'); var ServiceRegistry = require('../backend-app-api/src/wiring/ServiceRegistry.cjs.js'); class ServiceFactoryTester { #subject; #registry; /** * Creates a new {@link ServiceFactoryTester} used to test the provided subject. * * @param subject - The service factory to test. * @param options - Additional options * @returns A new tester instance for the provided subject. */ static from(subject, options) { const registry = ServiceRegistry.ServiceRegistry.create([ ...TestBackend.defaultServiceFactories, ...options?.dependencies ?? [], subject ]); return new ServiceFactoryTester(subject.service, registry); } constructor(subject, registry) { this.#subject = subject; this.#registry = registry; } /** * Returns the service instance for the subject. * * @remarks * * If the subject is a plugin scoped service factory a plugin ID * can be provided to instantiate the service for a specific plugin. * * By default the plugin ID 'test' is used. */ async getSubject(...args) { const [pluginId] = args; const instance = this.#registry.get(this.#subject, pluginId ?? "test"); return instance; } /** * Return the service instance for any of the provided dependencies or built-in services. * * @remarks * * A plugin ID can optionally be provided for plugin scoped services, otherwise the plugin ID 'test' is used. */ async getService(service, ...args) { const [pluginId] = args; const instance = await this.#registry.get(service, pluginId ?? "test"); if (instance === void 0) { throw new Error(`Service '${service.id}' not found`); } return instance; } } exports.ServiceFactoryTester = ServiceFactoryTester; //# sourceMappingURL=ServiceFactoryTester.cjs.js.map