Skip to content

Commit

Permalink
fix(open): set wait: false to run server.close successfully (#2001)
Browse files Browse the repository at this point in the history
fixes: #1990
  • Loading branch information
hiroppy authored Jun 10, 2019
1 parent 90d4a7c commit 2b4cb52
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 33 deletions.
5 changes: 3 additions & 2 deletions lib/utils/runOpen.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
const open = require('opn');

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

if (typeof options.open === 'string') {
openOptions = { app: options.open };
openOptions = Object.assign({}, openOptions, { app: options.open });
openMessage += `: ${options.open}`;
}

Expand Down
15 changes: 12 additions & 3 deletions test/server/open-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,18 @@ describe('open option', () => {
});

compiler.hooks.done.tap('webpack-dev-server', () => {
expect(opn.mock.calls[0]).toEqual(['http://localhost:8080/', {}]);
expect(opn.mock.invocationCallOrder[0]).toEqual(1);
server.close(done);
server.close(() => {
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"http://localhost:8080/",
Object {
"wait": false,
},
]
`);
expect(opn.mock.invocationCallOrder[0]).toEqual(1);
done();
});
});

compiler.run(() => {});
Expand Down
96 changes: 68 additions & 28 deletions test/server/utils/runOpen.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ describe('runOpen util', () => {

it('on specify URL', () => {
return runOpen('https://example.com', {}, console).then(() => {
expect(opn.mock.calls[0]).toEqual(['https://example.com', {}]);
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"https://example.com",
Object {
"wait": false,
},
]
`);
});
});

Expand All @@ -27,10 +34,14 @@ describe('runOpen util', () => {
{ openPage: '/index.html' },
console
).then(() => {
expect(opn.mock.calls[0]).toEqual([
'https://example.com/index.html',
{},
]);
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"https://example.com/index.html",
Object {
"wait": false,
},
]
`);
});
});

Expand All @@ -40,10 +51,15 @@ describe('runOpen util', () => {
{ open: 'Google Chrome' },
console
).then(() => {
expect(opn.mock.calls[0]).toEqual([
'https://example.com',
{ app: 'Google Chrome' },
]);
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"https://example.com",
Object {
"app": "Google Chrome",
"wait": false,
},
]
`);
});
});

Expand All @@ -53,10 +69,15 @@ describe('runOpen util', () => {
{ open: 'Google Chrome', openPage: '/index.html' },
console
).then(() => {
expect(opn.mock.calls[0]).toEqual([
'https://example.com/index.html',
{ app: 'Google Chrome' },
]);
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"https://example.com/index.html",
Object {
"app": "Google Chrome",
"wait": false,
},
]
`);
});
});
});
Expand All @@ -77,7 +98,14 @@ describe('runOpen util', () => {
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
`"Unable to open browser. If you are running in a headless environment, please do not use the --open flag"`
);
expect(opn.mock.calls[0]).toEqual(['https://example.com', {}]);
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"https://example.com",
Object {
"wait": false,
},
]
`);
});
});

Expand All @@ -90,10 +118,14 @@ describe('runOpen util', () => {
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
`"Unable to open browser. If you are running in a headless environment, please do not use the --open flag"`
);
expect(opn.mock.calls[0]).toEqual([
'https://example.com/index.html',
{},
]);
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"https://example.com/index.html",
Object {
"wait": false,
},
]
`);
});
});

Expand All @@ -106,12 +138,15 @@ describe('runOpen util', () => {
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
`"Unable to open browser: Google Chrome. If you are running in a headless environment, please do not use the --open flag"`
);
expect(opn.mock.calls[0]).toEqual([
'https://example.com',
{
app: 'Google Chrome',
},
]);
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"https://example.com",
Object {
"app": "Google Chrome",
"wait": false,
},
]
`);
});
});

Expand All @@ -124,10 +159,15 @@ describe('runOpen util', () => {
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
`"Unable to open browser: Google Chrome. If you are running in a headless environment, please do not use the --open flag"`
);
expect(opn.mock.calls[0]).toEqual([
'https://example.com/index.html',
{ app: 'Google Chrome' },
]);
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"https://example.com/index.html",
Object {
"app": "Google Chrome",
"wait": false,
},
]
`);
});
});
});
Expand Down

0 comments on commit 2b4cb52

Please sign in to comment.