-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat(open): modify devServer.options.open
to take an object
#1770
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,18 +2,29 @@ | |
|
||
const open = require('opn'); | ||
|
||
function handleInvalidOptionsConfig(log, optionsConfig) { | ||
log.warn('You have passed in an invalid options config'); | ||
log.info('For more on open see https://github.com/sindresorhus/open'); | ||
log.info('You passed in ', optionsConfig); | ||
} | ||
|
||
function runOpen(uri, options, log) { | ||
let openOptions = {}; | ||
let openMessage = 'Unable to open browser'; | ||
|
||
if (typeof options.open === 'string') { | ||
openOptions = { app: options.open }; | ||
openMessage += `: ${options.open}`; | ||
} else if (typeof options.open === 'object') { | ||
openOptions = options.open; | ||
if (!options.app && !options.wait) { | ||
handleInvalidOptionsConfig(log, options.open); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why we need this? open package can add more option in future |
||
} else { | ||
handleInvalidOptionsConfig(log, options.open); | ||
} | ||
|
||
open(`${uri}${options.openPage || ''}`, openOptions).catch(() => { | ||
log.warn( | ||
`${openMessage}. If you are running in a headless environment, please do not use the --open flag` | ||
'Unable to open browser. If you are running in a headless environment, please do not use the --open flag' | ||
); | ||
}); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict'; | ||
|
||
jest.mock('opn'); | ||
const open = require('opn'); | ||
const status = require('../lib/utils/status'); | ||
|
||
open.mockReturnValue('foo'); | ||
const catchMock = jest.fn(() => 'foo'); | ||
open.mockReturnValue({ | ||
catch: catchMock, | ||
}); | ||
|
||
const uri = 'http://127.0.0.1'; | ||
const log = { | ||
info: jest.fn(), | ||
warn: jest.fn(), | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to create const log = {
info: () => {}
warn: () => {}
}
const infoSpy = jest.spyOn(log, 'info')
const warnSpy = jest.spyOn(log, 'warn') There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ??? |
||
|
||
describe('status)', () => { | ||
beforeEach(() => { | ||
open.mockClear(); | ||
catchMock.mockClear(); | ||
log.info.mockClear(); | ||
log.warn.mockClear(); | ||
}); | ||
describe('open', () => { | ||
it('should NOT call open if `open` is not passed in', () => { | ||
const options = {}; | ||
status(uri, options, log, true); | ||
expect(open).not.toBeCalled(); | ||
}); | ||
it('should call open with the URI passed in', () => { | ||
const options = { | ||
open: true, | ||
}; | ||
status(uri, options, log, true); | ||
expect(open).toBeCalledWith(uri, {}); | ||
}); | ||
it('should call open with no options if open:true', () => { | ||
const options = { | ||
open: true, | ||
}; | ||
status(uri, options, log, true); | ||
expect(open).toBeCalledWith(uri, {}); | ||
}); | ||
it('should call open with app:"app_name" is open is a string ', () => { | ||
const options = { | ||
open: 'Google Chrome', | ||
}; | ||
status(uri, options, log, true); | ||
expect(open).toBeCalledWith(uri, { app: 'Google Chrome' }); | ||
}); | ||
it('should call open with app:["app_name", "option"] is open is an array ', () => { | ||
const options = { | ||
open: { app: ['Google Chrome', 'incognito'] }, | ||
}; | ||
status(uri, options, log, true); | ||
expect(open).toBeCalledWith(uri, { app: ['Google Chrome', 'incognito'] }); | ||
}); | ||
}); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move this tests to https://github.com/webpack/webpack-dev-server/blob/master/test/server/open-option.test.js |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add
Array.isArray
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#1770 (review)