|
| 1 | +import path from "node:path"; |
| 2 | +import { fileURLToPath } from "node:url"; |
| 3 | +import { describe, expect, it } from "vitest"; |
| 4 | +import { reactSourceLocPlugin } from "../react-source-loc-vite-plugin"; |
| 5 | + |
| 6 | +const testDir = path.dirname(fileURLToPath(import.meta.url)); |
| 7 | +const clientRoot = path.join(testDir, "client"); |
| 8 | +const projectRoot = testDir; |
| 9 | +const moduleId = path.join(clientRoot, "src", "Example.tsx"); |
| 10 | + |
| 11 | +interface TestableHooks { |
| 12 | + transform?: ( |
| 13 | + code: string, |
| 14 | + id: string, |
| 15 | + ) => |
| 16 | + | { code: string } |
| 17 | + | string |
| 18 | + | null |
| 19 | + | undefined |
| 20 | + | Promise<{ code: string } | string | null | undefined>; |
| 21 | +} |
| 22 | + |
| 23 | +async function transformSource( |
| 24 | + code: string, |
| 25 | + root: string = clientRoot, |
| 26 | + id: string = moduleId, |
| 27 | + rootForPaths: string = projectRoot, |
| 28 | +): Promise<string> { |
| 29 | + const { transform } = reactSourceLocPlugin({ |
| 30 | + projectRoot: rootForPaths, |
| 31 | + }) as unknown as TestableHooks; |
| 32 | + |
| 33 | + const result = await transform?.(code, id); |
| 34 | + if (!result) return code; |
| 35 | + return typeof result === "string" ? result : result.code; |
| 36 | +} |
| 37 | + |
| 38 | +describe("reactSourceLocPlugin", () => { |
| 39 | + it("injects data-source on native opening and self-closing tags", async () => { |
| 40 | + const code = `export function App() { |
| 41 | + return ( |
| 42 | + <motion.div> |
| 43 | + <div className="a"> |
| 44 | + <span /> |
| 45 | + </div> |
| 46 | + </motion.div> |
| 47 | + ); |
| 48 | +} |
| 49 | +`; |
| 50 | + const output = await transformSource(code); |
| 51 | + expect(output).toContain('data-source="client/src/Example.tsx:'); |
| 52 | + expect(output).toMatch(/<motion\.div>/); |
| 53 | + expect(output).toMatch(/<div data-source="[^"]+" className="a">/); |
| 54 | + expect(output).toMatch(/<span data-source="[^"]+" \/>/); |
| 55 | + expect(output).not.toContain("motion.div data-source"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("skips components, fragments, namespaced tags, and existing data-source", async () => { |
| 59 | + const code = `export function App() { |
| 60 | + return ( |
| 61 | + <> |
| 62 | + <Foo /> |
| 63 | + <Foo.Bar /> |
| 64 | + <svg:circle /> |
| 65 | + <motion.div data-source="manual" /> |
| 66 | + </> |
| 67 | + ); |
| 68 | +} |
| 69 | +`; |
| 70 | + const output = await transformSource(code); |
| 71 | + expect(output).not.toMatch(/<Foo data-source=/); |
| 72 | + expect(output).not.toMatch(/<Foo\.Bar data-source=/); |
| 73 | + expect(output).not.toMatch(/<svg:circle data-source=/); |
| 74 | + expect(output).toContain('<motion.div data-source="manual"'); |
| 75 | + expect(output).not.toMatch(/data-source="[^"]+" data-source=/); |
| 76 | + }); |
| 77 | + |
| 78 | + it("resolves paths from app root when vite root is the app root", async () => { |
| 79 | + const appRoot = path.join(testDir, "flat-app"); |
| 80 | + const flatModuleId = path.join(appRoot, "src", "Page.tsx"); |
| 81 | + const code = `export const Page = () => <div className="x" />;`; |
| 82 | + |
| 83 | + const output = await transformSource(code, appRoot, flatModuleId, appRoot); |
| 84 | + |
| 85 | + expect(output).toMatch(/<div data-source="src\/Page\.tsx:/); |
| 86 | + expect(output).not.toMatch(/data-source="[^"]*\.\./); |
| 87 | + }); |
| 88 | +}); |
0 commit comments