From 1911ebac7f4a8c8ea63ffc26b59f6939404b9cda Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 3 Feb 2026 00:21:07 +0100 Subject: [PATCH] frontend-test-utils: also add apis option to renderTestApp Signed-off-by: Patrik Oldsberg --- .changeset/api-override-test-utils.md | 13 ++++++- packages/frontend-test-utils/report.api.md | 7 ++-- .../src/app/renderTestApp.tsx | 34 +++++++++++++++++-- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/.changeset/api-override-test-utils.md b/.changeset/api-override-test-utils.md index f2c73db7f3..6ea60f9e1b 100644 --- a/.changeset/api-override-test-utils.md +++ b/.changeset/api-override-test-utils.md @@ -2,7 +2,7 @@ '@backstage/frontend-test-utils': patch --- -Added an `apis` option to `createExtensionTester` and `renderInTestApp` to override APIs when testing extensions. Use the `mockApis` helpers to create mock implementations: +Added an `apis` option to `createExtensionTester`, `renderInTestApp`, and `renderTestApp` to override APIs when testing extensions. Use the `mockApis` helpers to create mock implementations: ```typescript import { identityApiRef } from '@backstage/frontend-plugin-api'; @@ -27,4 +27,15 @@ renderInTestApp(, { ], ], }); + +// Override APIs in renderTestApp +renderTestApp({ + extensions: [myExtension], + apis: [ + [ + identityApiRef, + mockApis.identity({ userEntityRef: 'user:default/guest' }), + ], + ], +}); ``` diff --git a/packages/frontend-test-utils/report.api.md b/packages/frontend-test-utils/report.api.md index b1cee9ba1a..fa57b76e82 100644 --- a/packages/frontend-test-utils/report.api.md +++ b/packages/frontend-test-utils/report.api.md @@ -126,16 +126,17 @@ export function renderInTestApp( ): RenderResult; // @public -export function renderTestApp( - options: RenderTestAppOptions, +export function renderTestApp( + options: RenderTestAppOptions, ): RenderResult; // @public -export type RenderTestAppOptions = { +export type RenderTestAppOptions = { config?: JsonObject; extensions?: ExtensionDefinition[]; features?: FrontendFeature[]; initialRouteEntries?: string[]; + apis?: readonly [...TestApiPairs]; }; // @public diff --git a/packages/frontend-test-utils/src/app/renderTestApp.tsx b/packages/frontend-test-utils/src/app/renderTestApp.tsx index 879e3fe1ae..b12d3ffd8c 100644 --- a/packages/frontend-test-utils/src/app/renderTestApp.tsx +++ b/packages/frontend-test-utils/src/app/renderTestApp.tsx @@ -17,6 +17,7 @@ import { createSpecializedApp } from '@backstage/frontend-app-api'; import { coreExtensionData, + createApiFactory, createFrontendModule, createFrontendPlugin, ExtensionDefinition, @@ -28,6 +29,9 @@ import { JsonObject } from '@backstage/types'; import { ConfigReader } from '@backstage/config'; import { MemoryRouter } from 'react-router-dom'; import { RouterBlueprint } from '@backstage/plugin-app-react'; +import { type TestApiPairs } from '../utils'; +// eslint-disable-next-line @backstage/no-relative-monorepo-imports +import type { CreateSpecializedAppInternalOptions } from '../../../frontend-app-api/src/wiring/createSpecializedApp'; const DEFAULT_MOCK_CONFIG = { app: { baseUrl: 'http://localhost:3000' }, @@ -39,7 +43,7 @@ const DEFAULT_MOCK_CONFIG = { * * @public */ -export type RenderTestAppOptions = { +export type RenderTestAppOptions = { /** * Additional configuration passed to the app when rendering elements inside it. */ @@ -58,6 +62,23 @@ export type RenderTestAppOptions = { * Initial route entries to use for the router. */ initialRouteEntries?: string[]; + + /** + * API overrides to provide to the test app. Use `mockApis` helpers + * from `@backstage/frontend-test-utils` to create mock implementations. + * + * @example + * ```ts + * import { identityApiRef } from '@backstage/frontend-plugin-api'; + * import { mockApis } from '@backstage/frontend-test-utils'; + * + * renderTestApp({ + * apis: [[identityApiRef, mockApis.identity({ userEntityRef: 'user:default/guest' })]], + * extensions: [...], + * }) + * ``` + */ + apis?: readonly [...TestApiPairs]; }; const appPluginOverride = appPlugin.withOverrides({ @@ -74,7 +95,9 @@ const appPluginOverride = appPlugin.withOverrides({ * * @public */ -export function renderTestApp(options: RenderTestAppOptions) { +export function renderTestApp( + options: RenderTestAppOptions, +) { const extensions = [...(options.extensions ?? [])]; const features: FrontendFeature[] = [ @@ -111,7 +134,12 @@ export function renderTestApp(options: RenderTestAppOptions) { data: options?.config ?? DEFAULT_MOCK_CONFIG, }, ]), - }); + __internal: options?.apis && { + apiFactoryOverrides: options.apis.map(([apiRef, implementation]) => + createApiFactory(apiRef, implementation), + ), + }, + } as CreateSpecializedAppInternalOptions); return render( app.tree.root.instance!.getData(coreExtensionData.reactElement),