|
| 1 | +import { existsSync } from "node:fs"; |
| 2 | +import { fileURLToPath } from "node:url"; |
| 3 | +import { describe, it, expect } from "vitest"; |
| 4 | +import { resolveSync, resolvePathSync } from "../src"; |
| 5 | + |
| 6 | +const tests = [ |
| 7 | + // Resolve to path |
| 8 | + { input: "ufo", action: "resolves" }, |
| 9 | + // Return same input as-is |
| 10 | + { input: "https://foo.com/a/b.js?a=1", action: "same" }, |
| 11 | + // Throw error |
| 12 | + { input: 'script:alert("a")', action: "throws" }, |
| 13 | + { input: "/non/existent", action: "throws" }, |
| 14 | +] as const; |
| 15 | + |
| 16 | +describe("resolveSync", () => { |
| 17 | + for (const test of tests) { |
| 18 | + it(`${test.input} should ${test.action}`, () => { |
| 19 | + switch (test.action) { |
| 20 | + case "resolves": { |
| 21 | + const resolved = resolveSync(test.input, { url: import.meta.url }); |
| 22 | + expect(existsSync(fileURLToPath(resolved))).toBe(true); |
| 23 | + break; |
| 24 | + } |
| 25 | + case "same": { |
| 26 | + const resolved = resolveSync(test.input, { url: import.meta.url }); |
| 27 | + expect(resolved).toBe(test.input); |
| 28 | + break; |
| 29 | + } |
| 30 | + case "throws": { |
| 31 | + expect(() => resolveSync(test.input)).toThrow(); |
| 32 | + break; |
| 33 | + } |
| 34 | + } |
| 35 | + }); |
| 36 | + } |
| 37 | +}); |
| 38 | + |
| 39 | +describe("resolvePathSync", () => { |
| 40 | + for (const test of tests) { |
| 41 | + it(`${test.input} should ${test.action}`, () => { |
| 42 | + switch (test.action) { |
| 43 | + case "resolves": { |
| 44 | + const resolved = resolvePathSync(test.input, { |
| 45 | + url: import.meta.url, |
| 46 | + }); |
| 47 | + expect(existsSync(resolved)).toBe(true); |
| 48 | + break; |
| 49 | + } |
| 50 | + case "same": { |
| 51 | + const resolved = resolvePathSync(test.input, { |
| 52 | + url: import.meta.url, |
| 53 | + }); |
| 54 | + expect(resolved).toBe(test.input); |
| 55 | + break; |
| 56 | + } |
| 57 | + case "throws": { |
| 58 | + expect(() => resolvePathSync(test.input)).toThrow(); |
| 59 | + break; |
| 60 | + } |
| 61 | + } |
| 62 | + }); |
| 63 | + } |
| 64 | +}); |
0 commit comments