Skip to content

Commit 2b4cb52

Browse files
authored
fix(open): set wait: false to run server.close successfully (#2001)
fixes: #1990
1 parent 90d4a7c commit 2b4cb52

File tree

3 files changed

+83
-33
lines changed

3 files changed

+83
-33
lines changed

lib/utils/runOpen.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
const open = require('opn');
44

55
function runOpen(uri, options, log) {
6-
let openOptions = {};
6+
// https://github.com/webpack/webpack-dev-server/issues/1990
7+
let openOptions = { wait: false };
78
let openMessage = 'Unable to open browser';
89

910
if (typeof options.open === 'string') {
10-
openOptions = { app: options.open };
11+
openOptions = Object.assign({}, openOptions, { app: options.open });
1112
openMessage += `: ${options.open}`;
1213
}
1314

test/server/open-option.test.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,18 @@ describe('open option', () => {
2121
});
2222

2323
compiler.hooks.done.tap('webpack-dev-server', () => {
24-
expect(opn.mock.calls[0]).toEqual(['http://localhost:8080/', {}]);
25-
expect(opn.mock.invocationCallOrder[0]).toEqual(1);
26-
server.close(done);
24+
server.close(() => {
25+
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
26+
Array [
27+
"http://localhost:8080/",
28+
Object {
29+
"wait": false,
30+
},
31+
]
32+
`);
33+
expect(opn.mock.invocationCallOrder[0]).toEqual(1);
34+
done();
35+
});
2736
});
2837

2938
compiler.run(() => {});

test/server/utils/runOpen.test.js

+68-28
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ describe('runOpen util', () => {
1717

1818
it('on specify URL', () => {
1919
return runOpen('https://example.com', {}, console).then(() => {
20-
expect(opn.mock.calls[0]).toEqual(['https://example.com', {}]);
20+
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
21+
Array [
22+
"https://example.com",
23+
Object {
24+
"wait": false,
25+
},
26+
]
27+
`);
2128
});
2229
});
2330

@@ -27,10 +34,14 @@ describe('runOpen util', () => {
2734
{ openPage: '/index.html' },
2835
console
2936
).then(() => {
30-
expect(opn.mock.calls[0]).toEqual([
31-
'https://example.com/index.html',
32-
{},
33-
]);
37+
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
38+
Array [
39+
"https://example.com/index.html",
40+
Object {
41+
"wait": false,
42+
},
43+
]
44+
`);
3445
});
3546
});
3647

@@ -40,10 +51,15 @@ describe('runOpen util', () => {
4051
{ open: 'Google Chrome' },
4152
console
4253
).then(() => {
43-
expect(opn.mock.calls[0]).toEqual([
44-
'https://example.com',
45-
{ app: 'Google Chrome' },
46-
]);
54+
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
55+
Array [
56+
"https://example.com",
57+
Object {
58+
"app": "Google Chrome",
59+
"wait": false,
60+
},
61+
]
62+
`);
4763
});
4864
});
4965

@@ -53,10 +69,15 @@ describe('runOpen util', () => {
5369
{ open: 'Google Chrome', openPage: '/index.html' },
5470
console
5571
).then(() => {
56-
expect(opn.mock.calls[0]).toEqual([
57-
'https://example.com/index.html',
58-
{ app: 'Google Chrome' },
59-
]);
72+
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
73+
Array [
74+
"https://example.com/index.html",
75+
Object {
76+
"app": "Google Chrome",
77+
"wait": false,
78+
},
79+
]
80+
`);
6081
});
6182
});
6283
});
@@ -77,7 +98,14 @@ describe('runOpen util', () => {
7798
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
7899
`"Unable to open browser. If you are running in a headless environment, please do not use the --open flag"`
79100
);
80-
expect(opn.mock.calls[0]).toEqual(['https://example.com', {}]);
101+
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
102+
Array [
103+
"https://example.com",
104+
Object {
105+
"wait": false,
106+
},
107+
]
108+
`);
81109
});
82110
});
83111

@@ -90,10 +118,14 @@ describe('runOpen util', () => {
90118
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
91119
`"Unable to open browser. If you are running in a headless environment, please do not use the --open flag"`
92120
);
93-
expect(opn.mock.calls[0]).toEqual([
94-
'https://example.com/index.html',
95-
{},
96-
]);
121+
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
122+
Array [
123+
"https://example.com/index.html",
124+
Object {
125+
"wait": false,
126+
},
127+
]
128+
`);
97129
});
98130
});
99131

@@ -106,12 +138,15 @@ describe('runOpen util', () => {
106138
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
107139
`"Unable to open browser: Google Chrome. If you are running in a headless environment, please do not use the --open flag"`
108140
);
109-
expect(opn.mock.calls[0]).toEqual([
110-
'https://example.com',
111-
{
112-
app: 'Google Chrome',
113-
},
114-
]);
141+
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
142+
Array [
143+
"https://example.com",
144+
Object {
145+
"app": "Google Chrome",
146+
"wait": false,
147+
},
148+
]
149+
`);
115150
});
116151
});
117152

@@ -124,10 +159,15 @@ describe('runOpen util', () => {
124159
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
125160
`"Unable to open browser: Google Chrome. If you are running in a headless environment, please do not use the --open flag"`
126161
);
127-
expect(opn.mock.calls[0]).toEqual([
128-
'https://example.com/index.html',
129-
{ app: 'Google Chrome' },
130-
]);
162+
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
163+
Array [
164+
"https://example.com/index.html",
165+
Object {
166+
"app": "Google Chrome",
167+
"wait": false,
168+
},
169+
]
170+
`);
131171
});
132172
});
133173
});

0 commit comments

Comments
 (0)