From 73399c1b2441ef5063224b90392a40fb3b537cb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Sat, 25 Jul 2026 23:52:11 +0200 Subject: [PATCH] Fix tests --- packages/vite-plugin/src/plugin.test.ts | 10 ++++++++-- packages/vite-plugin/src/runtime.test.ts | 14 ++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/vite-plugin/src/plugin.test.ts b/packages/vite-plugin/src/plugin.test.ts index f78486c..822391f 100644 --- a/packages/vite-plugin/src/plugin.test.ts +++ b/packages/vite-plugin/src/plugin.test.ts @@ -1,9 +1,15 @@ import path from "node:path" -import type { Plugin, TransformPluginContext } from "vite" +import type { Plugin } from "vite" import { describe, expect, it } from "vitest" import { effectViewPlugin } from "./index.js" +type TransformHook = Extract< + NonNullable, + (...args: never[]) => unknown +> +type TransformPluginContext = ThisParameterType + const runTransform = async ( plugin: Plugin, code: string, @@ -19,7 +25,7 @@ const runTransform = async ( if (!result) return undefined - return typeof result === "string" ? result : result.code + return typeof result === "string" ? result : result.code?.toString() } const transform = ( diff --git a/packages/vite-plugin/src/runtime.test.ts b/packages/vite-plugin/src/runtime.test.ts index 65be4a7..da5745b 100644 --- a/packages/vite-plugin/src/runtime.test.ts +++ b/packages/vite-plugin/src/runtime.test.ts @@ -8,6 +8,12 @@ describe("refresh runtime", () => { const component = (value: A): A & Component.Component.Any => value as A & Component.Component.Any + const refreshCell = (value: Component.Component.Any): Refreshable.Cell => { + if (!Refreshable.isRefreshable(value)) + throw new Error("Expected a refreshable component") + return value[Refreshable.RefreshableTypeId] + } + it("retains a cell and notifies subscribers for compatible updates", async () => { const hot = { data: {}, @@ -15,12 +21,12 @@ describe("refresh runtime", () => { invalidate: vi.fn(), } const first = register(component({ body: "first" }), hot, "module:View", "hooks") - const cell = (first as Record)[Refreshable.RefreshableTypeId] as Refreshable.Cell + const cell = refreshCell(first) const listener = vi.fn() cell.subscribe(listener) const second = register(component({ body: "second" }), hot, "module:View", "hooks") - const secondCell = (second as Record)[Refreshable.RefreshableTypeId] + const secondCell = refreshCell(second) expect(secondCell).toBe(cell) expect(cell.current).toBe(second) @@ -40,7 +46,7 @@ describe("refresh runtime", () => { invalidate: vi.fn(), } const first = register(component({}), hot, "module:View", "one") - const cell = (first as Record)[Refreshable.RefreshableTypeId] as Refreshable.Cell + const cell = refreshCell(first) register(component({}), hot, "module:View", "two") expect(cell.snapshot.resetRevision).toBe(1) @@ -52,7 +58,7 @@ describe("refresh runtime", () => { it("is inert outside a Vite hot context", () => { const descriptor = component({}) expect(register(descriptor, undefined, "module:View", "hooks")).toBe(descriptor) - expect((descriptor as Record)[Refreshable.RefreshableTypeId]).toBeUndefined() + expect(Refreshable.isRefreshable(descriptor)).toBe(false) }) it("invalidates when the module's View IDs change", () => {