|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { parseConfigSetValue } from "./config-cli-path.js"; |
| 3 | + |
| 4 | +describe("parseConfigSetValue", () => { |
| 5 | + it.each([ |
| 6 | + { raw: "42", expected: 42 }, |
| 7 | + { raw: "3.14", expected: 3.14 }, |
| 8 | + { raw: "-0", expected: -0 }, |
| 9 | + { raw: "true", expected: true }, |
| 10 | + { raw: "false", expected: false }, |
| 11 | + { raw: "null", expected: null }, |
| 12 | + { raw: "{a:1}", expected: { a: 1 } }, |
| 13 | + { raw: "[1,2]", expected: [1, 2] }, |
| 14 | + ])("parses $raw as expected", ({ raw, expected }) => { |
| 15 | + expect(parseConfigSetValue(raw, false)).toEqual(expected); |
| 16 | + }); |
| 17 | + |
| 18 | + it("falls back to the raw string when JSON5 parsing fails", () => { |
| 19 | + expect(parseConfigSetValue("hello", false)).toBe("hello"); |
| 20 | + }); |
| 21 | + |
| 22 | + it.each([ |
| 23 | + { raw: "Infinity", label: "Infinity" }, |
| 24 | + { raw: "-Infinity", label: "negative Infinity" }, |
| 25 | + { raw: "NaN", label: "NaN" }, |
| 26 | + { raw: "1e999", label: "overflow exponent" }, |
| 27 | + { raw: "{timeout:1e999}", label: "object with overflow exponent" }, |
| 28 | + { raw: "[1e999]", label: "array with overflow exponent" }, |
| 29 | + ])("rejects $label in value mode", ({ raw }) => { |
| 30 | + expect(() => parseConfigSetValue(raw, false)).toThrow("Value must be a finite number"); |
| 31 | + }); |
| 32 | + |
| 33 | + it("rejects overflow exponent in strict JSON mode with the finite-number error", () => { |
| 34 | + expect(() => parseConfigSetValue("1e999", true)).toThrow("Value must be a finite number"); |
| 35 | + }); |
| 36 | + |
| 37 | + it.each([ |
| 38 | + { raw: "Infinity", label: "Infinity" }, |
| 39 | + { raw: "-Infinity", label: "negative Infinity" }, |
| 40 | + { raw: "NaN", label: "NaN" }, |
| 41 | + ])("rejects $label in strict JSON mode as invalid JSON", ({ raw }) => { |
| 42 | + expect(() => parseConfigSetValue(raw, true)).toThrow(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("still reports JSON parse errors in strict JSON mode", () => { |
| 46 | + expect(() => parseConfigSetValue("not-json", true)).toThrow( |
| 47 | + /(Unexpected token|Expected).*not-json/, |
| 48 | + ); |
| 49 | + }); |
| 50 | +}); |
0 commit comments