Skip to content

Commit adeb92e

Browse files
authored
feat: allow open option to accept an object (#2492)
* feat: allow open option to accept object * test: update snapshot * test: spread argv * test: remove error checks & use toBeCalledWith instead * test: return logMock error checks * test: remove unnecessary lines * test: update success & failure types for open option
1 parent c6bdfe4 commit adeb92e

File tree

6 files changed

+142
-5
lines changed

6 files changed

+142
-5
lines changed

lib/options.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@
219219
},
220220
{
221221
"type": "boolean"
222+
},
223+
{
224+
"type": "object"
222225
}
223226
]
224227
},
@@ -460,7 +463,7 @@
460463
"mimeTypes": "should be {Object} (https://webpack.js.org/configuration/dev-server/#devservermimetypes-)",
461464
"noInfo": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devservernoinfo-)",
462465
"onListening": "should be {Function} (https://webpack.js.org/configuration/dev-server/#onlistening)",
463-
"open": "should be {String|Boolean} (https://webpack.js.org/configuration/dev-server/#devserveropen)",
466+
"open": "should be {String|Boolean|Object} (https://webpack.js.org/configuration/dev-server/#devserveropen)",
464467
"openPage": "should be {String|Array} (https://webpack.js.org/configuration/dev-server/#devserveropenpage)",
465468
"overlay": "should be {Boolean|Object} (https://webpack.js.org/configuration/dev-server/#devserveroverlay)",
466469
"pfx": "should be {String|Buffer} (https://webpack.js.org/configuration/dev-server/#devserverpfx)",

