diff --git a/src/packages/runtime/CustomElement.test.ts b/src/packages/runtime/CustomElement.test.ts index 09d4345c..6f794c26 100644 --- a/src/packages/runtime/CustomElement.test.ts +++ b/src/packages/runtime/CustomElement.test.ts @@ -26,6 +26,10 @@ interface InternalElementType extends ApplicationElement { $inspectElementState?(): any; } +afterEach(() => { + vi.restoreAllMocks(); +}); + describe("simple rendering", function () { const SIMPLE_STYLE = ".test { color: red }"; const SIMPLE_ELEM = createCustomElement({ @@ -346,6 +350,8 @@ describe("application lifecycle events", function () { }); it("does not signal 'before-stop' when start fails", async function () { + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => undefined); + const events: string[] = []; class Listener implements ApplicationLifecycleListener { afterApplicationStart() { @@ -391,6 +397,30 @@ describe("application lifecycle events", function () { }); expect(events).toEqual([]); + expect(errorSpy).toMatchInlineSnapshot(` + [MockFunction error] { + "calls": [ + [ + "#1", + [Error: help!], + ], + [ + "#2", + [Error: runtime:config-resolution-failed: Failed to resolve application properties.], + ], + ], + "results": [ + { + "type": "return", + "value": undefined, + }, + { + "type": "return", + "value": undefined, + }, + ], + } + `); }); }); diff --git a/src/packages/runtime/react-integration/ReactIntegration.test.ts b/src/packages/runtime/react-integration/ReactIntegration.test.ts index 00dec226..dbc81ce7 100644 --- a/src/packages/runtime/react-integration/ReactIntegration.test.ts +++ b/src/packages/runtime/react-integration/ReactIntegration.test.ts @@ -3,21 +3,22 @@ /** * @vitest-environment happy-dom */ -import { createElement } from "react"; -import { beforeEach, expect, it } from "vitest"; -import { usePropertiesInternal, useServiceInternal, useServicesInternal } from "./hooks"; import { findByTestId, findByText } from "@testing-library/dom"; import { act } from "@testing-library/react"; +import { createElement } from "react"; +import { beforeEach, expect, it, SpyInstance, afterEach, vi } from "vitest"; import { Service, ServiceConstructor } from "../Service"; -// eslint-disable-next-line import/no-relative-packages -import { UIWithProperties, UIWithService, UIWithServices } from "./test-data/test-package/UI"; +import { usePropertiesInternal, useServiceInternal, useServicesInternal } from "./hooks"; +import { useTheme } from "@open-pioneer/chakra-integration"; +import { PackageIntl, createEmptyI18n } from "../i18n"; +import { InterfaceSpec, ReferenceSpec } from "../service-layer/InterfaceSpec"; +import { PackageRepr } from "../service-layer/PackageRepr"; import { ServiceLayer } from "../service-layer/ServiceLayer"; +import { ServiceRepr, createConstructorFactory } from "../service-layer/ServiceRepr"; import { ReactIntegration } from "./ReactIntegration"; -import { PackageRepr } from "../service-layer/PackageRepr"; -import { createConstructorFactory, ServiceRepr } from "../service-layer/ServiceRepr"; -import { InterfaceSpec, ReferenceSpec } from "../service-layer/InterfaceSpec"; -import { createEmptyI18n, PackageIntl } from "../i18n"; -import { useTheme } from "@open-pioneer/chakra-integration"; + +// eslint-disable-next-line import/no-relative-packages +import { UIWithProperties, UIWithService, UIWithServices } from "./test-data/test-package/UI"; interface TestProvider { value: string; @@ -27,6 +28,15 @@ beforeEach(() => { document.body.innerHTML = ""; }); +let errorSpy!: SpyInstance; +beforeEach(() => { + errorSpy = vi.spyOn(console, "error"); +}); + +afterEach(() => { + vi.restoreAllMocks(); +}); + it("should allow access to service via react hook", async () => { function TestComponent() { const service = useServiceInternal("test", "test.Provider") as TestProvider; @@ -55,6 +65,8 @@ it("should allow access to service via react hook", async () => { }); it("should get error when using undefined service", async () => { + errorSpy.mockImplementation(doNothing); + function TestComponent() { const service = useServiceInternal("test", "test.Provider") as TestProvider; return createElement("span", undefined, `Hello ${service.value}`); @@ -69,6 +81,7 @@ it("should get error when using undefined service", async () => { integration.render(TestComponent); }); }).toThrowErrorMatchingSnapshot(); + expect(errorSpy).toHaveBeenCalledOnce(); }); it("should allow access to service with qualifier via react hook", async () => { @@ -101,6 +114,8 @@ it("should allow access to service with qualifier via react hook", async () => { }); it("should deny access to service when the qualifier does not match", async () => { + errorSpy.mockImplementation(doNothing); + function TestComponent() { const service = useServiceInternal("test", "test.Provider", { qualifier: "bar" @@ -126,6 +141,7 @@ it("should deny access to service when the qualifier does not match", async () = integration.render(TestComponent); }); }).toThrowErrorMatchingSnapshot(); + expect(errorSpy).toHaveBeenCalledOnce(); }); it("should allow access to all services via react hook", async () => { @@ -174,6 +190,8 @@ it("should allow access to all services via react hook", async () => { }); it("should deny access to all services if declaration is missing", async () => { + errorSpy.mockImplementation(doNothing); + function TestComponent() { const services = useServicesInternal("test", "test.Provider") as TestProvider[]; return createElement( @@ -192,6 +210,7 @@ it("should deny access to all services if declaration is missing", async () => { integration.render(TestComponent); }); }).toThrowErrorMatchingSnapshot(); + expect(errorSpy).toHaveBeenCalledOnce(); }); it("should be able to read properties from react component", async () => { @@ -287,6 +306,8 @@ it("should provide the autogenerated useProperties hook", async () => { }); it("should throw error when requesting properties from an unknown package", async () => { + errorSpy.mockImplementation(doNothing); + const { integration } = createIntegration({ disablePackage: true }); @@ -301,6 +322,7 @@ it("should throw error when requesting properties from an unknown package", asyn integration.render(TestComponent); }); }).toThrowErrorMatchingSnapshot(); + expect(errorSpy).toHaveBeenCalledOnce(); }); it("should apply the configured chakra theme", async () => { @@ -393,3 +415,5 @@ function createIntegration(options?: { }); return { integration, wrapper }; } + +function doNothing() {}