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

feat: allow open option to accept an object #2492

Merged
merged 9 commits into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@
},
{
"type": "boolean"
},
{
"type": "object"
}
]
},
Expand Down Expand Up @@ -460,7 +463,7 @@
"mimeTypes": "should be {Object} (https://webpack.js.org/configuration/dev-server/#devservermimetypes-)",
"noInfo": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devservernoinfo-)",
"onListening": "should be {Function} (https://webpack.js.org/configuration/dev-server/#onlistening)",
"open": "should be {String|Boolean} (https://webpack.js.org/configuration/dev-server/#devserveropen)",
"open": "should be {String|Boolean|Object} (https://webpack.js.org/configuration/dev-server/#devserveropen)",
"openPage": "should be {String|Array} (https://webpack.js.org/configuration/dev-server/#devserveropenpage)",
"overlay": "should be {Boolean|Object} (https://webpack.js.org/configuration/dev-server/#devserveroverlay)",
"pfx": "should be {String|Buffer} (https://webpack.js.org/configuration/dev-server/#devserverpfx)",
Expand Down
3 changes: 3 additions & 0 deletions lib/utils/runOpen.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ function runOpen(uri, options, log) {
if (typeof options.open === 'string') {
openOptions = Object.assign({}, openOptions, { app: options.open });
openOptionValue = `: "${options.open}"`;
} else if (typeof options.open === 'object') {
openOptions = options.open;
openOptionValue = `: "${JSON.stringify(options.open)}"`;
}

const pages =
Expand Down
4 changes: 2 additions & 2 deletions test/options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ describe('options', () => {
failure: [''],
},
open: {
success: [true, ''],
failure: [{}],
success: [true, '', {}],
failure: [[]],
},
openPage: {
success: [''],
Expand Down
23 changes: 22 additions & 1 deletion test/server/utils/__snapshots__/createConfig.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,28 @@ Object {
}
`;

exports[`createConfig open option (browser) 1`] = `
exports[`createConfig open option (object) 1`] = `
Object {
"hot": true,
"hotOnly": false,
"noInfo": true,
"open": Object {
"app": Array [
"Google Chrome",
"--incognito",
],
},
"openPage": "",
"port": 8080,
"publicPath": "/",
"stats": Object {
"cached": false,
"cachedAssets": false,
},
}
`;

exports[`createConfig open option (string) 1`] = `
Object {
"hot": true,
"hotOnly": false,
Expand Down
16 changes: 15 additions & 1 deletion test/server/utils/createConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ describe('createConfig', () => {
expect(config).toMatchSnapshot();
});

it('open option (browser)', () => {
it('open option (string)', () => {
const config = createConfig(
webpackConfig,
Object.assign({}, argv, { open: 'Google Chrome' }),
Expand All @@ -865,6 +865,20 @@ describe('createConfig', () => {
expect(config).toMatchSnapshot();
});

it('open option (object)', () => {
const config = createConfig(
webpackConfig,
{
...argv,
open: {
app: ['Google Chrome', '--incognito'],
},
},
{ port: 8080 }
);
expect(config).toMatchSnapshot();
});

it('openPage option', () => {
const config = createConfig(
webpackConfig,
Expand Down
96 changes: 96 additions & 0 deletions test/server/utils/runOpen.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ describe('runOpen util', () => {

it('on specify URL', () => {
return runOpen('https://example.com', {}, console).then(() => {
expect(opn).toBeCalledWith('https://example.com', { wait: false });

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

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

expect(opn.mock.calls[0]).toMatchSnapshot();
});
});
Expand All @@ -61,6 +71,13 @@ describe('runOpen util', () => {
{ openPage: ['/index.html', '/index2.html'] },
console
).then(() => {
expect(opn).toBeCalledWith('https://example.com/index.html', {
wait: false,
});
expect(opn).toBeCalledWith('https://example.com/index2.html', {
wait: false,
});

expect(opn.mock.calls[0]).toMatchSnapshot();
expect(opn.mock.calls[1]).toMatchSnapshot();
});
Expand All @@ -72,6 +89,11 @@ describe('runOpen util', () => {
{ open: 'Google Chrome' },
console
).then(() => {
expect(opn).toBeCalledWith('https://example.com', {
app: 'Google Chrome',
wait: false,
});

expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"https://example.com",
Expand All @@ -90,6 +112,11 @@ describe('runOpen util', () => {
{ open: 'Google Chrome', openPage: '/index.html' },
console
).then(() => {
expect(opn).toBeCalledWith('https://example.com/index.html', {
app: 'Google Chrome',
wait: false,
});

expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"https://example.com/index.html",
Expand All @@ -108,6 +135,11 @@ describe('runOpen util', () => {
{ open: 'Google Chrome', openPage: 'https://example2.com' },
console
).then(() => {
expect(opn).toBeCalledWith('https://example2.com', {
app: 'Google Chrome',
wait: false,
});

expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"https://example2.com",
Expand All @@ -126,6 +158,10 @@ describe('runOpen util', () => {
{ open: 'Google Chrome', openPage: 'http://example2.com' },
console
).then(() => {
expect(opn).toBeCalledWith('http://example2.com', {
app: 'Google Chrome',
wait: false,
});
expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"http://example2.com",
Expand All @@ -148,6 +184,14 @@ describe('runOpen util', () => {
},
console
).then(() => {
expect(opn).toBeCalledWith('https://example2.com', {
app: 'Google Chrome',
wait: false,
});
expect(opn).toBeCalledWith('https://example3.com', {
app: 'Google Chrome',
wait: false,
});
expect(opn.mock.calls[0]).toMatchSnapshot();
expect(opn.mock.calls[1]).toMatchSnapshot();
});
Expand All @@ -162,6 +206,15 @@ describe('runOpen util', () => {
},
console
).then(() => {
expect(opn).toBeCalledWith('https://example.com/index.html', {
app: 'Google Chrome',
wait: false,
});
expect(opn).toBeCalledWith('https://example2.com', {
app: 'Google Chrome',
wait: false,
});

expect(opn.mock.calls[0]).toMatchSnapshot();
expect(opn.mock.calls[1]).toMatchSnapshot();
});
Expand All @@ -183,6 +236,8 @@ describe('runOpen util', () => {
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
`"Unable to open \\"https://example.com\\" in browser. If you are running in a headless environment, please do not use the --open flag"`
);
expect(opn).toBeCalledWith('https://example.com', { wait: false });

expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"https://example.com",
Expand All @@ -203,6 +258,10 @@ describe('runOpen util', () => {
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
`"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"`
);
expect(opn).toBeCalledWith('https://example.com/index.html', {
wait: false,
});

Copy link
Member

@alexander-akait alexander-akait Apr 1, 2020

Choose a reason for hiding this comment

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

Let's return warn check (logMock.warn)

expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"https://example.com/index.html",
Expand All @@ -223,6 +282,11 @@ describe('runOpen util', () => {
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
`"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"`
);
expect(opn).toBeCalledWith('https://example.com', {
app: 'Google Chrome',
wait: false,
});

expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"https://example.com",
Expand All @@ -244,6 +308,11 @@ describe('runOpen util', () => {
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
`"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"`
);
expect(opn).toBeCalledWith('https://example.com/index.html', {
app: 'Google Chrome',
wait: false,
});

expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"https://example.com/index.html",
Expand All @@ -255,5 +324,32 @@ describe('runOpen util', () => {
`);
});
});

it('on specify URL with page in Google Chrome incognito mode and log error ', () => {
return runOpen(
'https://example.com',
{
open: { app: ['Google Chrome', '--incognito'] },
openPage: '/index.html',
},
logMock
).then(() => {
expect(opn).toBeCalledWith('https://example.com/index.html', {
app: ['Google Chrome', '--incognito'],
});

expect(opn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"https://example.com/index.html",
Object {
"app": Array [
"Google Chrome",
"--incognito",
],
},
]
`);
});
});
});
});