@@ -25,7 +25,7 @@ describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
25
25
await harness . writeFile ( 'src/main.ts' , '' ) ;
26
26
} ) ;
27
27
28
- it ( 'proxies requests based on the JSON proxy configuration file provided in the option' , async ( ) => {
28
+ it ( 'proxies requests based on the `.json` proxy configuration file provided in the option' , async ( ) => {
29
29
harness . useTarget ( 'serve' , {
30
30
...BASE_OPTIONS ,
31
31
proxyConfig : 'proxy.config.json' ,
@@ -49,7 +49,34 @@ describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
49
49
}
50
50
} ) ;
51
51
52
- it ( 'proxies requests based on the JS proxy configuration file provided in the option' , async ( ) => {
52
+ it ( 'proxies requests based on the `.json` (with comments) proxy configuration file provided in the option' , async ( ) => {
53
+ harness . useTarget ( 'serve' , {
54
+ ...BASE_OPTIONS ,
55
+ proxyConfig : 'proxy.config.json' ,
56
+ } ) ;
57
+
58
+ const proxyServer = createProxyServer ( ) ;
59
+ try {
60
+ await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
61
+ const proxyAddress = proxyServer . address ( ) as import ( 'net' ) . AddressInfo ;
62
+
63
+ await harness . writeFiles ( {
64
+ 'proxy.config.json' : `
65
+ // JSON file with comments
66
+ { "/api/*": { "target": "http://127.0.0.1:${ proxyAddress . port } " } }
67
+ ` ,
68
+ } ) ;
69
+
70
+ const { result, response } = await executeOnceAndFetch ( harness , '/api/test' ) ;
71
+
72
+ expect ( result ?. success ) . toBeTrue ( ) ;
73
+ expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
74
+ } finally {
75
+ await new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ;
76
+ }
77
+ } ) ;
78
+
79
+ it ( 'proxies requests based on the `.js` (CommonJS) proxy configuration file provided in the option' , async ( ) => {
53
80
harness . useTarget ( 'serve' , {
54
81
...BASE_OPTIONS ,
55
82
proxyConfig : 'proxy.config.js' ,
@@ -73,7 +100,56 @@ describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
73
100
}
74
101
} ) ;
75
102
76
- it ( 'proxies requests based on the MJS proxy configuration file provided in the option' , async ( ) => {
103
+ it ( 'proxies requests based on the `.js` (ESM) proxy configuration file provided in the option' , async ( ) => {
104
+ harness . useTarget ( 'serve' , {
105
+ ...BASE_OPTIONS ,
106
+ proxyConfig : 'proxy.config.js' ,
107
+ } ) ;
108
+
109
+ const proxyServer = createProxyServer ( ) ;
110
+ try {
111
+ await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
112
+ const proxyAddress = proxyServer . address ( ) as import ( 'net' ) . AddressInfo ;
113
+
114
+ await harness . writeFiles ( {
115
+ 'proxy.config.js' : `export default { "/api/*": { "target": "http://127.0.0.1:${ proxyAddress . port } " } }` ,
116
+ 'package.json' : '{ "type": "module" }' ,
117
+ } ) ;
118
+
119
+ const { result, response } = await executeOnceAndFetch ( harness , '/api/test' ) ;
120
+
121
+ expect ( result ?. success ) . toBeTrue ( ) ;
122
+ expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
123
+ } finally {
124
+ await new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ;
125
+ }
126
+ } ) ;
127
+
128
+ it ( 'proxies requests based on the `.cjs` proxy configuration file provided in the option' , async ( ) => {
129
+ harness . useTarget ( 'serve' , {
130
+ ...BASE_OPTIONS ,
131
+ proxyConfig : 'proxy.config.cjs' ,
132
+ } ) ;
133
+
134
+ const proxyServer = createProxyServer ( ) ;
135
+ try {
136
+ await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
137
+ const proxyAddress = proxyServer . address ( ) as import ( 'net' ) . AddressInfo ;
138
+
139
+ await harness . writeFiles ( {
140
+ 'proxy.config.cjs' : `module.exports = { "/api/*": { "target": "http://127.0.0.1:${ proxyAddress . port } " } }` ,
141
+ } ) ;
142
+
143
+ const { result, response } = await executeOnceAndFetch ( harness , '/api/test' ) ;
144
+
145
+ expect ( result ?. success ) . toBeTrue ( ) ;
146
+ expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
147
+ } finally {
148
+ await new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ;
149
+ }
150
+ } ) ;
151
+
152
+ it ( 'proxies requests based on the `.mjs` proxy configuration file provided in the option' , async ( ) => {
77
153
harness . useTarget ( 'serve' , {
78
154
...BASE_OPTIONS ,
79
155
proxyConfig : 'proxy.config.mjs' ,
@@ -112,6 +188,30 @@ describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
112
188
} ) ,
113
189
) ;
114
190
} ) ;
191
+
192
+ it ( 'throws an error when JSON proxy configuration file cannot be parsed' , async ( ) => {
193
+ harness . useTarget ( 'serve' , {
194
+ ...BASE_OPTIONS ,
195
+ proxyConfig : 'proxy.config.json' ,
196
+ } ) ;
197
+
198
+ // Create a JSON file with a parse error (target property has no value)
199
+ await harness . writeFiles ( {
200
+ 'proxy.config.json' : `
201
+ // JSON file with comments
202
+ { "/api/*": { "target": } }
203
+ ` ,
204
+ } ) ;
205
+
206
+ const { result, error } = await harness . executeOnce ( { outputLogsOnException : false } ) ;
207
+
208
+ expect ( result ) . toBeUndefined ( ) ;
209
+ expect ( error ) . toEqual (
210
+ jasmine . objectContaining ( {
211
+ message : jasmine . stringMatching ( 'contains parse errors:\\n\\[3, 35\\] ValueExpected' ) ,
212
+ } ) ,
213
+ ) ;
214
+ } ) ;
115
215
} ) ;
116
216
} ) ;
117
217
0 commit comments