// Snapshot tests for @twinkleplop/twoslash-svelte. // // Three fixtures pinned with vitest snapshots (hover / query / error) // plus balance and structural checks. The highlighter is built once in // beforeAll so the underlying twoslash TypeScript environment is reused // across tests — otherwise each test spins up a fresh TS language // service, which is slow. // // Note: twoslash's `// @errors:` directive scanner requires the comment // to be at column 0. Our error fixture therefore has no leading // indentation on the directive line. import { describe, it, expect, beforeAll } from "vitest"; import { readFileSync } from "node:fs"; import { fileURLToPath } from "node:url"; import { dirname, join } from "node:path"; import { create_highlighter } from "../src/index.js"; const here = dirname(fileURLToPath(import.meta.url)); const fixture = (name: string) => readFileSync(join(here, "fixtures", name), "utf8"); describe("@twinkleplop/twoslash-svelte", () => { let highlight: (code: string) => string; beforeAll(() => { highlight = create_highlighter(); }); it("renders hovers on script + template identifiers", () => { const html = highlight(fixture("hover.svelte")); expect(html).toContain(`class="twoslash-hover"`); // Template interpolation `{count}` should also carry a hover. expect(html.match(/twoslash-hover/g)?.length ?? 0).toBeGreaterThan(3); expect(html).toMatchSnapshot(); }); it("renders a ^? query inside a script block", () => { const html = highlight(fixture("query.svelte")); expect(html).toContain(`class="twoslash-query"`); expect(html).toMatchSnapshot(); }); it("renders an expected error with a sibling error-line", () => { const html = highlight(fixture("error.svelte")); expect(html).toContain(`class="twoslash-error"`); expect(html).toContain(`class="twoslash-error-line"`); expect(html).toMatchSnapshot(); }); // `render` is shared with @twinkleplop/twoslash, so what this really pins // is that a zero-length node survives the svelte2tsx round trip — its start // and end map to one offset, which the remap could reject as an empty range. it("renders a ^| completion list inside a script block", () => { const html = highlight(fixture("completions.svelte")); expect(html).toContain(``); expect(html).toContain( `find`, ); // the caret sits after `fin`, so the host lands between the two halves // of the identifier and wraps no text — it holds only the list. expect(html).toMatch( /fin<\/span>.*?<\/span><\/span>d<\/span>/s, ); const opens = (html.match(//g) ?? []).length; expect(opens).toBe(closes); }); it("produces a well-formed
 wrapper", () => {
    const html = highlight(fixture("hover.svelte"));
    expect(html.startsWith(`
`)).toBe(true);
    expect(html.endsWith(`
`)).toBe(true); }); it("closes every span it opens", () => { const html = highlight(fixture("hover.svelte")); const opens = (html.match(//g) ?? []).length; expect(opens).toBe(closes); }); });