add some test for invalid ids

Signed-off-by: Juan Pablo Garcia Ripa <sarabadu@gmail.com>
This commit is contained in:
Juan Pablo Garcia Ripa
2026-01-26 00:04:49 +01:00
parent bb9b471bd3
commit bd7d15dd7e
4 changed files with 44 additions and 49 deletions
@@ -28,8 +28,6 @@ import { BackendInitializer } from './BackendInitializer';
import { mockServices } from '@backstage/backend-test-utils';
import { BackendStartupError } from './BackendStartupError';
const ID_PATTERN = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/i;
const baseFactories = [
mockServices.rootLifecycle.factory(),
mockServices.lifecycle.factory(),
@@ -1013,53 +1011,6 @@ describe('BackendInitializer', () => {
"Service or extension point dependencies of module 'test-mod' for plugin 'test' are missing for the following ref(s): serviceRef{a}",
);
});
it('should reject plugins with invalid pluginId', async () => {
const init = new BackendInitializer(baseFactories);
init.add(
createBackendPlugin({
pluginId: 'test:invalid&id',
register(reg) {
reg.registerInit({
deps: {},
async init() {},
});
},
}),
);
await expect(init.start()).rejects.toThrow(
`Invalid pluginId 'test:invalid&id', must match the pattern ${ID_PATTERN} (letters, digits, and dashes only, starting with a letter)`,
);
});
it('should reject modules with invalid moduleId', async () => {
const init = new BackendInitializer(baseFactories);
init.add(
createBackendPlugin({
pluginId: 'test',
register(reg) {
reg.registerInit({
deps: {},
async init() {},
});
},
}),
);
init.add(
createBackendModule({
pluginId: 'test',
moduleId: 'invalid:module&id',
register(reg) {
reg.registerInit({
deps: {},
async init() {},
});
},
}),
);
await expect(init.start()).rejects.toThrow(
`Invalid moduleId 'invalid:module&id' for plugin 'test', must match the pattern ${ID_PATTERN} (letters, digits, and dashes only, starting with a letter)`,
);
});
it('should properly load double-default CJS modules', async () => {
expect.assertions(3);
@@ -15,6 +15,7 @@
*/
import { createServiceRef } from '../services';
import { ID_PATTERN } from './constants';
import { createBackendModule } from './createBackendModule';
import { createExtensionPoint } from './createExtensionPoint';
import { InternalBackendRegistrations } from './types';
@@ -77,4 +78,20 @@ describe('createBackendModule', () => {
expect(plugin.$$type).toEqual('@backstage/BackendFeature');
});
it('should reject modules with invalid moduleId', async () => {
expect(() =>
createBackendModule({
pluginId: 'test',
moduleId: 'invalid:module&id',
register(reg) {
reg.registerInit({
deps: {},
async init() {},
});
},
}),
).toThrow(
`Invalid moduleId 'invalid:module&id' for plugin 'test', must match the pattern ${ID_PATTERN} (letters, digits, and dashes only, starting with a letter)`,
);
});
});
@@ -15,6 +15,7 @@
*/
import { createServiceRef } from '../services';
import { ID_PATTERN } from './constants';
import { createBackendPlugin } from './createBackendPlugin';
import { createExtensionPoint } from './createExtensionPoint';
import { InternalBackendRegistrations } from './types';
@@ -90,4 +91,19 @@ describe('createBackendPlugin', () => {
expect(plugin.$$type).toEqual('@backstage/BackendFeature');
});
it('should reject plugins with invalid pluginId', async () => {
expect(() =>
createBackendPlugin({
pluginId: 'test:invalid&id',
register(reg) {
reg.registerInit({
deps: {},
async init() {},
});
},
}),
).toThrow(
`Invalid pluginId 'test:invalid&id', must match the pattern ${ID_PATTERN} (letters, digits, and dashes only, starting with a letter)`,
);
});
});
@@ -142,6 +142,17 @@ describe('createFrontendPlugin', () => {
expect(String(plugin)).toBe('Plugin{id=test}');
});
it('should warn about invalid plugin IDs', () => {
const consoleWarn = jest
.spyOn(console, 'warn')
.mockImplementation(() => {});
createFrontendPlugin({ pluginId: 'invalid&id' });
expect(consoleWarn).toHaveBeenCalledWith(
expect.stringContaining("The pluginId 'invalid&id' will be invalid soon"),
);
consoleWarn.mockRestore();
});
it('should create a plugin with extension instances', async () => {
const plugin = createFrontendPlugin({
pluginId: 'test',