6
6
stringify ,
7
7
pathToRegexp ,
8
8
TokenData ,
9
+ ParseError ,
9
10
} from "./index.js" ;
10
11
import {
11
12
PARSER_TESTS ,
@@ -18,44 +19,65 @@ 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 ParseError (
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 ParseError (
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 ParseError ( "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" ,
59
+ new ParseError (
60
+ "Unexpected END at index 12, expected }" ,
61
+ "/{:foo/{x,y}" ,
34
62
) ,
35
63
) ;
36
64
} ) ;
37
65
38
66
it ( "should throw on missing param name" , ( ) => {
39
67
expect ( ( ) => parse ( "/:/" ) ) . toThrow (
40
- new TypeError (
41
- "Missing parameter name at index 2: /:/; visit https://git.new/pathToRegexpError for info" ,
42
- ) ,
68
+ new ParseError ( "Missing parameter name at index 2" , "/:/" ) ,
43
69
) ;
44
70
} ) ;
45
71
46
72
it ( "should throw on missing wildcard name" , ( ) => {
47
73
expect ( ( ) => parse ( "/*/" ) ) . toThrow (
48
- new TypeError (
49
- "Missing parameter name at index 2: /*/; visit https://git.new/pathToRegexpError for info" ,
50
- ) ,
74
+ new ParseError ( "Missing parameter name at index 2" , "/*/" ) ,
51
75
) ;
52
76
} ) ;
53
77
54
78
it ( "should throw on unterminated quote" , ( ) => {
55
79
expect ( ( ) => parse ( '/:"foo' ) ) . toThrow (
56
- new TypeError (
57
- 'Unterminated quote at index 2: /:"foo; visit https://git.new/pathToRegexpError for info' ,
58
- ) ,
80
+ new ParseError ( "Unterminated quote at index 2" , '/:"foo' ) ,
59
81
) ;
60
82
} ) ;
61
83
} ) ;
@@ -105,9 +127,7 @@ describe("path-to-regexp", () => {
105
127
describe ( "pathToRegexp errors" , ( ) => {
106
128
it ( "should throw when missing text between params" , ( ) => {
107
129
expect ( ( ) => pathToRegexp ( "/:foo:bar" ) ) . toThrow (
108
- new TypeError (
109
- 'Missing text before "bar": /:foo:bar; visit https://git.new/pathToRegexpError for info' ,
110
- ) ,
130
+ new ParseError ( 'Missing text before "bar" param' , "/:foo:bar" ) ,
111
131
) ;
112
132
} ) ;
113
133
@@ -119,11 +139,7 @@ describe("path-to-regexp", () => {
119
139
{ type : "param" , name : "b" } ,
120
140
] ) ,
121
141
) ,
122
- ) . toThrow (
123
- new TypeError (
124
- 'Missing text before "b"; visit https://git.new/pathToRegexpError for info' ,
125
- ) ,
126
- ) ;
142
+ ) . toThrow ( new ParseError ( 'Missing text before "b" param' , undefined ) ) ;
127
143
} ) ;
128
144
129
145
it ( "should throw with `originalPath` when missing text between params using TokenData" , ( ) => {
@@ -137,11 +153,7 @@ describe("path-to-regexp", () => {
137
153
"/[a][b]" ,
138
154
) ,
139
155
) ,
140
- ) . toThrow (
141
- new TypeError (
142
- 'Missing text before "b": /[a][b]; visit https://git.new/pathToRegexpError for info' ,
143
- ) ,
144
- ) ;
156
+ ) . toThrow ( new ParseError ( 'Missing text before "b" param' , "/[a][b]" ) ) ;
145
157
} ) ;
146
158
147
159
it ( "should contain the error line" , ( ) => {
0 commit comments