Skip to content

Commit

Permalink
feat: allow open option to accept an object (#2492)
Browse files Browse the repository at this point in the history
* feat: allow open option to accept object

* test: update snapshot

* test: spread argv

* test: remove error checks & use toBeCalledWith instead

* test: return logMock error checks

* test: remove unnecessary lines

* test: update success & failure types for open option
  • Loading branch information
EslamHiko committed Apr 2, 2020
1 parent c6bdfe4 commit adeb92e
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/options.json
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
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
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
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
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
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,
});

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",
],
},
]
`);
});
});
});
});

0 comments on commit adeb92e

Please sign in to comment.