-
-
Notifications
You must be signed in to change notification settings - Fork 488
Expand file tree
/
Copy pathindex.spec.ts
More file actions
252 lines (218 loc) · 6.91 KB
/
Copy pathindex.spec.ts
File metadata and controls
252 lines (218 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import { describe, it, expect } from "vitest";
import {
parse,
compile,
match,
stringify,
pathToRegexp,
TokenData,
PathError,
} from "./index.js";
import {
PARSER_TESTS,
COMPILE_TESTS,
MATCH_TESTS,
STRINGIFY_TESTS,
} from "./cases.spec.js";
/**
* Dynamically generate the entire test suite.
*/
describe("path-to-regexp", () => {
describe("ParseError", () => {
it("should contain original path and debug url", () => {
const error = new PathError(
"Unexpected end at index 7, expected }",
"/{:foo,",
);
expect(error).toBeInstanceOf(TypeError);
expect(error.message).toBe(
"Unexpected end at index 7, expected }: /{:foo,; visit https://git.new/pathToRegexpError for info",
);
expect(error.originalPath).toBe("/{:foo,");
});
it("should omit original url when undefined", () => {
const error = new PathError(
"Unexpected end at index 7, expected }",
undefined,
);
expect(error).toBeInstanceOf(TypeError);
expect(error.message).toBe(
"Unexpected end at index 7, expected }; visit https://git.new/pathToRegexpError for info",
);
expect(error.originalPath).toBeUndefined();
});
});
describe("parse errors", () => {
it("should throw on unbalanced group", () => {
expect(() => parse("/{:foo,")).toThrow(
new PathError("Unexpected end at index 7, expected }", "/{:foo,"),
);
});
it("should throw on nested unbalanced group", () => {
expect(() => parse("/{:foo/{x,y}")).toThrow(
new PathError("Unexpected end at index 12, expected }", "/{:foo/{x,y}"),
);
});
it("should throw on missing param name", () => {
expect(() => parse("/:/")).toThrow(
new PathError("Missing parameter name at index 2", "/:/"),
);
});
it("should throw on missing wildcard name", () => {
expect(() => parse("/*/")).toThrow(
new PathError("Missing parameter name at index 2", "/*/"),
);
});
it("should throw on unterminated quote", () => {
expect(() => parse('/:"foo')).toThrow(
new PathError("Unterminated quote at index 2", '/:"foo'),
);
});
it("should throw on incomplete pattern", () => {
expect(() => parse("/(")).toThrow(
new PathError("Unbalanced pattern at index 2", "/("),
);
});
it("should throw on missing opening pattern", () => {
expect(() => parse("/)")).toThrow(
new PathError("Unexpected ) at index 1, expected end", "/)"),
);
});
});
describe("compile errors", () => {
it("should throw when a param is missing", () => {
const toPath = compile("/a/:b/c");
expect(() => {
toPath();
}).toThrow(new TypeError("Missing parameters: b"));
});
it("should throw when expecting a repeated value", () => {
const toPath = compile("/*foo");
expect(() => {
toPath({ foo: [] });
}).toThrow(new TypeError('Expected "foo" to be a non-empty array'));
});
it("should throw when param gets an array", () => {
const toPath = compile("/:foo");
expect(() => {
toPath({ foo: [] });
}).toThrow(new TypeError('Expected "foo" to be a string'));
});
it("should throw when a wildcard is not an array", () => {
const toPath = compile("/*foo");
expect(() => {
toPath({ foo: "a" });
}).toThrow(new TypeError('Expected "foo" to be a non-empty array'));
});
it("should throw when a wildcard array value is not a string", () => {
const toPath = compile("/*foo");
expect(() => {
toPath({ foo: [1, "a"] as any });
}).toThrow(new TypeError('Expected "foo/0" to be a string'));
});
});
describe("pathToRegexp errors", () => {
it("should throw when missing text between params", () => {
expect(() => pathToRegexp("/:foo:bar")).toThrow(
new PathError('Missing text before "bar" param', "/:foo:bar"),
);
});
it("should throw when missing text between params using TokenData", () => {
expect(() =>
pathToRegexp(
new TokenData([
{ type: "param", name: "a" },
{ type: "param", name: "b" },
]),
),
).toThrow(new PathError('Missing text before "b" param', undefined));
});
it("should throw with `originalPath` when missing text between params using TokenData", () => {
expect(() =>
pathToRegexp(
new TokenData(
[
{ type: "param", name: "a" },
{ type: "param", name: "b" },
],
"/[a][b]",
),
),
).toThrow(new PathError('Missing text before "b" param', "/[a][b]"));
});
it("should contain the error line", () => {
expect.hasAssertions();
try {
pathToRegexp("/:");
} catch (error) {
const stack = (error as Error).stack
?.split("\n")
.slice(0, 5)
.join("\n");
expect(stack).toContain("index.spec.ts");
}
});
describe("patterns", () => {
it("should throw on unsupported pattern", () => {
expect(() => pathToRegexp("/:foo(??)")).toThrow(
new PathError(
'Unsupported pattern "??" for "foo" param',
"/:foo(??)",
),
);
});
it("should throw on empty pattern", () => {
expect(() => pathToRegexp("/:foo()")).toThrow(
new PathError('Unsupported pattern "" for "foo" param', "/:foo()"),
);
});
});
});
describe("stringify errors", () => {
it("should error on unknown token", () => {
expect(() =>
stringify({ tokens: [{ type: "unknown", value: "test" } as any] }),
).toThrow(new TypeError("Unknown token type: unknown"));
});
});
describe.each(PARSER_TESTS)(
"parse $path with $options",
({ path, options, expected }) => {
it("should parse the path", () => {
const data = parse(path, options);
expect(data).toEqual(expected);
});
},
);
describe.each(STRINGIFY_TESTS)(
"stringify $tokens with $options",
({ data, expected }) => {
it("should stringify the path", () => {
const path = stringify(data);
expect(path).toEqual(expected);
});
},
);
describe.each(COMPILE_TESTS)(
"compile $path with $options",
({ path, options, tests }) => {
it.each(tests)("should compile $input", ({ input, expected }) => {
const toPath = compile(path, options);
if (expected === null) {
expect(() => toPath(input)).toThrow();
} else {
expect(toPath(input)).toEqual(expected);
}
});
},
);
describe.each(MATCH_TESTS)(
"match $path with $options",
({ path, options, tests }) => {
it.each(tests)("should match $input", ({ input, expected }) => {
const fn = match(path, options);
expect(fn(input)).toEqual(expected);
});
},
);
});