Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

semver.test.ts

62 lines2.5 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 compareSemverVersions,
5 normalizeSemverVersion,
6 parseSemver,
7 satisfiesSemverRange,
8} from "./semver.ts";
9​
10describe("semver helpers", () => {
11 it("matches supported range groups", () => {
12 const range = "^22.16 || ^23.11 || >=24.10";
13​
14 expect(satisfiesSemverRange("22.16.0", range)).toBe(true);
15 expect(satisfiesSemverRange("23.11.1", range)).toBe(true);
16 expect(satisfiesSemverRange("24.10.0", range)).toBe(true);
17 expect(satisfiesSemverRange("22.15.9", range)).toBe(false);
18 expect(satisfiesSemverRange("23.10.9", range)).toBe(false);
19 expect(satisfiesSemverRange("24.9.9", range)).toBe(false);
20 });
21​
22 it("normalizes and parses shorthand major-only versions", () => {
23 expect(normalizeSemverVersion("20")).toBe("20.0.0");
24 expect(normalizeSemverVersion("v18")).toBe("v18.0.0");
25 expect(normalizeSemverVersion("20-rc.1")).toBe("20.0.0-rc.1");
26 expect(parseSemver("20")).toEqual({ major: 20, minor: 0, patch: 0, prerelease: [] });
27 });
28​
29 it("compares shorthand versions numerically instead of lexically", () => {
30 expect(compareSemverVersions("20", "9")).toBeGreaterThan(0);
31 expect(compareSemverVersions("18", "18.0.0")).toBe(0);
32 });
33​
34 it("compares prerelease versions before stable versions", () => {
35 expect(compareSemverVersions("2.1.111-beta.1", "2.1.111")).toBeLessThan(0);
36 });
37​
38 it("supports comparison comparators", () => {
39 expect(satisfiesSemverRange("24.9.0", ">=24.0 <24.10")).toBe(true);
40 expect(satisfiesSemverRange("24.10.0", ">=24.0 <24.10")).toBe(false);
41 });
42​
43 it("honors caret range upper bounds for zero-major versions", () => {
44 expect(satisfiesSemverRange("0.2.3", "^0.2.3")).toBe(true);
45 expect(satisfiesSemverRange("0.2.9", "^0.2.3")).toBe(true);
46 expect(satisfiesSemverRange("0.3.0", "^0.2.3")).toBe(false);
47 expect(satisfiesSemverRange("0.5.0", "^0.2.3")).toBe(false);
48 expect(satisfiesSemverRange("0.0.3", "^0.0.3")).toBe(true);
49 expect(satisfiesSemverRange("0.0.4", "^0.0.3")).toBe(false);
50 });
51​
52 it("keeps the range checker stringifiable and executable as plain JavaScript", () => {
53 const source = satisfiesSemverRange.toString();
54 const recreated = Function(`return (${source});`)() as typeof satisfiesSemverRange;
55​
56 expect(source).toContain("function satisfiesSemverRange");
57 expect(source).not.toContain(": string");
58 expect(source).not.toContain(": boolean");
59 expect(recreated("24.10.0", ">=24.10")).toBe(true);
60 expect(recreated("24.9.9", ">=24.10")).toBe(false);
61 });
62});