| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { fromHarnessOptions, resolveExportTarget } from "../index.ts"; |
| 3 | |
| 4 | describe("fromHarnessOptions", () => { |
| 5 | it("maps the shared vite options onto SvelteKit options", () => { |
| 6 | const bindings = ["text-binding-hook"]; |
| 7 | const options = fromHarnessOptions({ |
| 8 | vite: { |
| 9 | compatibilityDate: "2026-03-10", |
| 10 | compatibilityFlags: ["nodejs_compat"], |
| 11 | worker: { |
| 12 | bindings, |
| 13 | assets: { notFoundHandling: "404-page" }, |
| 14 | }, |
| 15 | }, |
| 16 | }); |
| 17 | expect(options.compatibilityDate).toBe("2026-03-10"); |
| 18 | expect(options.compatibilityFlags).toEqual(["nodejs_compat"]); |
| 19 | expect(options.adapter?.notFoundHandling).toBe("404-page"); |
| 20 | // binding hooks pass through wholesale — the deploy target's dev |
| 21 | // platform serves them (resource bindings included) |
| 22 | expect(options.dev?.bindings).toBe(bindings); |
| 23 | }); |
| 24 | |
| 25 | it("prefers the target-scoped carriage over the deprecated vite alias", () => { |
| 26 | const scoped = ["scoped-binding-hook"]; |
| 27 | const options = fromHarnessOptions({ |
| 28 | target: { |
| 29 | cloudflare: { |
| 30 | worker: { |
| 31 | compatibilityDate: "2026-03-10", |
| 32 | worker: { |
| 33 | bindings: scoped, |
| 34 | assets: { notFoundHandling: "single-page-application" }, |
| 35 | }, |
| 36 | }, |
| 37 | }, |
| 38 | }, |
| 39 | vite: { |
| 40 | compatibilityDate: "1999-01-01", |
| 41 | worker: { assets: { notFoundHandling: "none" } }, |
| 42 | }, |
| 43 | }); |
| 44 | expect(options.compatibilityDate).toBe("2026-03-10"); |
| 45 | expect(options.adapter?.notFoundHandling).toBe("single-page-application"); |
| 46 | expect(options.dev?.bindings).toBe(scoped); |
| 47 | }); |
| 48 | |
| 49 | it("falls back to the deprecated vite alias when no target is scoped", () => { |
| 50 | const options = fromHarnessOptions({ |
| 51 | vite: { compatibilityDate: "2026-03-10" }, |
| 52 | }); |
| 53 | expect(options.compatibilityDate).toBe("2026-03-10"); |
| 54 | }); |
| 55 | |
| 56 | it("tolerates fully-empty options", () => { |
| 57 | const options = fromHarnessOptions({}); |
| 58 | expect(options.compatibilityDate).toBeUndefined(); |
| 59 | expect(options.dev?.bindings).toBeUndefined(); |
| 60 | }); |
| 61 | }); |
| 62 | |
| 63 | describe("resolveExportTarget", () => { |
| 64 | it("accepts a plain string target", () => { |
| 65 | expect(resolveExportTarget("./src/exports/vite/index.js")).toBe( |
| 66 | "./src/exports/vite/index.js", |
| 67 | ); |
| 68 | }); |
| 69 | |
| 70 | it("picks the import condition (kit's ESM-only ./vite export)", () => { |
| 71 | expect( |
| 72 | resolveExportTarget({ |
| 73 | types: "./types/index.d.ts", |
| 74 | import: "./src/exports/vite/index.js", |
| 75 | }), |
| 76 | ).toBe("./src/exports/vite/index.js"); |
| 77 | }); |
| 78 | |
| 79 | it("falls back to default and resolves nested conditions", () => { |
| 80 | expect( |
| 81 | resolveExportTarget({ default: { import: "./dist/index.js" } }), |
| 82 | ).toBe("./dist/index.js"); |
| 83 | }); |
| 84 | |
| 85 | it("returns undefined for unusable entries", () => { |
| 86 | expect(resolveExportTarget(undefined)).toBeUndefined(); |
| 87 | expect( |
| 88 | resolveExportTarget({ types: "./types/index.d.ts" }), |
| 89 | ).toBeUndefined(); |
| 90 | expect(resolveExportTarget(null)).toBeUndefined(); |
| 91 | }); |
| 92 | }); |