Skip to main content

@backstage/test-utils

Home > @backstage/test-utils

Utilities to test Backstage plugins and apps.

Classes

ClassDescription
MockAnalyticsApiMock implementation of AnalyticsApi with helpers to ensure that events are sent correctly. Use getEvents in tests to verify captured events.
MockConfigApiMockConfigApi is a thin wrapper around ConfigReader that can be used to mock configuration using a plain object.
MockErrorApiMock implementation of the ErrorApi to be used in tests. Includes withForError and getErrors methods for error testing.
MockFetchApiA test helper implementation of FetchApi.
MockPermissionApiMock implementation of PermissionApi. Supply a requestHandler function to override the mock result returned for a given request.
MockStorageApiMock implementation of the StorageApi to be used in tests
TestApiRegistryThe TestApiRegistry is an ApiHolder implementation that is particularly well suited for development and test environments such as unit tests, storybooks, and isolated plugin development setups.

Functions

FunctionDescription
createTestAppWrapper(options)Creates a Wrapper component that wraps a component inside a Backstage test app, providing a mocked theme and app context, along with mocked APIs.
mockBreakpoint(options)

This is a mocking method suggested in the Jest docs, as it is not implemented in JSDOM yet. It can be used to mock values for the Material UI useMediaQuery hook if it is used in a tested component.

For issues checkout the documentation: https://jestjs.io/docs/manual-mocks\#mocking-methods-which-are-not-implemented-in-jsdom

If there are any updates from Material UI React on testing useMediaQuery this mock should be replaced https://mui.com/material-ui/react-use-media-query/\#testing

renderInTestApp(Component, options)

Renders a component inside a Backstage test app, providing a mocked theme and app context, along with mocked APIs.

The render executes async effects similar to renderWithEffects. To avoid this behavior, use a regular render() + wrapInTestApp() instead.

renderWithEffects(nodes, options)Simplifies rendering of async components in by taking care of the wrapping inside act
setupRequestMockHandlers(worker)Sets up handlers for request mocking
withLogCollector(callback)Asynchronous log collector with that collects all categories
withLogCollector(callback)Synchronous log collector with that collects all categories
withLogCollector(logsToCollect, callback)Asynchronous log collector with that only collects selected categories
withLogCollector(logsToCollect, callback)Synchronous log collector with that only collects selected categories
wrapInTestApp(Component, options)Wraps a component inside a Backstage test app, providing a mocked theme and app context, along with mocked APIs.

Interfaces

InterfaceDescription
MockFetchApiOptionsThe options given when constructing a MockFetchApi.

Variables

VariableDescription
TestApiProvider

The TestApiProvider is a Utility API context provider that is particularly well suited for development and test environments such as unit tests, storybooks, and isolated plugin development setups.

It lets you provide any number of API implementations, without necessarily having to fully implement each of the APIs.

textContentMatcherReturns a @testing-library/react valid MatcherFunction for supplied text

Type Aliases

Type AliasDescription
AsyncLogCollectorAsyncLogCollector type used in withLogCollector() callback function.
CollectedLogsMap of severity level and corresponding log lines.
ErrorWithContextErrorWithContext contains error and ErrorApiErrorContext
LegacyRootOptionSet legacy mode when using React 18/RTL 13+. Mock this option while we're working against React 17 or lower.
LogCollectorUnion type used in withLogCollector() callback function.
LogFuncsSeverity levels of CollectedLogs
MockErrorApiOptionsConstructor arguments for MockErrorApi
MockStorageBucketType for map holding data in MockStorageApi
SyncLogCollectorSyncLogCollector type used in withLogCollector() callback function.
TestApiProviderPropsProperties for the TestApiProvider component.
TestAppOptionsOptions to customize the behavior of the test app wrapper.