diff --git a/packages/test-utils/src/testUtils/appWrappers.test.tsx b/packages/test-utils/src/testUtils/appWrappers.test.tsx
index a30e8f9823..34a20be63b 100644
--- a/packages/test-utils/src/testUtils/appWrappers.test.tsx
+++ b/packages/test-utils/src/testUtils/appWrappers.test.tsx
@@ -14,11 +14,18 @@
* limitations under the License.
*/
-import React, { FC } from 'react';
+import React, { FC, useEffect } from 'react';
import { render } from '@testing-library/react';
import { wrapInTestApp, renderInTestApp } from './appWrappers';
import { Route } from 'react-router';
import { withLogCollector } from '@backstage/test-utils-core';
+import {
+ useApi,
+ errorApiRef,
+ ApiProvider,
+ ApiRegistry,
+} from '@backstage/core-api';
+import { MockErrorApi } from './apis';
describe('wrapInTestApp', () => {
it('should provide routing and warn about missing act()', async () => {
@@ -69,4 +76,44 @@ describe('wrapInTestApp', () => {
const rendered = await renderInTestApp();
expect(rendered.getByText('foo')).toBeInTheDocument();
});
+
+ it('should provide mock API implementations', async () => {
+ const A: FC<{}> = () => {
+ const errorApi = useApi(errorApiRef);
+ errorApi.post(new Error('NOPE'));
+ return null;
+ };
+
+ const { error } = await withLogCollector(['error'], async () => {
+ await expect(renderInTestApp(A)).rejects.toThrow('NOPE');
+ });
+
+ expect(error).toEqual([
+ expect.stringMatching(
+ /^Error: Uncaught \[Error: MockErrorApi received unexpected error, Error: NOPE\]/,
+ ),
+ expect.stringMatching(/^The above error occurred in the component:/),
+ ]);
+ });
+
+ it('should allow custom API implementations', async () => {
+ const mockErrorApi = new MockErrorApi({ collect: true });
+
+ const A: FC<{}> = () => {
+ const errorApi = useApi(errorApiRef);
+ useEffect(() => {
+ errorApi.post(new Error('NOPE'));
+ }, [errorApi]);
+ return foo
;
+ };
+
+ const rendered = await renderInTestApp(
+
+
+ ,
+ );
+
+ expect(rendered.getByText('foo')).toBeInTheDocument();
+ expect(mockErrorApi.getErrors()).toEqual([{ error: new Error('NOPE') }]);
+ });
});
diff --git a/packages/test-utils/src/testUtils/appWrappers.tsx b/packages/test-utils/src/testUtils/appWrappers.tsx
index 6d48082b7c..89e68c143a 100644
--- a/packages/test-utils/src/testUtils/appWrappers.tsx
+++ b/packages/test-utils/src/testUtils/appWrappers.tsx
@@ -20,11 +20,11 @@ import { Route } from 'react-router-dom';
import { lightTheme } from '@backstage/theme';
import privateExports, {
defaultSystemIcons,
- ApiTestRegistry,
BootErrorPageProps,
} from '@backstage/core-api';
import { RenderResult } from '@testing-library/react';
import { renderWithEffects } from '@backstage/test-utils-core';
+import { createMockApiRegistry } from './mockApiRegistry';
const { PrivateAppImpl } = privateExports;
const NotFoundErrorPage = () => {
@@ -57,9 +57,10 @@ export function wrapInTestApp(
options: TestAppOptions = {},
): ReactElement {
const { routeEntries = ['/'] } = options;
+ const apis = createMockApiRegistry();
const app = new PrivateAppImpl({
- apis: new ApiTestRegistry(),
+ apis,
components: {
NotFoundErrorPage,
BootErrorPage,
diff --git a/packages/test-utils/src/testUtils/mockApiRegistry.ts b/packages/test-utils/src/testUtils/mockApiRegistry.ts
new file mode 100644
index 0000000000..96d12a9df2
--- /dev/null
+++ b/packages/test-utils/src/testUtils/mockApiRegistry.ts
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2020 Spotify AB
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { ApiTestRegistry } from '@backstage/core-api';
+import { MockErrorApi } from './apis';
+
+export function createMockApiRegistry(): ApiTestRegistry {
+ const registry = new ApiTestRegistry();
+
+ registry.register(MockErrorApi.factory);
+
+ return registry;
+}