Skip to main content
Version: Next

@backstage/test-utils

Home > @backstage/test-utils

Utilities to test Backstage plugins and apps.

Classes

Class

Description

MockAnalyticsApi

Mock implementation of AnalyticsApi with helpers to ensure that events are sent correctly. Use getEvents in tests to verify captured events.

MockConfigApi

MockConfigApi is a thin wrapper around ConfigReader that can be used to mock configuration using a plain object.

MockErrorApi

Mock implementation of the ErrorApi to be used in tests. Includes withForError and getErrors methods for error testing.

MockFetchApi

A test helper implementation of FetchApi.

MockPermissionApi

Mock implementation of PermissionApi. Supply a requestHandler function to override the mock result returned for a given request.

MockStorageApi

Mock implementation of the StorageApi to be used in tests

TestApiRegistry

The 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

Function

Description

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

registerMswTestHooks(worker)

Sets up handlers for request mocking

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)

TestApiProvider(props)

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.

textContentMatcher(text)

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

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

Interface

Description

MockFetchApiOptions

The options given when constructing a MockFetchApi.

Namespaces

Namespace

Description

mockApis

Mock implementations of the core utility APIs, to be used in tests.

Type Aliases

Type Alias

Description

ApiMock

Represents a mocked version of an API, where you automatically have access to the mocked versions of all of its methods along with a factory that returns that same mock.

AsyncLogCollector

AsyncLogCollector type used in withLogCollector() callback function.

CollectedLogs

Map of severity level and corresponding log lines.

ErrorWithContext

ErrorWithContext contains error and ErrorApiErrorContext

LegacyRootOption

Set legacy mode when using React 18/RTL 13+. Mock this option while we're working against React 17 or lower.

LogCollector

Union type used in withLogCollector() callback function.

LogFuncs

Severity levels of CollectedLogs

MockErrorApiOptions

Constructor arguments for MockErrorApi

MockStorageBucket

Type for map holding data in MockStorageApi

SyncLogCollector

SyncLogCollector type used in withLogCollector() callback function.

TestApiProviderProps

Properties for the TestApiProvider() component.

TestAppOptions

Options to customize the behavior of the test app wrapper.