lib/utils/runOpen.js

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ function runOpen(uri, options, log) {
1111
if (typeof options.open === 'string') {
1212
openOptions = Object.assign({}, openOptions, { app: options.open });
1313
openOptionValue = `: "${options.open}"`;
14+
} else if (typeof options.open === 'object') {
15+
openOptions = options.open;
16+
openOptionValue = `: "${JSON.stringify(options.open)}"`;
1417
}
1518

1619
const pages =

test/options.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ describe('options', () => {
262262
failure: [''],
263263
},
264264
open: {
265-
success: [true, ''],
266-
failure: [{}],
265+
success: [true, '', {}],
266+
failure: [[]],
267267
},
268268
openPage: {
269269
success: [''],

test/server/utils/__snapshots__/createConfig.test.js.snap

+22-1
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,28 @@ Object {
802802
}
803803
`;
804804

805-
exports[`createConfig open option (browser) 1`] = `
805+
exports[`createConfig open option (object) 1`] = `
806+
Object {
807+
"hot": true,
808+
"hotOnly": false,
809+
"noInfo": true,
810+
"open": Object {
811+
"app": Array [
812+
"Google Chrome",
813+
"--incognito",
814+
],
815+
},
816+
"openPage": "",
817+
"port": 8080,
818+
"publicPath": "/",
819+
"stats": Object {
820+
"cached": false,
821+
"cachedAssets": false,
822+
},
823+
}
824+
`;
825+
826+
exports[`createConfig open option (string) 1`] = `
806827
Object {
807828
"hot": true,
808829
"hotOnly": false,

test/server/utils/createConfig.test.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ describe('createConfig', () => {
855855
expect(config).toMatchSnapshot();
856856
});
857857

858-
it('open option (browser)', () => {
858+
it('open option (string)', () => {
859859
const config = createConfig(
860860
webpackConfig,
861861
Object.assign({}, argv, { open: 'Google Chrome' }),
@@ -865,6 +865,20 @@ describe('createConfig', () => {
865865
expect(config).toMatchSnapshot();
866866
});
867867

868+
it('open option (object)', () => {
869+
const config = createConfig(
870+
webpackConfig,
871+
{
872+
...argv,
873+
open: {
874+
app: ['Google Chrome', '--incognito'],
875+
},
876+
},
877+
{ port: 8080 }
878+
);
879+
expect(config).toMatchSnapshot();
880+
});
881+
868882
it('openPage option', () => {
869883
const config = createConfig(
870884
webpackConfig,

test/server/utils/runOpen.test.js

+96
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ describe('runOpen util', () => {
1717

1818
it('on specify URL', () => {
1919
return runOpen('https://example.com', {}, console).then(() => {
20+
expect(opn).toBeCalledWith('https://example.com', { wait: false });
21+
2022
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
2123
Array [
2224
"https://example.com",
@@ -34,6 +36,10 @@ describe('runOpen util', () => {
3436
{ openPage: '/index.html' },
3537
console
3638
).then(() => {
39+
expect(opn).toBeCalledWith('https://example.com/index.html', {
40+
wait: false,
41+
});
42+
3743
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
3844
Array [
3945
"https://example.com/index.html",
@@ -51,6 +57,10 @@ describe('runOpen util', () => {
5157
{ openPage: ['/index.html'] },
5258
console
5359
).then(() => {
60+
expect(opn).toBeCalledWith('https://example.com/index.html', {
61+
wait: false,
62+
});
63+
5464
expect(opn.mock.calls[0]).toMatchSnapshot();
5565
});
5666
});
@@ -61,6 +71,13 @@ describe('runOpen util', () => {
6171
{ openPage: ['/index.html', '/index2.html'] },
6272
console
6373
).then(() => {
74+
expect(opn).toBeCalledWith('https://example.com/index.html', {
75+
wait: false,
76+
});
77+
expect(opn).toBeCalledWith('https://example.com/index2.html', {
78+
wait: false,
79+
});
80+
6481
expect(opn.mock.calls[0]).toMatchSnapshot();
6582
expect(opn.mock.calls[1]).toMatchSnapshot();
6683
});
@@ -72,6 +89,11 @@ describe('runOpen util', () => {
7289
{ open: 'Google Chrome' },
7390
console
7491
).then(() => {
92+
expect(opn).toBeCalledWith('https://example.com', {
93+
app: 'Google Chrome',
94+
wait: false,
95+
});
96+
7597
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
7698
Array [
7799
"https://example.com",
@@ -90,6 +112,11 @@ describe('runOpen util', () => {
90112
{ open: 'Google Chrome', openPage: '/index.html' },
91113
console
92114
).then(() => {
115+
expect(opn).toBeCalledWith('https://example.com/index.html', {
116+
app: 'Google Chrome',
117+
wait: false,
118+
});
119+
93120
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
94121
Array [
95122
"https://example.com/index.html",
@@ -108,6 +135,11 @@ describe('runOpen util', () => {
108135
{ open: 'Google Chrome', openPage: 'https://example2.com' },
109136
console
110137
).then(() => {
138+
expect(opn).toBeCalledWith('https://example2.com', {
139+
app: 'Google Chrome',
140+
wait: false,
141+
});
142+
111143
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
112144
Array [
113145
"https://example2.com",
@@ -126,6 +158,10 @@ describe('runOpen util', () => {
126158
{ open: 'Google Chrome', openPage: 'http://example2.com' },
127159
console
128160
).then(() => {
161+
expect(opn).toBeCalledWith('http://example2.com', {
162+
app: 'Google Chrome',
163+
wait: false,
164+
});
129165
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
130166
Array [
131167
"http://example2.com",
@@ -148,6 +184,14 @@ describe('runOpen util', () => {
148184
},
149185
console
150186
).then(() => {
187+
expect(opn).toBeCalledWith('https://example2.com', {
188+
app: 'Google Chrome',
189+
wait: false,
190+
});
191+
expect(opn).toBeCalledWith('https://example3.com', {
192+
app: 'Google Chrome',
193+
wait: false,
194+
});
151195
expect(opn.mock.calls[0]).toMatchSnapshot();
152196
expect(opn.mock.calls[1]).toMatchSnapshot();
153197
});
@@ -162,6 +206,15 @@ describe('runOpen util', () => {
162206
},
163207
console
164208
).then(() => {
209+
expect(opn).toBeCalledWith('https://example.com/index.html', {
210+
app: 'Google Chrome',
211+
wait: false,
212+
});
213+
expect(opn).toBeCalledWith('https://example2.com', {
214+
app: 'Google Chrome',
215+
wait: false,
216+
});
217+
165218
expect(opn.mock.calls[0]).toMatchSnapshot();
166219
expect(opn.mock.calls[1]).toMatchSnapshot();
167220
});
@@ -183,6 +236,8 @@ describe('runOpen util', () => {
183236
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
184237
`"Unable to open \\"https://example.com\\" in browser. If you are running in a headless environment, please do not use the --open flag"`
185238
);
239+
expect(opn).toBeCalledWith('https://example.com', { wait: false });
240+
186241
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
187242
Array [
188243
"https://example.com",
@@ -203,6 +258,10 @@ describe('runOpen util', () => {
203258
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
204259
`"Unable to open \\"https://example.com/index.html\\" in browser. If you are running in a headless environment, please do not use the --open flag"`
205260
);
261+
expect(opn).toBeCalledWith('https://example.com/index.html', {
262+
wait: false,
263+
});
264+
206265
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
207266
Array [
208267
"https://example.com/index.html",
@@ -223,6 +282,11 @@ describe('runOpen util', () => {
223282
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
224283
`"Unable to open \\"https://example.com\\" in browser: \\"Google Chrome\\". If you are running in a headless environment, please do not use the --open flag"`
225284
);
285+
expect(opn).toBeCalledWith('https://example.com', {
286+
app: 'Google Chrome',
287+
wait: false,
288+
});
289+
226290
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
227291
Array [
228292
"https://example.com",
@@ -244,6 +308,11 @@ describe('runOpen util', () => {
244308
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
245309
`"Unable to open \\"https://example.com/index.html\\" in browser: \\"Google Chrome\\". If you are running in a headless environment, please do not use the --open flag"`
246310
);
311+
expect(opn).toBeCalledWith('https://example.com/index.html', {
312+
app: 'Google Chrome',
313+
wait: false,
314+
});
315+
247316
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
248317
Array [
249318
"https://example.com/index.html",
@@ -255,5 +324,32 @@ describe('runOpen util', () => {
255324
`);
256325
});
257326
});
327+
328+
it('on specify URL with page in Google Chrome incognito mode and log error ', () => {
329+
return runOpen(
330+
'https://example.com',
331+
{
332+
open: { app: ['Google Chrome', '--incognito'] },
333+
openPage: '/index.html',
334+
},
335+
logMock
336+
).then(() => {
337+
expect(opn).toBeCalledWith('https://example.com/index.html', {
338+
app: ['Google Chrome', '--incognito'],
339+
});
340+
341+
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
342+
Array [
343+
"https://example.com/index.html",
344+
Object {
345+
"app": Array [
346+
"Google Chrome",
347+
"--incognito",
348+
],
349+
},
350+
]
351+
`);
352+
});
353+
});
258354
});
259355
});

0 commit comments

Comments
 (0)