6
6
stringify ,
7
7
pathToRegexp ,
8
8
TokenData ,
9
+ PathError ,
9
10
} from "./index.js" ;
10
11
import {
11
12
PARSER_TESTS ,
@@ -18,44 +19,62 @@ import {
18
19
* Dynamically generate the entire test suite.
19
20
*/
20
21
describe ( "path-to-regexp" , ( ) => {
22
+ describe ( "ParseError" , ( ) => {
23
+ it ( "should contain original path and debug url" , ( ) => {
24
+ const error = new PathError (
25
+ "Unexpected END at index 7, expected }" ,
26
+ "/{:foo," ,
27
+ ) ;
28
+
29
+ expect ( error ) . toBeInstanceOf ( TypeError ) ;
30
+ expect ( error . message ) . toBe (
31
+ "Unexpected END at index 7, expected }: /{:foo,; visit https://git.new/pathToRegexpError for info" ,
32
+ ) ;
33
+ expect ( error . originalPath ) . toBe ( "/{:foo," ) ;
34
+ } ) ;
35
+
36
+ it ( "should omit original url when undefined" , ( ) => {
37
+ const error = new PathError (
38
+ "Unexpected END at index 7, expected }" ,
39
+ undefined ,
40
+ ) ;
41
+
42
+ expect ( error ) . toBeInstanceOf ( TypeError ) ;
43
+ expect ( error . message ) . toBe (
44
+ "Unexpected END at index 7, expected }; visit https://git.new/pathToRegexpError for info" ,
45
+ ) ;
46
+ expect ( error . originalPath ) . toBeUndefined ( ) ;
47
+ } ) ;
48
+ } ) ;
49
+
21
50
describe ( "parse errors" , ( ) => {
22
51
it ( "should throw on unbalanced group" , ( ) => {
23
52
expect ( ( ) => parse ( "/{:foo," ) ) . toThrow (
24
- new TypeError (
25
- "Unexpected END at index 7, expected }: /{:foo,; visit https://git.new/pathToRegexpError for info" ,
26
- ) ,
53
+ new PathError ( "Unexpected END at index 7, expected }" , "/{:foo," ) ,
27
54
) ;
28
55
} ) ;
29
56
30
57
it ( "should throw on nested unbalanced group" , ( ) => {
31
58
expect ( ( ) => parse ( "/{:foo/{x,y}" ) ) . toThrow (
32
- new TypeError (
33
- "Unexpected END at index 12, expected }: /{:foo/{x,y}; visit https://git.new/pathToRegexpError for info" ,
34
- ) ,
59
+ new PathError ( "Unexpected END at index 12, expected }" , "/{:foo/{x,y}" ) ,
35
60
) ;
36
61
} ) ;
37
62
38
63
it ( "should throw on missing param name" , ( ) => {
39
64
expect ( ( ) => parse ( "/:/" ) ) . toThrow (
40
- new TypeError (
41
- "Missing parameter name at index 2: /:/; visit https://git.new/pathToRegexpError for info" ,
42
- ) ,
65
+ new PathError ( "Missing parameter name at index 2" , "/:/" ) ,
43
66
) ;
44
67
} ) ;
45
68
46
69
it ( "should throw on missing wildcard name" , ( ) => {
47
70
expect ( ( ) => parse ( "/*/" ) ) . toThrow (
48
- new TypeError (
49
- "Missing parameter name at index 2: /*/; visit https://git.new/pathToRegexpError for info" ,
50
- ) ,
71
+ new PathError ( "Missing parameter name at index 2" , "/*/" ) ,
51
72
) ;
52
73
} ) ;
53
74
54
75
it ( "should throw on unterminated quote" , ( ) => {
55
76
expect ( ( ) => parse ( '/:"foo' ) ) . toThrow (
56
- new TypeError (
57
- 'Unterminated quote at index 2: /:"foo; visit https://git.new/pathToRegexpError for info' ,
58
- ) ,
77
+ new PathError ( "Unterminated quote at index 2" , '/:"foo' ) ,
59
78
) ;
60
79
} ) ;
61
80
} ) ;
@@ -105,9 +124,7 @@ describe("path-to-regexp", () => {
105
124
describe ( "pathToRegexp errors" , ( ) => {
106
125
it ( "should throw when missing text between params" , ( ) => {
107
126
expect ( ( ) => pathToRegexp ( "/:foo:bar" ) ) . toThrow (
108
- new TypeError (
109
- 'Missing text before "bar": /:foo:bar; visit https://git.new/pathToRegexpError for info' ,
110
- ) ,
127
+ new PathError ( 'Missing text before "bar" param' , "/:foo:bar" ) ,
111
128
) ;
112
129
} ) ;
113
130
@@ -119,11 +136,7 @@ describe("path-to-regexp", () => {
119
136
{ type : "param" , name : "b" } ,
120
137
] ) ,
121
138
) ,
122
- ) . toThrow (
123
- new TypeError (
124
- 'Missing text before "b"; visit https://git.new/pathToRegexpError for info' ,
125
- ) ,
126
- ) ;
139
+ ) . toThrow ( new PathError ( 'Missing text before "b" param' , undefined ) ) ;
127
140
} ) ;
128
141
129
142
it ( "should throw with `originalPath` when missing text between params using TokenData" , ( ) => {
@@ -137,11 +150,7 @@ describe("path-to-regexp", () => {
137
150
"/[a][b]" ,
138
151
) ,
139
152
) ,
140
- ) . toThrow (
141
- new TypeError (
142
- 'Missing text before "b": /[a][b]; visit https://git.new/pathToRegexpError for info' ,
143
- ) ,
144
- ) ;
153
+ ) . toThrow ( new PathError ( 'Missing text before "b" param' , "/[a][b]" ) ) ;
145
154
} ) ;
146
155
147
156
it ( "should contain the error line" , ( ) => {
0 commit comments