Skip to content

Commit 991e7d8

Browse files
committed
test: snapshot compiler result
1 parent f40d606 commit 991e7d8

File tree

3 files changed

+87
-4
lines changed

3 files changed

+87
-4
lines changed

β€Žeslint.config.mjsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import unjs from "eslint-config-unjs";
22

33
// https://github.com/unjs/eslint-config
44
export default unjs({
5-
ignores: [],
5+
ignores: ["test/.snapshot"],
66
rules: {
77
"unicorn/no-null": 0,
88
"@typescript-eslint/no-non-null-assertion": 0,

β€Žtest/.snapshot/compiler.mjsβ€Ž

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
(m, p) => {
2+
if (p[p.length - 1] === "/") p = p.slice(0, -1);
3+
if (p === "/test") {
4+
if (m === "GET") return { data: d1 };
5+
}
6+
if (p === "/test/foo") {
7+
if (m === "GET") return { data: d2 };
8+
}
9+
if (p === "/test/foo/bar/qux") {
10+
if (m === "GET") return { data: d3 };
11+
}
12+
if (p === "/test/foo/baz") {
13+
if (m === "GET") return { data: d4 };
14+
}
15+
if (p === "/test/fooo") {
16+
if (m === "GET") return { data: d5 };
17+
}
18+
if (p === "/another/path") {
19+
if (m === "GET") return { data: d6 };
20+
}
21+
let s = p.split("/").filter((q) => q !== ""),
22+
l = s.length;
23+
if (s[0] === "test") {
24+
if (l === 1) {
25+
if (m === "GET") return { data: d7 };
26+
}
27+
if (s[1] === "foo") {
28+
if (l === 2) {
29+
if (m === "GET") return { data: d8 };
30+
}
31+
if (s[2] === "bar") {
32+
if (s[3] === "qux") {
33+
if (l === 4) {
34+
if (m === "GET") return { data: d9 };
35+
}
36+
}
37+
}
38+
if (s[2] === "baz") {
39+
if (l === 3) {
40+
if (m === "GET") return { data: d10 };
41+
}
42+
}
43+
if (l === 3 || l === 2) {
44+
if (m === "GET") return { data: d11, params: { _0: s[2] } };
45+
}
46+
if (m === "GET")
47+
return { data: d12, params: { _: s.slice(2).join("/") } };
48+
}
49+
if (s[1] === "fooo") {
50+
if (l === 2) {
51+
if (m === "GET") return { data: d13 };
52+
}
53+
}
54+
if (l === 2 || l === 1) {
55+
if (m === "GET") if (l >= 2) return { data: d14, params: { id: s[1] } };
56+
}
57+
if (s[2] === "y") {
58+
if (l === 3) {
59+
if (m === "GET") return { data: d15, params: { idY: s[1] } };
60+
}
61+
if (s[3] === "z") {
62+
if (l === 4) {
63+
if (m === "GET") return { data: d16, params: { idYZ: s[1] } };
64+
}
65+
}
66+
}
67+
}
68+
if (s[0] === "another") {
69+
if (s[1] === "path") {
70+
if (l === 2) {
71+
if (m === "GET") return { data: d17 };
72+
}
73+
}
74+
}
75+
if (s[0] === "wildcard") {
76+
if (m === "GET") return { data: d18, params: { _: s.slice(1).join("/") } };
77+
}
78+
};

β€Žtest/compiler.test.tsβ€Ž

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, it, expect } from "vitest";
22
import { createRouter, formatTree } from "./_utils.ts";
33
import { compileRouter } from "../src/compiler.ts";
4+
import { format } from "prettier";
45

56
describe("Compiled router", () => {
67
const router = createRouter([
@@ -18,7 +19,9 @@ describe("Compiled router", () => {
1819
"/wildcard/**",
1920
]);
2021

21-
it("snapshot", () => {
22+
const compiledLookup = compileRouter(router);
23+
24+
it("snapshot", async () => {
2225
expect(formatTree(router.root)).toMatchInlineSnapshot(`
2326
"<root>
2427
β”œβ”€β”€ /test β”ˆ> [GET] /test
@@ -37,11 +40,13 @@ describe("Compiled router", () => {
3740
β”œβ”€β”€ /wildcard
3841
β”‚ β”œβ”€β”€ /** β”ˆ> [GET] /wildcard/**"
3942
`);
43+
44+
await expect(
45+
await format(compiledLookup.toString(), { parser: "acorn" }),
46+
).toMatchFileSnapshot(".snapshot/compiler.mjs");
4047
});
4148

4249
it("lookup works", () => {
43-
const compiledLookup = compileRouter(router);
44-
4550
// Static
4651
expect(compiledLookup("GET", "/test")).toEqual({
4752
data: { path: "/test" },

0 commit comments

Comments
Β (0)