Skip to content

Commit e5c7b50

Browse files
committed
test: merge compiler with main tests
1 parent 991e7d8 commit e5c7b50

File tree

3 files changed

+75
-58
lines changed

3 files changed

+75
-58
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"lint:fix": "automd && eslint --fix . && prettier -w src test",
2525
"release": "pnpm test && pnpm build && changelogen --release && git push --follow-tags && npm publish",
2626
"test": "pnpm lint && pnpm test:types && vitest run --coverage",
27-
"test:compiler": "TEST_COMPILER=1 vitest",
2827
"test:types": "tsc --noEmit"
2928
},
3029
"devDependencies": {

test/basic.test.ts renamed to test/find.test.ts

Lines changed: 65 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { describe, it, expect } from "vitest";
22
import { createRouter, formatTree } from "./_utils.ts";
33
import { findRoute, removeRoute } from "../src/index.ts";
4+
import { compileRouter } from "../src/compiler.ts";
45

5-
describe("Basic router", () => {
6+
describe("route matching", () => {
67
const router = createRouter([
78
"/test",
89
"/test/:id",
@@ -18,6 +19,8 @@ describe("Basic router", () => {
1819
"/wildcard/**",
1920
]);
2021

22+
const compiledLookup = compileRouter(router);
23+
2124
it("snapshot", () => {
2225
expect(formatTree(router.root)).toMatchInlineSnapshot(`
2326
"<root>
@@ -39,55 +42,68 @@ describe("Basic router", () => {
3942
`);
4043
});
4144

42-
it("lookup works", () => {
43-
// Static
44-
expect(findRoute(router, "GET", "/test")).toEqual({
45-
data: { path: "/test" },
46-
});
47-
expect(findRoute(router, "GET", "/test/foo")).toEqual({
48-
data: { path: "/test/foo" },
49-
});
50-
expect(findRoute(router, "GET", "/test/fooo")).toEqual({
51-
data: { path: "/test/fooo" },
52-
});
53-
expect(findRoute(router, "GET", "/another/path")).toEqual({
54-
data: { path: "/another/path" },
55-
});
56-
// Param
57-
expect(findRoute(router, "GET", "/test/123")).toEqual({
58-
data: { path: "/test/:id" },
59-
params: { id: "123" },
60-
});
61-
expect(findRoute(router, "GET", "/test/123/y")).toEqual({
62-
data: { path: "/test/:idY/y" },
63-
params: { idY: "123" },
64-
});
65-
expect(findRoute(router, "GET", "/test/123/y/z")).toEqual({
66-
data: { path: "/test/:idYZ/y/z" },
67-
params: { idYZ: "123" },
68-
});
69-
expect(findRoute(router, "GET", "/test/foo/123")).toEqual({
70-
data: { path: "/test/foo/*" },
71-
params: { _0: "123" },
72-
});
73-
// Wildcard
74-
expect(findRoute(router, "GET", "/test/foo/123/456")).toEqual({
75-
data: { path: "/test/foo/**" },
76-
params: { _: "123/456" },
77-
});
78-
expect(findRoute(router, "GET", "/wildcard/foo")).toEqual({
79-
data: { path: "/wildcard/**" },
80-
params: { _: "foo" },
81-
});
82-
expect(findRoute(router, "GET", "/wildcard/foo/bar")).toEqual({
83-
data: { path: "/wildcard/**" },
84-
params: { _: "foo/bar" },
85-
});
86-
expect(findRoute(router, "GET", "/wildcard")).toEqual({
87-
data: { path: "/wildcard/**" },
88-
params: { _: "" },
45+
const lookups = [
46+
{
47+
name: "findRoute",
48+
match: (method: string, path: string) => findRoute(router, method, path),
49+
},
50+
{
51+
name: "compiledLookup",
52+
match: (method: string, path: string) => compiledLookup(method, path),
53+
},
54+
];
55+
56+
for (const { name, match } of lookups) {
57+
it(`match with ${name}`, () => {
58+
// Static
59+
expect(match("GET", "/test")).toEqual({
60+
data: { path: "/test" },
61+
});
62+
expect(match("GET", "/test/foo")).toEqual({
63+
data: { path: "/test/foo" },
64+
});
65+
expect(match("GET", "/test/fooo")).toEqual({
66+
data: { path: "/test/fooo" },
67+
});
68+
expect(match("GET", "/another/path")).toEqual({
69+
data: { path: "/another/path" },
70+
});
71+
// Param
72+
expect(match("GET", "/test/123")).toEqual({
73+
data: { path: "/test/:id" },
74+
params: { id: "123" },
75+
});
76+
expect(match("GET", "/test/123/y")).toEqual({
77+
data: { path: "/test/:idY/y" },
78+
params: { idY: "123" },
79+
});
80+
expect(match("GET", "/test/123/y/z")).toEqual({
81+
data: { path: "/test/:idYZ/y/z" },
82+
params: { idYZ: "123" },
83+
});
84+
expect(match("GET", "/test/foo/123")).toEqual({
85+
data: { path: "/test/foo/*" },
86+
params: { _0: "123" },
87+
});
88+
// Wildcard
89+
expect(match("GET", "/test/foo/123/456")).toEqual({
90+
data: { path: "/test/foo/**" },
91+
params: { _: "123/456" },
92+
});
93+
expect(match("GET", "/wildcard/foo")).toEqual({
94+
data: { path: "/wildcard/**" },
95+
params: { _: "foo" },
96+
});
97+
expect(match("GET", "/wildcard/foo/bar")).toEqual({
98+
data: { path: "/wildcard/**" },
99+
params: { _: "foo/bar" },
100+
});
101+
expect(match("GET", "/wildcard")).toEqual({
102+
data: { path: "/wildcard/**" },
103+
params: { _: "" },
104+
});
89105
});
90-
});
106+
}
91107

92108
it("remove works", () => {
93109
removeRoute(router, "GET", "/test");

test/router.test.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ function testRouter(
2323
) {
2424
const router = createRouter<{ path?: string }>(routes);
2525

26-
const compiledMatch = process.env.TEST_COMPILER
27-
? compileRouter(router)
28-
: undefined;
26+
const compiledMatch = compileRouter(router);
2927

3028
if (!tests) {
3129
tests = Array.isArray(routes)
@@ -56,11 +54,15 @@ function testRouter(
5654
it.skipIf(tests[path]?.skip)(
5755
`lookup ${path} should be ${JSON.stringify(tests[path])}`,
5856
() => {
59-
if (process.env.TEST_COMPILER) {
60-
expect(compiledMatch!("GET", path)).to.toMatchObject(tests[path]!);
61-
} else {
62-
expect(findRoute(router, "GET", path)).to.toMatchObject(tests[path]!);
63-
}
57+
expect(
58+
findRoute(router, "GET", path),
59+
`findRoute(GET, ${path})`,
60+
).to.toMatchObject(tests[path]!);
61+
62+
expect(
63+
compiledMatch("GET", path),
64+
`compiledMatch(GET, ${path})`,
65+
).to.toMatchObject(tests[path]!);
6466
},
6567
);
6668
}

0 commit comments

Comments
 (0)