-
-
Notifications
You must be signed in to change notification settings - Fork 624
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(serve): merge CLI and devServer options correctly (#1649)
- Loading branch information
1 parent
b1e7024
commit 2cdf5ce
Showing
18 changed files
with
856 additions
and
111 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
packages/serve/__tests__/__snapshots__/parseArgs.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`parseArgs handles hot arg 1`] = ` | ||
Object { | ||
"devServerArgs": Object { | ||
"clientLogLevel": "info", | ||
"hot": true, | ||
"inline": true, | ||
"liveReload": true, | ||
"serveIndex": true, | ||
}, | ||
"webpackArgs": Object { | ||
"color": true, | ||
"config": Array [], | ||
"hot": true, | ||
"mode": "production", | ||
}, | ||
} | ||
`; | ||
|
||
exports[`parseArgs handles unknown args 1`] = ` | ||
Array [ | ||
Array [ | ||
"Unknown argument: --unknown-arg", | ||
], | ||
Array [ | ||
"Unknown argument: --unknown-arg-2", | ||
], | ||
] | ||
`; | ||
|
||
exports[`parseArgs parses webpack and dev server args 1`] = ` | ||
Object { | ||
"devServerArgs": Object { | ||
"bonjour": true, | ||
"clientLogLevel": "info", | ||
"inline": true, | ||
"liveReload": true, | ||
"port": 8080, | ||
"serveIndex": true, | ||
}, | ||
"webpackArgs": Object { | ||
"color": true, | ||
"config": Array [], | ||
"mode": "production", | ||
"target": "node", | ||
}, | ||
} | ||
`; |
84 changes: 84 additions & 0 deletions
84
packages/serve/__tests__/__snapshots__/startDevServer.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`startDevServer should set default port and host if not provided 1`] = ` | ||
Object { | ||
"host": "localhost", | ||
"port": 8080, | ||
} | ||
`; | ||
|
||
exports[`startDevServer should set default port and host if not provided 2`] = ` | ||
Array [ | ||
8080, | ||
"localhost", | ||
[Function], | ||
] | ||
`; | ||
|
||
exports[`startDevServer should start dev server correctly for multi compiler with 1 devServer config 1`] = ` | ||
Object { | ||
"bonjour": true, | ||
"host": "my.host", | ||
"hot": true, | ||
"port": 9000, | ||
"progress": true, | ||
} | ||
`; | ||
|
||
exports[`startDevServer should start dev server correctly for multi compiler with 1 devServer config 2`] = ` | ||
Array [ | ||
9000, | ||
"my.host", | ||
[Function], | ||
] | ||
`; | ||
|
||
exports[`startDevServer should start dev server correctly for single compiler 1`] = ` | ||
Object { | ||
"bonjour": true, | ||
"host": "my.host", | ||
"hot": true, | ||
"port": 9000, | ||
"progress": true, | ||
} | ||
`; | ||
|
||
exports[`startDevServer should start dev server correctly for single compiler 2`] = ` | ||
Array [ | ||
9000, | ||
"my.host", | ||
[Function], | ||
] | ||
`; | ||
|
||
exports[`startDevServer should start dev servers correctly for multi compiler with 2 devServer configs 1`] = ` | ||
Object { | ||
"host": "localhost", | ||
"port": 9000, | ||
"progress": true, | ||
} | ||
`; | ||
|
||
exports[`startDevServer should start dev servers correctly for multi compiler with 2 devServer configs 2`] = ` | ||
Object { | ||
"host": "localhost", | ||
"port": 9001, | ||
"progress": true, | ||
} | ||
`; | ||
|
||
exports[`startDevServer should start dev servers correctly for multi compiler with 2 devServer configs 3`] = ` | ||
Array [ | ||
9000, | ||
"localhost", | ||
[Function], | ||
] | ||
`; | ||
|
||
exports[`startDevServer should start dev servers correctly for multi compiler with 2 devServer configs 4`] = ` | ||
Array [ | ||
9001, | ||
"localhost", | ||
[Function], | ||
] | ||
`; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict'; | ||
|
||
import createConfig from '../src/createConfig'; | ||
|
||
describe('createConfig', () => { | ||
it('creates config with arguments', () => { | ||
const args = { | ||
hot: true, | ||
openPage: 'main', | ||
}; | ||
expect(createConfig(args)).toEqual(args); | ||
}); | ||
|
||
it('sets client object using clientLogging argument', () => { | ||
const args = { | ||
clientLogging: 'verbose', | ||
}; | ||
expect(createConfig(args)).toEqual({ | ||
client: { | ||
logging: 'verbose', | ||
}, | ||
}); | ||
}); | ||
|
||
it('sets hot using hotOnly argument', () => { | ||
const args = { | ||
hotOnly: true, | ||
}; | ||
expect(createConfig(args)).toEqual({ | ||
hot: 'only', | ||
}); | ||
}); | ||
|
||
it('overrides hot with hotOnly', () => { | ||
const args = { | ||
hot: true, | ||
hotOnly: true, | ||
}; | ||
expect(createConfig(args)).toEqual({ | ||
hot: 'only', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
|
||
import getDevServerOptions from '../src/getDevServerOptions'; | ||
|
||
describe('getDevServerOptions', () => { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires, node/no-extraneous-require | ||
const webpack = require('webpack'); | ||
|
||
it('gets dev server options from single compiler', () => { | ||
const compiler = webpack({ | ||
devServer: { | ||
hot: true, | ||
host: 'my.host', | ||
}, | ||
}); | ||
expect(getDevServerOptions(compiler)).toEqual([ | ||
{ | ||
hot: true, | ||
host: 'my.host', | ||
}, | ||
]); | ||
}); | ||
|
||
it('gets dev server options from multi compiler', () => { | ||
const compiler = webpack([ | ||
{ | ||
devServer: { | ||
hot: true, | ||
host: 'my.host', | ||
}, | ||
}, | ||
{ | ||
devServer: { | ||
hot: false, | ||
host: 'other.host', | ||
}, | ||
}, | ||
]); | ||
|
||
expect(getDevServerOptions(compiler)).toEqual([ | ||
{ | ||
hot: true, | ||
host: 'my.host', | ||
}, | ||
{ | ||
hot: false, | ||
host: 'other.host', | ||
}, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
import mergeOptions from '../src/mergeOptions'; | ||
|
||
describe('mergeOptions', () => { | ||
it('merges CLI and devServer options correctly', () => { | ||
const cliOptions = { | ||
client: { | ||
logging: 'verbose', | ||
}, | ||
hot: true, | ||
bonjour: true, | ||
}; | ||
const devServerOptions = { | ||
client: { | ||
host: 'localhost', | ||
logging: 'none', | ||
}, | ||
hot: false, | ||
liveReload: false, | ||
}; | ||
// CLI should take priority | ||
expect(mergeOptions(cliOptions, devServerOptions)).toEqual({ | ||
client: { | ||
host: 'localhost', | ||
logging: 'verbose', | ||
}, | ||
hot: true, | ||
bonjour: true, | ||
liveReload: false, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict'; | ||
|
||
// TODO: update snapshots once we update to webpack-dev-server@4 | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const errorMock: any = jest.fn(); | ||
jest.mock('webpack-cli/lib/utils/logger', () => { | ||
return { | ||
error: errorMock, | ||
}; | ||
}); | ||
|
||
import WebpackCLI from 'webpack-cli'; | ||
import parseArgs from '../src/parseArgs'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const processExitSpy: any = jest.spyOn(process, 'exit'); | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
processExitSpy.mockImplementation(() => {}); | ||
|
||
describe('parseArgs', () => { | ||
const cli = new WebpackCLI(); | ||
|
||
beforeEach(() => { | ||
errorMock.mockClear(); | ||
processExitSpy.mockClear(); | ||
}); | ||
|
||
it('parses webpack and dev server args', () => { | ||
const args = parseArgs(cli, ['--bonjour', '--target=node', '--port', '8080']); | ||
expect(args).toMatchSnapshot(); | ||
expect(errorMock.mock.calls.length).toEqual(0); | ||
expect(processExitSpy.mock.calls.length).toEqual(0); | ||
}); | ||
|
||
it('handles hot arg', () => { | ||
const args = parseArgs(cli, ['--hot']); | ||
expect(args).toMatchSnapshot(); | ||
expect(errorMock.mock.calls.length).toEqual(0); | ||
expect(processExitSpy.mock.calls.length).toEqual(0); | ||
}); | ||
|
||
it('handles unknown args', () => { | ||
parseArgs(cli, ['--unknown-arg', '--unknown-arg-2']); | ||
expect(errorMock.mock.calls).toMatchSnapshot(); | ||
expect(processExitSpy.mock.calls.length).toEqual(1); | ||
expect(processExitSpy.mock.calls[0]).toEqual([2]); | ||
}); | ||
}); |
Oops, something went wrong.