66 stringify ,
77 pathToRegexp ,
88 TokenData ,
9+ PathError ,
910} from "./index.js" ;
1011import {
1112 PARSER_TESTS ,
@@ -18,44 +19,62 @@ import {
1819 * Dynamically generate the entire test suite.
1920 */
2021describe ( "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+
2150 describe ( "parse errors" , ( ) => {
2251 it ( "should throw on unbalanced group" , ( ) => {
2352 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," ) ,
2754 ) ;
2855 } ) ;
2956
3057 it ( "should throw on nested unbalanced group" , ( ) => {
3158 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}" ) ,
3560 ) ;
3661 } ) ;
3762
3863 it ( "should throw on missing param name" , ( ) => {
3964 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" , "/:/" ) ,
4366 ) ;
4467 } ) ;
4568
4669 it ( "should throw on missing wildcard name" , ( ) => {
4770 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" , "/*/" ) ,
5172 ) ;
5273 } ) ;
5374
5475 it ( "should throw on unterminated quote" , ( ) => {
5576 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' ) ,
5978 ) ;
6079 } ) ;
6180 } ) ;
@@ -105,9 +124,7 @@ describe("path-to-regexp", () => {
105124 describe ( "pathToRegexp errors" , ( ) => {
106125 it ( "should throw when missing text between params" , ( ) => {
107126 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" ) ,
111128 ) ;
112129 } ) ;
113130
@@ -119,11 +136,7 @@ describe("path-to-regexp", () => {
119136 { type : "param" , name : "b" } ,
120137 ] ) ,
121138 ) ,
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 ) ) ;
127140 } ) ;
128141
129142 it ( "should throw with `originalPath` when missing text between params using TokenData" , ( ) => {
@@ -137,11 +150,7 @@ describe("path-to-regexp", () => {
137150 "/[a][b]" ,
138151 ) ,
139152 ) ,
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]" ) ) ;
145154 } ) ;
146155
147156 it ( "should contain the error line" , ( ) => {
0 commit comments