Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(open): set wait: false to run server.close successfully #2001

Merged
merged 4 commits into from Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/utils/runOpen.js
Expand Up @@ -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,
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let openOptions = { wait: false };
// ...
if (typeof options.open === 'string') {
  openOptions = Object.assign({} openOptions, { app: options.open });
}

No need modify openOptions variable, it is very bad practice

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

openOptions = Object.assign({} openOptions, { app: options.open }); equals Object.assign(openOptions, { app: options.open }).

I don't think this is a very bad practice.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but it is not good, we modify original object and it is bad practice because you can forget about this in future and it is lead to some problems when you refactor code.

When you pass options to third party package(s) better always cloning object. Other package can modify object too so it can lead to very strange errors and situations.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you pass options to third party package(s) better always cloning object. Other package can modify object too so it can lead to very strange errors and situations.

If you say, we should not assign to the same variable.

I don't have any strong opinions, so I'll change.

openMessage += `: ${options.open}`;
}

Expand Down
96 changes: 68 additions & 28 deletions test/server/utils/runOpen.test.js
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