For most test scenarios, prefer using the apis option in renderInTestApp or
createExtensionTester instead of wrapping components with TestApiProvider.
import { render } from '\@testing-library/react';
import { myCustomApiRef } from '../apis';
import { TestApiProvider, mockApis } from '\@backstage/frontend-test-utils';
// Mock custom APIs with tuple syntax
const myCustomApiMock = { myMethod: jest.fn() };
render(
<TestApiProvider
apis={[
[myCustomApiRef, myCustomApiMock]
]}
>
<MyComponent />
</TestApiProvider>
);
// Use with built-in mock APIs (no tuples needed)
render(
<TestApiProvider
apis={[
mockApis.identity({ userEntityRef: 'user:default/guest' }),
mockApis.alert(),
]}
>
<MyComponent />
</TestApiProvider>
);
The
TestApiProvideris a Utility API context provider for standalone rendering scenarios where you're not usingrenderInTestAppor other test utilities.It lets you provide any number of API implementations, without necessarily having to fully implement each of the APIs.