From bf696bdc6c6ed2f88bff6470c8c9c4b1bcfd1c3b Mon Sep 17 00:00:00 2001 From: Yuta Hiroto Date: Sat, 8 Jun 2019 15:12:40 +0200 Subject: [PATCH 1/2] fix(open): set `wait: false` to run server.close successfully fixes: https://github.com/webpack/webpack-dev-server/issues/1990 --- lib/utils/runOpen.js | 7 ++- test/server/utils/runOpen.test.js | 96 ++++++++++++++++++++++--------- 2 files changed, 73 insertions(+), 30 deletions(-) diff --git a/lib/utils/runOpen.js b/lib/utils/runOpen.js index b499888356..bd34574ede 100644 --- a/lib/utils/runOpen.js +++ b/lib/utils/runOpen.js @@ -3,11 +3,14 @@ const open = require('opn'); function runOpen(uri, options, log) { - let openOptions = {}; + // https://github.com/webpack/webpack-dev-server/issues/1990 + const openOptions = { wait: false }; let openMessage = 'Unable to open browser'; if (typeof options.open === 'string') { - openOptions = { app: options.open }; + Object.assign(openOptions, { + app: options.open, + }); openMessage += `: ${options.open}`; } diff --git a/test/server/utils/runOpen.test.js b/test/server/utils/runOpen.test.js index 51fce41300..fcb4b42225 100644 --- a/test/server/utils/runOpen.test.js +++ b/test/server/utils/runOpen.test.js @@ -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, + }, + ] + `); }); }); @@ -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, + }, + ] + `); }); }); @@ -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, + }, + ] + `); }); }); @@ -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, + }, + ] + `); }); }); }); @@ -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, + }, + ] + `); }); }); @@ -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, + }, + ] + `); }); }); @@ -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, + }, + ] + `); }); }); @@ -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, + }, + ] + `); }); }); }); From abeaa9ce44ca47454afb4d800678eb2e2ece2d42 Mon Sep 17 00:00:00 2001 From: Yuta Hiroto Date: Sun, 9 Jun 2019 08:42:17 +0200 Subject: [PATCH 2/2] test(open-option): fix test --- lib/utils/runOpen.js | 6 ++---- test/server/open-option.test.js | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/utils/runOpen.js b/lib/utils/runOpen.js index bd34574ede..cf4de34279 100644 --- a/lib/utils/runOpen.js +++ b/lib/utils/runOpen.js @@ -4,13 +4,11 @@ const open = require('opn'); function runOpen(uri, options, log) { // https://github.com/webpack/webpack-dev-server/issues/1990 - const openOptions = { wait: false }; + let openOptions = { wait: false }; let openMessage = 'Unable to open browser'; if (typeof options.open === 'string') { - Object.assign(openOptions, { - app: options.open, - }); + openOptions = Object.assign({}, openOptions, { app: options.open }); openMessage += `: ${options.open}`; } diff --git a/test/server/open-option.test.js b/test/server/open-option.test.js index c0f1768b99..109798e476 100644 --- a/test/server/open-option.test.js +++ b/test/server/open-option.test.js @@ -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(() => {});