Repository: spotify/backstage
File: plugins/gateway-backend/src/plugin.test.ts (TypeScript)

Code before:
      });
    },
  });

  const discovery = mockServices.discovery.mock();
  discovery.getBaseUrl.mockImplementation(async (pluginId: string) => {
    if (pluginId === 'external-plugin') {
      return 'http://localhost:7778/api/external-plugin';
    }
    return `http://localhost:7777/api/${pluginId}`;
  });

  beforeAll(async () => {
    backend = createBackend();
    backend.add(mockServices.rootHttpRouter.factory());
    backend.add(
      mockServices.rootConfig.factory({
        data: {
          backend: { baseUrl: 'http://localhost:7777', listen: { port: 7777 } },
        },
      }),
    );
    backend.add(mockServices.auth.factory());
    backend.add(mockServices.httpAuth.factory());
    backend.add(discovery.factory);
    backend.add(dummyPlugin);
    backend.add(import('./'));

    anotherBackend = createBackend();
    anotherBackend.add(mockServices.rootHttpRouter.factory());
    anotherBackend.add(
      mockServices.rootConfig.factory({
        data: {
          backend: { baseUrl: 'http://localhost:7778', listen: { port: 7778 } },
        },
      }),
    );
    anotherBackend.add(mockServices.auth.factory());
    anotherBackend.add(mockServices.httpAuth.factory());
    anotherBackend.add(mockServices.discovery.factory());
    anotherBackend.add(
      createBackendPlugin({
        pluginId: 'external-plugin',
        register(env) {
          env.registerInit({
            deps: {
              rootHttpRouter: coreServices.rootHttpRouter,
              httpRouter: coreServices.httpRouter,
            },
            async init({ httpRouter }) {
              const router = Router();

Review comment (suggestion):
Tempting to make `.factory` unnecessary here too, but at the same time we probably don't want to pollute the regular backend interface with that capability. If we make a dedicated test backend perhaps.

Code after:
      });
    },
  });

  const discovery = mockServices.discovery.mock({
    async getBaseUrl(pluginId) {
      if (pluginId === 'external-plugin') {
        return 'http://localhost:7778/api/external-plugin';
      }
      return `http://localhost:7777/api/${pluginId}`;
    },
  });

  beforeAll(async () => {
    backend = createBackend();
    backend.add(mockServices.rootHttpRouter.factory());
    backend.add(
      mockServices.rootConfig.factory({
        data: {
          backend: { baseUrl: 'http://localhost:7777', listen: { port: 7777 } },
        },
      }),
    );
    backend.add(mockServices.auth.factory());
    backend.add(mockServices.httpAuth.factory());
    backend.add(discovery.factory);
    backend.add(dummyPlugin);
    backend.add(import('./'));

    anotherBackend = createBackend();
    anotherBackend.add(mockServices.rootHttpRouter.factory());
    anotherBackend.add(
      mockServices.rootConfig.factory({
        data: {
          backend: { baseUrl: 'http://localhost:7778', listen: { port: 7778 } },
        },
      }),
    );
    anotherBackend.add(mockServices.auth.factory());
    anotherBackend.add(mockServices.httpAuth.factory());
    anotherBackend.add(mockServices.discovery.factory());
    anotherBackend.add(
      createBackendPlugin({
        pluginId: 'external-plugin',
        register(env) {
          env.registerInit({
            deps: {
              rootHttpRouter: coreServices.rootHttpRouter,
              httpRouter: coreServices.httpRouter,
            },
            async init({ httpRouter }) {