Skip to content

Commit 98246be

Browse files
committed
Add more test coverage for error message
1 parent 3f25805 commit 98246be

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

src/cases.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ export const PARSER_TESTS: ParserTestSet[] = [
5353
"/:test",
5454
),
5555
},
56+
{
57+
path: "/:a:b",
58+
expected: new TokenData(
59+
[
60+
{ type: "text", value: "/" },
61+
{ type: "param", name: "a" },
62+
{ type: "param", name: "b" },
63+
],
64+
"/:a:b",
65+
),
66+
},
5667
{
5768
path: '/:"0"',
5869
expected: new TokenData(

src/index.spec.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { describe, it, expect } from "vitest";
2-
import { parse, compile, match, stringify } from "./index.js";
2+
import {
3+
parse,
4+
compile,
5+
match,
6+
stringify,
7+
pathToRegexp,
8+
TokenData,
9+
} from "./index.js";
310
import {
411
PARSER_TESTS,
512
COMPILE_TESTS,
@@ -19,6 +26,7 @@ describe("path-to-regexp", () => {
1926
),
2027
);
2128
});
29+
2230
it("should throw on nested unbalanced group", () => {
2331
expect(() => parse("/{:foo/{x,y}")).toThrow(
2432
new TypeError(
@@ -94,6 +102,49 @@ describe("path-to-regexp", () => {
94102
});
95103
});
96104

105+
describe("pathToRegexp errors", () => {
106+
it("should throw when missing text between params", () => {
107+
expect(() => pathToRegexp("/:foo:bar")).toThrow(
108+
new TypeError(
109+
'Missing text before "bar": /:foo:bar; visit https://git.new/pathToRegexpError for more info',
110+
),
111+
);
112+
});
113+
114+
it("should throw when missing text between params using TokenData", () => {
115+
expect(() =>
116+
pathToRegexp(
117+
new TokenData([
118+
{ type: "param", name: "a" },
119+
{ type: "param", name: "b" },
120+
]),
121+
),
122+
).toThrow(
123+
new TypeError(
124+
'Missing text before "b"; visit https://git.new/pathToRegexpError for more info',
125+
),
126+
);
127+
});
128+
129+
it("should throw with `originalPath` when missing text between params using TokenData", () => {
130+
expect(() =>
131+
pathToRegexp(
132+
new TokenData(
133+
[
134+
{ type: "param", name: "a" },
135+
{ type: "param", name: "b" },
136+
],
137+
"/[a][b]",
138+
),
139+
),
140+
).toThrow(
141+
new TypeError(
142+
'Missing text before "b": /[a][b]; visit https://git.new/pathToRegexpError for more info',
143+
),
144+
);
145+
});
146+
});
147+
97148
describe.each(PARSER_TESTS)(
98149
"parse $path with $options",
99150
({ path, options, expected }) => {

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ function toRegExp(
604604
if (token.type === "param" || token.type === "wildcard") {
605605
if (!isSafeSegmentParam && !backtrack) {
606606
throw new TypeError(
607-
errorMessage(`Missing text after "${token.name}"`, originalPath),
607+
errorMessage(`Missing text before "${token.name}"`, originalPath),
608608
);
609609
}
610610

0 commit comments

Comments
 (0)