diff --git a/globalSetupTest.js b/globalSetupTest.js new file mode 100644 index 0000000000..1e4123b5fa --- /dev/null +++ b/globalSetupTest.js @@ -0,0 +1,31 @@ +'use strict'; + +// eslint-disable-next-line import/no-extraneous-dependencies +const tcpPortUsed = require('tcp-port-used'); +const ports = require('./test/ports-map'); + +async function validatePorts() { + const samples = []; + + Object.entries(ports).forEach(([key, value]) => { + const arr = Array.isArray(value) ? value : [value]; + + arr.forEach((port) => { + const check = tcpPortUsed.check(port, 'localhost').then((inUse) => { + if (inUse) throw new Error(`${port} has already used. [${key}]`); + }); + + samples.push(check); + }); + }); + + try { + await Promise.all(samples); + } catch (e) { + // eslint-disable-next-line no-console + console.error(e); + process.exit(1); + } +} + +module.exports = validatePorts; diff --git a/jest.config.js b/jest.config.js index 322de40ff2..47f4bfd4e7 100644 --- a/jest.config.js +++ b/jest.config.js @@ -7,4 +7,5 @@ module.exports = { moduleFileExtensions: ['js', 'json'], testMatch: ['**/test/**/*.test.js'], setupFilesAfterEnv: ['/setupTest.js'], + globalSetup: '/globalSetupTest.js', }; diff --git a/package-lock.json b/package-lock.json index 00a5b64e9e..3d8dd0183d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5416,8 +5416,7 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "aproba": { "version": "1.2.0", @@ -5438,14 +5437,12 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, - "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -5460,20 +5457,17 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "core-util-is": { "version": "1.0.2", @@ -5590,8 +5584,7 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "ini": { "version": "1.3.5", @@ -5603,7 +5596,6 @@ "version": "1.0.0", "bundled": true, "dev": true, - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -5618,7 +5610,6 @@ "version": "3.0.4", "bundled": true, "dev": true, - "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -5626,14 +5617,12 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, - "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -5652,7 +5641,6 @@ "version": "0.5.1", "bundled": true, "dev": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -5733,8 +5721,7 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "object-assign": { "version": "4.1.1", @@ -5746,7 +5733,6 @@ "version": "1.4.0", "bundled": true, "dev": true, - "optional": true, "requires": { "wrappy": "1" } @@ -5832,8 +5818,7 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "safer-buffer": { "version": "2.1.2", @@ -5869,7 +5854,6 @@ "version": "1.0.2", "bundled": true, "dev": true, - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -5889,7 +5873,6 @@ "version": "3.0.1", "bundled": true, "dev": true, - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -5933,14 +5916,12 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true } } }, @@ -7304,6 +7285,12 @@ "integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo=", "dev": true }, + "is-url": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/is-url/-/is-url-1.2.4.tgz", + "integrity": "sha512-ITvGim8FhRiYe4IQ5uHSkj7pVaPDrCTkNd3yq3cV7iZAcJdHTUMPMEHcqSOy9xZ9qFenQCvi+2wjH9a1nXqHww==", + "dev": true + }, "is-utf8": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/is-utf8/-/is-utf8-0.2.1.tgz", @@ -7320,6 +7307,17 @@ "resolved": "https://registry.npmjs.org/is-wsl/-/is-wsl-1.1.0.tgz", "integrity": "sha1-HxbkqiKwTRM2tmGIpmrzxgDDpm0=" }, + "is2": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/is2/-/is2-2.0.1.tgz", + "integrity": "sha512-+WaJvnaA7aJySz2q/8sLjMb2Mw14KTplHmSwcSpZ/fWJPkUmqw3YTzSWbPJ7OAwRvdYTWF2Wg+yYJ1AdP5Z8CA==", + "dev": true, + "requires": { + "deep-is": "^0.1.3", + "ip-regex": "^2.1.0", + "is-url": "^1.2.2" + } + }, "isarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", @@ -11505,6 +11503,33 @@ "integrity": "sha512-4WK/bYZmj8xLr+HUCODHGF1ZFzsYffasLUgEiMBY4fgtltdO6B4WJtlSbPaDTLpYTcGVwM2qLnFTICEcNxs3kA==", "dev": true }, + "tcp-port-used": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/tcp-port-used/-/tcp-port-used-1.0.1.tgz", + "integrity": "sha512-rwi5xJeU6utXoEIiMvVBMc9eJ2/ofzB+7nLOdnZuFTmNCLqRiQh2sMG9MqCxHU/69VC/Fwp5dV9306Qd54ll1Q==", + "dev": true, + "requires": { + "debug": "4.1.0", + "is2": "2.0.1" + }, + "dependencies": { + "debug": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.0.tgz", + "integrity": "sha512-heNPJUJIqC+xB6ayLAMHaIrmN9HKa7aQO8MGqKpvCA+uJYVcvR6l5kgdrhRuwPFHU7P5/A1w0BjByPHwpfTDKg==", + "dev": true, + "requires": { + "ms": "^2.1.1" + } + }, + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", + "dev": true + } + } + }, "terser": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/terser/-/terser-4.0.0.tgz", diff --git a/package.json b/package.json index acc6a6041c..4b312c9109 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "lint": "npm-run-all -l -p \"lint:**\"", "commitlint": "commitlint --from=master", "security": "npm audit", - "test:only": "jest --runInBand", + "test:only": "jest", "test:coverage": "npm run test:only -- --coverage", "test:watch": "npm run test:coverage --watch", "test": "npm run test:coverage", @@ -103,6 +103,7 @@ "standard-version": "^6.0.1", "style-loader": "^0.23.1", "supertest": "^4.0.2", + "tcp-port-used": "^1.0.1", "url-loader": "^2.0.0", "webpack": "^4.33.0", "webpack-cli": "^3.3.3", diff --git a/test/cli/cli.test.js b/test/cli/cli.test.js index 65b38dab4b..404cff789c 100644 --- a/test/cli/cli.test.js +++ b/test/cli/cli.test.js @@ -128,8 +128,10 @@ describe('CLI', () => { const cliPath = resolve(__dirname, '../../bin/webpack-dev-server.js'); const examplePath = resolve(__dirname, '../../examples/cli/public'); - const cp = execa('node', [cliPath], { cwd: examplePath }); - const cp2 = execa('node', [cliPath], { cwd: examplePath }); + const cp = execa('node', [cliPath, '--colors=false'], { cwd: examplePath }); + const cp2 = execa('node', [cliPath, '--colors=false'], { + cwd: examplePath, + }); const runtime = { cp: { diff --git a/test/client/clients/SockJSClient.test.js b/test/client/clients/SockJSClient.test.js index 2aed40499e..62817459da 100644 --- a/test/client/clients/SockJSClient.test.js +++ b/test/client/clients/SockJSClient.test.js @@ -5,6 +5,7 @@ const express = require('express'); const sockjs = require('sockjs'); const SockJSClient = require('../../../client-src/clients/SockJSClient'); const timer = require('../../helpers/timer'); +const port = require('../../ports-map').sockJSClient; describe('SockJSClient', () => { let socketServer; @@ -15,7 +16,7 @@ describe('SockJSClient', () => { const app = new express(); listeningApp = http.createServer(app); - listeningApp.listen(8080, 'localhost', () => { + listeningApp.listen(port, 'localhost', () => { socketServer = sockjs.createServer(); socketServer.installHandlers(listeningApp, { prefix: '/sockjs-node', @@ -33,7 +34,7 @@ describe('SockJSClient', () => { connection.close(); }); - const client = new SockJSClient('http://localhost:8080/sockjs-node'); + const client = new SockJSClient(`http://localhost:${port}/sockjs-node`); const data = []; client.onOpen(() => { diff --git a/test/e2e/Client.test.js b/test/e2e/Client.test.js index d5e16e0cf6..3b15a29d18 100644 --- a/test/e2e/Client.test.js +++ b/test/e2e/Client.test.js @@ -9,6 +9,7 @@ const reloadConfig = require('../fixtures/reload-config/webpack.config'); const { writeAsync } = require('../helpers/fs'); const testServer = require('../helpers/test-server'); const runBrowser = require('../helpers/run-browser'); +const port = require('../ports-map').Client; const cssFilePath = resolve(__dirname, '../fixtures/reload-config/main.css'); @@ -20,7 +21,7 @@ describe('reload', () => { 'body { background-color: rgb(0, 0, 255); }' ); const options = { - port: 9000, + port, host: '0.0.0.0', inline: true, hot: true, @@ -41,7 +42,7 @@ describe('reload', () => { const { page, browser } = await runBrowser(); let refreshed = false; - page.goto('http://localhost:9000/main'); + page.goto(`http://localhost:${port}/main`); await page.waitForNavigation({ waitUntil: 'load' }); { @@ -59,7 +60,7 @@ describe('reload', () => { if ( req.isNavigationRequest() && req.frame() === page.mainFrame() && - req.url() === 'http://localhost:9000/main' + req.url() === `http://localhost:${port}/main` ) { refreshed = true; } diff --git a/test/e2e/ClientOptions.test.js b/test/e2e/ClientOptions.test.js index fed3492343..d37f66fe03 100644 --- a/test/e2e/ClientOptions.test.js +++ b/test/e2e/ClientOptions.test.js @@ -7,6 +7,7 @@ const testServer = require('../helpers/test-server'); const config = require('../fixtures/client-config/webpack.config'); const runBrowser = require('../helpers/run-browser'); const timer = require('../helpers/timer'); +const [port1, port2, port3] = require('../ports-map').ClientOptions; describe('Client code', () => { function startProxy(port) { @@ -14,7 +15,7 @@ describe('Client code', () => { proxy.use( '/', httpProxy({ - target: 'http://localhost:9001', + target: `http://localhost:${port1}`, ws: true, changeOrigin: true, }) @@ -25,7 +26,7 @@ describe('Client code', () => { beforeAll((done) => { const options = { compress: true, - port: 9001, + port: port1, host: '0.0.0.0', disableHostCheck: true, inline: true, @@ -39,12 +40,12 @@ describe('Client code', () => { afterAll(testServer.close); - // [HPM] Proxy created: / -> http://localhost:9001 + // [HPM] Proxy created: / -> http://localhost:{port1} describe('behind a proxy', () => { let proxy; beforeAll(() => { - proxy = startProxy(9000); + proxy = startProxy(port2); }); afterAll((done) => { @@ -54,10 +55,10 @@ describe('Client code', () => { }); it('responds with a 200', async () => { - await request('http://localhost:9000') + await request(`http://localhost:${port2}`) .get('/sockjs-node') .expect(200, 'Welcome to SockJS!\n'); - await request('http://localhost:9001') + await request(`http://localhost:${port1}`) .get('/sockjs-node') .expect(200, 'Welcome to SockJS!\n'); }); @@ -65,13 +66,15 @@ describe('Client code', () => { it('requests websocket through the proxy with proper port number', async () => { const { page, browser } = await runBrowser(); - page.goto('http://localhost:9000/main'); + page.goto(`http://localhost:${port2}/main`); const req = await page.waitForRequest((requestObj) => requestObj.url().match(/sockjs-node/) ); - expect(req.url()).toMatch(/^http:\/\/localhost:9001\/sockjs-node/); + expect( + req.url().includes(`http://localhost:${port1}/sockjs-node`) + ).toBeTruthy(); await browser.close(); }); @@ -81,7 +84,7 @@ describe('Client code', () => { describe('Client complex inline script path', () => { beforeAll((done) => { const options = { - port: 9000, + port: port2, host: '0.0.0.0', inline: true, watchOptions: { @@ -99,13 +102,15 @@ describe('Client complex inline script path', () => { it('uses the correct public hostname and sockPath', async () => { const { page, browser } = await runBrowser(); - page.goto('http://localhost:9000/main'); + page.goto(`http://localhost:${port2}/main`); const req = await page.waitForRequest((requestObj) => requestObj.url().match(/foo\/test\/bar/) ); - expect(req.url()).toMatch(/^http:\/\/myhost\.test:9000\/foo\/test\/bar/); + expect( + req.url().includes(`http://myhost.test:${port2}/foo/test/bar/`) + ).toBeTruthy(); await browser.close(); }); @@ -115,14 +120,14 @@ describe('Client complex inline script path', () => { describe('Client complex inline script path with sockPort', () => { beforeAll((done) => { const options = { - port: 9000, + port: port2, host: '0.0.0.0', inline: true, watchOptions: { poll: true, }, sockPath: '/foo/test/bar/', - sockPort: 8080, + sockPort: port3, }; testServer.startAwaitingCompilation(config, options, done); }); @@ -133,13 +138,15 @@ describe('Client complex inline script path with sockPort', () => { it('uses the correct sockPort', async () => { const { page, browser } = await runBrowser(); - page.goto('http://localhost:9000/main'); + page.goto(`http://localhost:${port2}/main`); const req = await page.waitForRequest((requestObj) => requestObj.url().match(/foo\/test\/bar/) ); - expect(req.url()).toMatch(/^http:\/\/localhost:8080\/foo\/test\/bar/); + expect( + req.url().includes(`http://localhost:${port3}/foo/test/bar`) + ).toBeTruthy(); await browser.close(); }); @@ -152,13 +159,13 @@ describe('Client complex inline script path with sockPort', () => { describe('Client complex inline script path with sockPort, no sockPath', () => { beforeAll((done) => { const options = { - port: 9000, + port: port2, host: '0.0.0.0', inline: true, watchOptions: { poll: true, }, - sockPort: 8080, + sockPort: port3, }; testServer.startAwaitingCompilation(config, options, done); }); @@ -169,13 +176,15 @@ describe('Client complex inline script path with sockPort, no sockPath', () => { it('uses the correct sockPort and sockPath', async () => { const { page, browser } = await runBrowser(); - page.goto('http://localhost:9000/main'); + page.goto(`http://localhost:${port2}/main`); const req = await page.waitForRequest((requestObj) => requestObj.url().match(/sockjs-node/) ); - expect(req.url()).toMatch(/^http:\/\/localhost:8080\/sockjs-node/); + expect( + req.url().includes(`http://localhost:${port3}/sockjs-node`) + ).toBeTruthy(); await browser.close(); }); @@ -185,7 +194,7 @@ describe('Client complex inline script path with sockPort, no sockPath', () => { describe('Client complex inline script path with sockHost', () => { beforeAll((done) => { const options = { - port: 9000, + port: port2, host: '0.0.0.0', inline: true, watchOptions: { @@ -202,13 +211,15 @@ describe('Client complex inline script path with sockHost', () => { it('uses the correct sockHost', async () => { const { page, browser } = await runBrowser(); - page.goto('http://localhost:9000/main'); + page.goto(`http://localhost:${port2}/main`); const req = await page.waitForRequest((requestObj) => requestObj.url().match(/sockjs-node/) ); - expect(req.url()).toMatch(/^http:\/\/myhost\.test:9000\/sockjs-node/); + expect( + req.url().includes(`http://myhost.test:${port2}/sockjs-node`) + ).toBeTruthy(); await browser.close(); }); @@ -217,7 +228,7 @@ describe('Client complex inline script path with sockHost', () => { describe('Client console.log', () => { const baseOptions = { - port: 9000, + port: port2, host: '0.0.0.0', }; const cases = [ @@ -265,12 +276,12 @@ describe('Client console.log', () => { const { page, browser } = await runBrowser(); - page.goto('http://localhost:9000/main'); + page.goto(`http://localhost:${port2}/main`); page.on('console', ({ _text }) => { res.push(_text); }); - await timer(3000); + await timer(1000); expect(res).toMatchSnapshot(); await browser.close(); diff --git a/test/integration/MultiCompiler.test.js b/test/integration/MultiCompiler.test.js index 109e9d8060..d35fe9ba78 100644 --- a/test/integration/MultiCompiler.test.js +++ b/test/integration/MultiCompiler.test.js @@ -3,13 +3,14 @@ const request = require('supertest'); const testServer = require('../helpers/test-server'); const config = require('../fixtures/multi-compiler-config/webpack.config'); +const port = require('../ports-map').MultiCompiler; describe('multi compiler', () => { let server; let req; beforeAll((done) => { - server = testServer.start(config, {}, done); + server = testServer.start(config, { port }, done); req = request(server.app); }); diff --git a/test/integration/UniversalCompiler.test.js b/test/integration/UniversalCompiler.test.js index f7d6caab2b..3ae2c3bd7a 100644 --- a/test/integration/UniversalCompiler.test.js +++ b/test/integration/UniversalCompiler.test.js @@ -3,13 +3,14 @@ const request = require('supertest'); const testServer = require('../helpers/test-server'); const config = require('../fixtures/universal-compiler-config/webpack.config'); +const port = require('../ports-map').UniversalCompiler; describe('universal compiler', () => { let server; let req; beforeAll((done) => { - server = testServer.start(config, { inline: true }, done); + server = testServer.start(config, { inline: true, port }, done); req = request(server.app); }); diff --git a/test/ports-map.js b/test/ports-map.js new file mode 100644 index 0000000000..f39661bc0b --- /dev/null +++ b/test/ports-map.js @@ -0,0 +1,49 @@ +'use strict'; + +// test-file-name: the number of ports +const portsList = { + cli: 2, + sockJSClient: 1, + SockJSServer: 1, + Client: 1, + ClientOptions: 3, + MultiCompiler: 1, + UniversalCompiler: 1, + Server: 1, + routes: 1, + createDomain: 2, + 'after-option': 1, + 'before-option': 1, + 'compress-option': 1, + 'contentBase-option': 1, + 'headers-option': 1, + 'historyApiFallback-option': 1, + 'host-option': 1, + 'hot-option': 1, + 'hotOnly-option': 1, + 'http2-option': 1, + 'https-option': 1, + 'inline-option': 1, + 'lazy-option': 1, + 'liveReload-option': 1, + 'mineTypes-option': 1, + 'onListening-option': 1, + 'open-option': 1, + 'port-option': 1, + 'proxy-option': 4, + 'serverMode-option': 1, + 'sockPath-option': 1, + 'stats-option': 1, +}; + +let startPort = 8079; +const ports = {}; + +Object.entries(portsList).forEach(([key, value]) => { + ports[key] = + value === 1 + ? (startPort += 1) + : [...new Array(value)].map(() => (startPort += 1)); +}); + +module.exports = ports; diff --git a/test/server/Server.test.js b/test/server/Server.test.js index b2b44731e2..487942e212 100644 --- a/test/server/Server.test.js +++ b/test/server/Server.test.js @@ -6,6 +6,7 @@ const sockjs = require('sockjs/lib/transport'); const { noop } = require('webpack-dev-middleware/lib/util'); const Server = require('../../lib/Server'); const config = require('../fixtures/simple-config/webpack.config'); +const port = require('../ports-map').Server; jest.mock('sockjs/lib/transport'); @@ -23,6 +24,7 @@ describe('Server', () => { const compiler = webpack(config); const server = new Server(compiler, { hot: true, + port, }); expect( @@ -45,6 +47,7 @@ describe('Server', () => { const compiler = webpack(config); const server = new Server(compiler, { hotOnly: true, + port, }); expect( @@ -92,7 +95,7 @@ describe('Server', () => { }); const compiler = webpack(config); - const server = new Server(compiler); + const server = new Server(compiler, { port }); compiler.hooks.done.tap('webpack-dev-server', (s) => { const output = server.getStats(s); @@ -102,7 +105,7 @@ describe('Server', () => { }); compiler.run(() => {}); - server.listen(8080, 'localhost'); + server.listen(port, 'localhost'); }); }); @@ -136,7 +139,7 @@ describe('Server', () => { describe('Testing callback functions on calling invalidate without callback', () => { it('should be `noop` (the default callback function)', (done) => { const compiler = webpack(config); - const server = new Server(compiler); + const server = new Server(compiler, { port }); server.invalidate(); expect(server.middleware.context.callbacks[0]).toBe(noop); @@ -153,7 +156,7 @@ describe('Server', () => { it('should be `callback` function', (done) => { const compiler = webpack(config); const callback = jest.fn(); - const server = new Server(compiler); + const server = new Server(compiler, { port }); server.invalidate(callback); diff --git a/test/server/__snapshots__/Server.test.js.snap b/test/server/__snapshots__/Server.test.js.snap index b606b0561f..8d2d66684b 100644 --- a/test/server/__snapshots__/Server.test.js.snap +++ b/test/server/__snapshots__/Server.test.js.snap @@ -5,7 +5,7 @@ Array [ Array [ "client", "index.js?http:", - "localhost", + "localhost:8090", ], Array [ "node_modules", @@ -35,7 +35,7 @@ Array [ Array [ "client", "index.js?http:", - "localhost", + "localhost:8090", ], Array [ "node_modules", diff --git a/test/server/after-option.test.js b/test/server/after-option.test.js index 51a795fd92..07e7fd5545 100644 --- a/test/server/after-option.test.js +++ b/test/server/after-option.test.js @@ -3,6 +3,7 @@ const request = require('supertest'); const testServer = require('../helpers/test-server'); const config = require('../fixtures/simple-config/webpack.config'); +const port = require('../ports-map')['after-option']; describe('after option', () => { let server; @@ -29,6 +30,7 @@ describe('after option', () => { response.send('after'); }); }, + port, }, done ); diff --git a/test/server/before-option.test.js b/test/server/before-option.test.js index f857ccbd87..4e831133c2 100644 --- a/test/server/before-option.test.js +++ b/test/server/before-option.test.js @@ -3,6 +3,7 @@ const request = require('supertest'); const testServer = require('../helpers/test-server'); const config = require('../fixtures/simple-config/webpack.config'); +const port = require('../ports-map')['before-option']; describe('before option', () => { let server; @@ -29,6 +30,7 @@ describe('before option', () => { response.send('before'); }); }, + port, }, done ); diff --git a/test/server/compress-option.test.js b/test/server/compress-option.test.js index 8fe337bd73..644d40356f 100644 --- a/test/server/compress-option.test.js +++ b/test/server/compress-option.test.js @@ -8,6 +8,7 @@ const request = require('supertest'); const testServer = require('../helpers/test-server'); const config = require('../fixtures/simple-config-other/webpack.config'); +const port = require('../ports-map')['compress-option']; describe('compress option', () => { let server; @@ -15,7 +16,7 @@ describe('compress option', () => { describe('not specify', () => { beforeAll((done) => { - server = testServer.start(config, {}, done); + server = testServer.start(config, { port }, done); req = request(server.app); }); @@ -39,6 +40,7 @@ describe('compress option', () => { config, { compress: true, + port, }, done ); @@ -61,6 +63,7 @@ describe('compress option', () => { config, { compress: false, + port, }, done ); diff --git a/test/server/contentBase-option.test.js b/test/server/contentBase-option.test.js index f630ee470d..00619a657a 100644 --- a/test/server/contentBase-option.test.js +++ b/test/server/contentBase-option.test.js @@ -5,6 +5,7 @@ const fs = require('fs'); const request = require('supertest'); const testServer = require('../helpers/test-server'); const config = require('../fixtures/contentbase-config/webpack.config'); +const port = require('../ports-map')['contentBase-option']; const contentBasePublic = path.resolve( __dirname, @@ -28,6 +29,7 @@ describe('contentBase option', () => { { contentBase: contentBasePublic, watchContentBase: true, + port, }, done ); @@ -91,6 +93,7 @@ describe('contentBase option', () => { contentBase: contentBasePublic, watchContentBase: true, serveIndex: false, + port, }, done ); @@ -120,6 +123,7 @@ describe('contentBase option', () => { contentBase: contentBasePublic, watchContentBase: true, serveIndex: true, + port, }, done ); @@ -146,6 +150,7 @@ describe('contentBase option', () => { { contentBase: contentBasePublic, watchContentBase: true, + port, }, done ); @@ -171,6 +176,7 @@ describe('contentBase option', () => { config, { contentBase: [contentBasePublic, contentBaseOther], + port, }, done ); @@ -196,6 +202,7 @@ describe('contentBase option', () => { config, { contentBase: 9099999, + port, }, done ); @@ -220,6 +227,7 @@ describe('contentBase option', () => { config, { contentBase: 'http://example.com/', + port, }, done ); @@ -251,7 +259,7 @@ describe('contentBase option', () => { beforeAll((done) => { jest.spyOn(process, 'cwd').mockImplementation(() => contentBasePublic); - server = testServer.start(config, {}, done); + server = testServer.start(config, { port }, done); req = request(server.app); }); @@ -274,6 +282,7 @@ describe('contentBase option', () => { config, { contentBase: false, + port, }, done ); @@ -295,6 +304,7 @@ describe('contentBase option', () => { config, { contentBase: [contentBasePublic], + port, }, done ); diff --git a/test/server/headers-option.test.js b/test/server/headers-option.test.js index 394d18ff3a..1891fe01a2 100644 --- a/test/server/headers-option.test.js +++ b/test/server/headers-option.test.js @@ -3,6 +3,7 @@ const request = require('supertest'); const testServer = require('../helpers/test-server'); const config = require('../fixtures/simple-config/webpack.config'); +const port = require('../ports-map')['headers-option']; describe('headers option', () => { let server; @@ -14,6 +15,7 @@ describe('headers option', () => { config, { headers: { 'X-Foo': '1' }, + port, }, done ); @@ -36,6 +38,7 @@ describe('headers option', () => { config, { headers: { 'X-Bar': ['key1=value1', 'key2=value2'] }, + port, }, done ); diff --git a/test/server/historyApiFallback-option.test.js b/test/server/historyApiFallback-option.test.js index e876004995..e76f0ea625 100644 --- a/test/server/historyApiFallback-option.test.js +++ b/test/server/historyApiFallback-option.test.js @@ -6,6 +6,7 @@ const testServer = require('../helpers/test-server'); const config = require('../fixtures/historyapifallback-config/webpack.config'); const config2 = require('../fixtures/historyapifallback-2-config/webpack.config'); const config3 = require('../fixtures/historyapifallback-3-config/webpack.config'); +const port = require('../ports-map')['historyApiFallback-option']; describe('historyApiFallback option', () => { let server; @@ -19,6 +20,7 @@ describe('historyApiFallback option', () => { config, { historyApiFallback: true, + port, }, done ); @@ -41,6 +43,7 @@ describe('historyApiFallback option', () => { historyApiFallback: { index: '/bar.html', }, + port, }, done ); @@ -67,6 +70,7 @@ describe('historyApiFallback option', () => { historyApiFallback: { index: '/bar.html', }, + port, }, done ); @@ -113,6 +117,7 @@ describe('historyApiFallback option', () => { historyApiFallback: { index: '/bar.html', }, + port, }, done ); @@ -148,6 +153,7 @@ describe('historyApiFallback option', () => { }, ], }, + port, }, done ); @@ -186,6 +192,7 @@ describe('historyApiFallback option', () => { '../fixtures/historyapifallback-3-config' ), historyApiFallback: true, + port, }, done ); diff --git a/test/server/host-option.test.js b/test/server/host-option.test.js index c908ba5316..667caabd9f 100644 --- a/test/server/host-option.test.js +++ b/test/server/host-option.test.js @@ -3,6 +3,7 @@ const request = require('supertest'); const config = require('../fixtures/simple-config/webpack.config'); const testServer = require('../helpers/test-server'); +const port = require('../ports-map')['host-option']; describe('host option', () => { let server = null; @@ -10,7 +11,7 @@ describe('host option', () => { describe('is not be specified', () => { beforeAll((done) => { - server = testServer.start(config, {}, done); + server = testServer.start(config, { port }, done); req = request(server.app); }); @@ -18,7 +19,7 @@ describe('host option', () => { const address = server.listeningApp.address(); expect(address.address).toBe('127.0.0.1'); - expect(address.port).toBe(8080); + expect(address.port).toBe(port); }); it('Request to index', (done) => { @@ -35,6 +36,7 @@ describe('host option', () => { { // eslint-disable-next-line no-undefined host: undefined, + port, }, done ); @@ -45,7 +47,7 @@ describe('host option', () => { const address = server.listeningApp.address(); expect(address.address).toBe('::'); - expect(address.port).toBe(8080); + expect(address.port).toBe(port); }); it('Request to index', async () => { @@ -61,6 +63,7 @@ describe('host option', () => { config, { host: null, + port, }, done ); @@ -71,7 +74,7 @@ describe('host option', () => { const address = server.listeningApp.address(); expect(address.address).toBe('::'); - expect(address.port).toBe(8080); + expect(address.port).toBe(port); }); it('Request to index', async () => { @@ -87,6 +90,7 @@ describe('host option', () => { config, { host: '127.0.0.1', + port, }, done ); @@ -97,7 +101,7 @@ describe('host option', () => { const address = server.listeningApp.address(); expect(address.address).toBe('127.0.0.1'); - expect(address.port).toBe(8080); + expect(address.port).toBe(port); }); it('Request to index', async () => { @@ -113,6 +117,7 @@ describe('host option', () => { config, { host: 'localhost', + port, }, done ); @@ -123,7 +128,7 @@ describe('host option', () => { const address = server.listeningApp.address(); expect(address.address).toBe('127.0.0.1'); - expect(address.port).toBe(8080); + expect(address.port).toBe(port); }); it('Request to index', async () => { @@ -139,6 +144,7 @@ describe('host option', () => { config, { host: '0.0.0.0', + port, }, done ); @@ -149,7 +155,7 @@ describe('host option', () => { const address = server.listeningApp.address(); expect(address.address).toBe('0.0.0.0'); - expect(address.port).toBe(8080); + expect(address.port).toBe(port); }); it('Request to index', async () => { diff --git a/test/server/hot-option.test.js b/test/server/hot-option.test.js index 6804b313eb..e79679ff4b 100644 --- a/test/server/hot-option.test.js +++ b/test/server/hot-option.test.js @@ -4,6 +4,7 @@ const request = require('supertest'); const testServer = require('../helpers/test-server'); const config = require('../fixtures/client-config/webpack.config'); const multiCompilerConfig = require('../fixtures/multi-compiler-config/webpack.config'); +const port = require('../ports-map')['hot-option']; describe('hot option', () => { let server; @@ -12,7 +13,7 @@ describe('hot option', () => { describe('simple hot config entries', () => { beforeAll((done) => { const options = { - port: 9000, + port, inline: true, hot: true, watchOptions: { @@ -33,7 +34,7 @@ describe('hot option', () => { describe('multi compiler hot config entries', () => { beforeAll((done) => { const options = { - port: 9000, + port, inline: true, hot: true, watchOptions: { @@ -58,7 +59,7 @@ describe('hot option', () => { describe('hot disabled entries', () => { beforeAll((done) => { const options = { - port: 9000, + port, inline: true, hot: false, watchOptions: { @@ -85,7 +86,7 @@ describe('hot option', () => { it('should register the HMR plugin before compilation is complete', (done) => { let pluginFound = false; const options = { - port: 9000, + port, inline: true, hot: true, watchOptions: { @@ -119,7 +120,7 @@ describe('hot option', () => { it('should register the HMR plugin before compilation is complete', (done) => { let pluginFound = false; const options = { - port: 9000, + port, inline: true, hot: true, watchOptions: { @@ -153,7 +154,7 @@ describe('hot option', () => { it('should NOT register the HMR plugin before compilation is complete', (done) => { let pluginFound = false; const options = { - port: 9000, + port, inline: true, hot: false, watchOptions: { diff --git a/test/server/hotOnly-option.test.js b/test/server/hotOnly-option.test.js index aa15ef5b39..61cba1eb88 100644 --- a/test/server/hotOnly-option.test.js +++ b/test/server/hotOnly-option.test.js @@ -3,6 +3,7 @@ const request = require('supertest'); const testServer = require('../helpers/test-server'); const config = require('../fixtures/client-config/webpack.config'); +const port = require('../ports-map')['hotOnly-option']; describe('hotOnly options', () => { let server; @@ -11,7 +12,7 @@ describe('hotOnly options', () => { describe('simple hotOnly config entries', () => { beforeAll((done) => { const options = { - port: 9000, + port, inline: true, hotOnly: true, watchOptions: { @@ -35,7 +36,7 @@ describe('hotOnly options', () => { it('should register the HMR plugin before compilation is complete', (done) => { let pluginFound = false; const options = { - port: 9000, + port, inline: true, hotOnly: true, watchOptions: { diff --git a/test/server/http2-option.test.js b/test/server/http2-option.test.js index ee05992483..eee2862848 100644 --- a/test/server/http2-option.test.js +++ b/test/server/http2-option.test.js @@ -5,6 +5,7 @@ const request = require('supertest'); const semver = require('semver'); const testServer = require('../helpers/test-server'); const config = require('../fixtures/contentbase-config/webpack.config'); +const port = require('../ports-map')['http2-option']; const contentBasePublic = path.resolve( __dirname, @@ -37,6 +38,7 @@ describe('http2 option', () => { contentBase: contentBasePublic, https: true, http2: true, + port, }, done ); @@ -44,7 +46,7 @@ describe('http2 option', () => { }); it('confirm http2 client can connect', (done) => { - const client = http2.connect('https://localhost:8080', { + const client = http2.connect(`https://localhost:${port}`, { rejectUnauthorized: false, }); client.on('error', (err) => console.error(err)); @@ -78,6 +80,7 @@ describe('http2 option', () => { { contentBase: contentBasePublic, http2: true, + port, }, done ); @@ -99,6 +102,7 @@ describe('http2 option', () => { contentBase: contentBasePublic, https: true, http2: false, + port, }, done ); diff --git a/test/server/https-option.test.js b/test/server/https-option.test.js index 5dcd2964aa..2c569ea5df 100644 --- a/test/server/https-option.test.js +++ b/test/server/https-option.test.js @@ -5,8 +5,8 @@ const fs = require('fs'); const request = require('supertest'); const testServer = require('../helpers/test-server'); const config = require('../fixtures/contentbase-config/webpack.config'); -const skipTestOnWindows = require('../helpers/conditional-test') - .skipTestOnWindows; +const { skipTestOnWindows } = require('../helpers/conditional-test'); +const port = require('../ports-map')['https-option']; const httpsCertificateDirectory = path.resolve( __dirname, @@ -28,6 +28,7 @@ describe('https option', () => { { contentBase: contentBasePublic, https: true, + port, }, done ); @@ -62,6 +63,7 @@ describe('https option', () => { ), passphrase: 'webpack-dev-server', }, + port, }, done ); @@ -86,6 +88,7 @@ describe('https option', () => { cert: path.join(httpsCertificateDirectory, 'server.crt'), passphrase: 'webpack-dev-server', }, + port, }, done ); @@ -114,6 +117,7 @@ describe('https option', () => { cert: path.join(httpsCertificateDirectory, 'server-symlink.crt'), passphrase: 'webpack-dev-server', }, + port, }, done ); @@ -149,6 +153,7 @@ describe('https option', () => { .toString(), passphrase: 'webpack-dev-server', }, + port, }, done ); diff --git a/test/server/inline-option.test.js b/test/server/inline-option.test.js index b3d2e40e3c..18f72bdcef 100644 --- a/test/server/inline-option.test.js +++ b/test/server/inline-option.test.js @@ -4,6 +4,7 @@ const request = require('supertest'); const testServer = require('../helpers/test-server'); const config = require('../fixtures/client-config/webpack.config'); const multiCompilerConfig = require('../fixtures/multi-compiler-config/webpack.config'); +const port = require('../ports-map')['inline-option']; describe('inline option', () => { let server; @@ -12,7 +13,7 @@ describe('inline option', () => { describe('simple inline config entries', () => { beforeAll((done) => { const options = { - port: 9000, + port, host: '0.0.0.0', inline: true, watchOptions: { @@ -26,16 +27,16 @@ describe('inline option', () => { afterAll(testServer.close); it('should include inline client script in the bundle', async () => { - await req - .get('/main.js') - .expect(200, /client\/index\.js\?http:\/\/0\.0\.0\.0:9000/); + const url = new RegExp(`client/index.js\\?http://0.0.0.0:${port}`); + + await req.get('/main.js').expect(200, url); }); }); describe('multi compiler inline config entries', () => { beforeAll((done) => { const options = { - port: 9000, + port, host: '0.0.0.0', inline: true, watchOptions: { @@ -52,17 +53,17 @@ describe('inline option', () => { afterAll(testServer.close); - it('should include inline client script in the bundle', (done) => { - req - .get('/main.js') - .expect(200, /client\/index\.js\?http:\/\/0\.0\.0\.0:9000/, done); + it('should include inline client script in the bundle', async () => { + const url = new RegExp(`client/index.js\\?http://0.0.0.0:${port}`); + + await req.get('/main.js').expect(200, url); }); }); describe('inline disabled entries', () => { beforeAll((done) => { const options = { - port: 9000, + port, host: '0.0.0.0', inline: false, watchOptions: { @@ -78,7 +79,7 @@ describe('inline option', () => { it('should NOT include inline client script in the bundle', async () => { const { text } = await req.get('/main.js').expect(200); - expect(text).not.toMatch(/client\/index\.js\?http:\/\/0\.0\.0\.0:9000/); + expect(text.includes(`client/index.js?http://0.0.0.0:${port}`)); }); }); }); diff --git a/test/server/lazy-option.test.js b/test/server/lazy-option.test.js index 515126f8ed..1d2a660aaf 100644 --- a/test/server/lazy-option.test.js +++ b/test/server/lazy-option.test.js @@ -2,6 +2,7 @@ const testServer = require('../helpers/test-server'); const config = require('../fixtures/simple-config/webpack.config'); +const port = require('../ports-map')['lazy-option']; describe('lazy option', () => { afterEach(testServer.close); @@ -10,6 +11,7 @@ describe('lazy option', () => { expect(() => { testServer.start(config, { lazy: true, + port, }); }).toThrow(/'filename' option must be set/); }); @@ -20,6 +22,7 @@ describe('lazy option', () => { { lazy: true, filename: 'bundle.js', + port, }, done ); diff --git a/test/server/liveReload-option.test.js b/test/server/liveReload-option.test.js index 6226f4d771..5cde4c0c7c 100644 --- a/test/server/liveReload-option.test.js +++ b/test/server/liveReload-option.test.js @@ -4,6 +4,7 @@ const path = require('path'); const fs = require('fs'); const testServer = require('../helpers/test-server'); const config = require('../fixtures/contentbase-config/webpack.config'); +const port = require('../ports-map')['liveReload-option']; const contentBasePublic = path.resolve( __dirname, @@ -23,6 +24,7 @@ describe('liveReload option', () => { contentBase: contentBasePublic, watchContentBase: true, liveReload: false, + port, }, done ); @@ -70,6 +72,7 @@ describe('liveReload option', () => { contentBase: contentBasePublic, watchContentBase: true, liveReload: true, + port, }, done ); diff --git a/test/server/mimeTypes-option.test.js b/test/server/mimeTypes-option.test.js index 4d5f56e45f..5f2fef5e7a 100644 --- a/test/server/mimeTypes-option.test.js +++ b/test/server/mimeTypes-option.test.js @@ -3,6 +3,7 @@ const request = require('supertest'); const testServer = require('../helpers/test-server'); const config = require('../fixtures/simple-config/webpack.config'); +const port = require('../ports-map')['mineTypes-option']; describe('mimeTypes option', () => { describe('as an object', () => { @@ -12,6 +13,7 @@ describe('mimeTypes option', () => { expect(() => { testServer.start(config, { mimeTypes: { 'application/octet-stream': ['js'] }, + port, }); }).toThrow(/Attempt to change mapping for/); }); @@ -24,6 +26,7 @@ describe('mimeTypes option', () => { typeMap: { 'application/octet-stream': ['js'] }, force: true, }, + port, }, done ); @@ -42,6 +45,7 @@ describe('mimeTypes option', () => { typeMap: { 'application/octet-stream': ['js'] }, force: true, }, + port, }, done ); diff --git a/test/server/onListening-option.test.js b/test/server/onListening-option.test.js index 43db18eec8..d6d66b1565 100644 --- a/test/server/onListening-option.test.js +++ b/test/server/onListening-option.test.js @@ -2,6 +2,7 @@ const testServer = require('../helpers/test-server'); const config = require('../fixtures/simple-config/webpack.config'); +const port = require('../ports-map')['onListening-option']; describe('onListening option', () => { let onListeningIsRunning = false; @@ -17,6 +18,7 @@ describe('onListening option', () => { onListeningIsRunning = true; }, + port, }, done ); diff --git a/test/server/open-option.test.js b/test/server/open-option.test.js index 1dae3cba70..b8f289859e 100644 --- a/test/server/open-option.test.js +++ b/test/server/open-option.test.js @@ -6,6 +6,7 @@ const webpack = require('webpack'); const open = require('open'); const Server = require('../../lib/Server'); const config = require('../fixtures/simple-config/webpack.config'); +const port = require('../ports-map')['open-option']; open.mockImplementation(() => { return { @@ -18,15 +19,16 @@ describe('open option', () => { const compiler = webpack(config); const server = new Server(compiler, { open: true, + port, }); compiler.hooks.done.tap('webpack-dev-server', () => { - expect(open.mock.calls[0]).toEqual(['http://localhost:8080/', {}]); + expect(open.mock.calls[0]).toEqual([`http://localhost:${port}/`, {}]); expect(open.mock.invocationCallOrder[0]).toEqual(1); server.close(done); }); compiler.run(() => {}); - server.listen(8080, 'localhost'); + server.listen(port, 'localhost'); }); }); diff --git a/test/server/port-option.test.js b/test/server/port-option.test.js index 534fab419f..d9fd6c2391 100644 --- a/test/server/port-option.test.js +++ b/test/server/port-option.test.js @@ -3,6 +3,7 @@ const request = require('supertest'); const testServer = require('../helpers/test-server'); const config = require('../fixtures/simple-config/webpack.config'); +const port = require('../ports-map')['port-option']; describe('port', () => { let server = null; @@ -10,7 +11,7 @@ describe('port', () => { describe('is not be specified', () => { beforeAll((done) => { - server = testServer.start(config, {}, done); + server = testServer.start(config, { port }, done); req = request(server.app); }); diff --git a/test/server/proxy-option.test.js b/test/server/proxy-option.test.js index 8f465ccf56..21917d9ab8 100644 --- a/test/server/proxy-option.test.js +++ b/test/server/proxy-option.test.js @@ -7,21 +7,21 @@ const bodyParser = require('body-parser'); const WebSocket = require('ws'); const testServer = require('../helpers/test-server'); const config = require('../fixtures/proxy-config/webpack.config'); +const [port1, port2, port3, port4] = require('../ports-map')['proxy-option']; const WebSocketServer = WebSocket.Server; const contentBase = path.resolve(__dirname, '../fixtures/proxy-config'); const proxyOptionPathsAsProperties = { '/proxy1': { - target: 'http://localhost:9000', + target: `http://localhost:${port1}`, }, '/api/proxy2': { - target: 'http://localhost:9001', + target: `http://localhost:${port2}`, pathRewrite: { '^/api': '' }, }, '/foo': { bypass(req) { - console.log(req.path); if (/\.html$/.test(req.path)) { return '/index.html'; } @@ -40,7 +40,7 @@ const proxyOptionPathsAsProperties = { const proxyOption = { context: () => true, - target: 'http://localhost:9000', + target: `http://localhost:${port1}`, }; const proxyOptionOfArray = [ @@ -48,7 +48,7 @@ const proxyOptionOfArray = [ function proxy() { return { context: '/api/proxy2', - target: 'http://localhost:9001', + target: `http://localhost:${port2}`, pathRewrite: { '^/api': '' }, }; }, @@ -69,8 +69,8 @@ function startProxyServers() { res.send('from proxy2'); }); - listeners.push(proxy1.listen(9000)); - listeners.push(proxy2.listen(9001)); + listeners.push(proxy1.listen(port1)); + listeners.push(proxy2.listen(port2)); // return a function to shutdown proxy servers return function proxy() { @@ -93,6 +93,7 @@ describe('proxy option', () => { { contentBase, proxy: proxyOptionPathsAsProperties, + port: port3, }, done ); @@ -149,6 +150,7 @@ describe('proxy option', () => { { contentBase, proxy: proxyOption, + port: port3, }, done ); @@ -179,6 +181,7 @@ describe('proxy option', () => { { contentBase, proxy: proxyOptionOfArray, + port: port3, }, done ); @@ -206,7 +209,7 @@ describe('proxy option', () => { let req; let listener; const proxyTarget = { - target: 'http://localhost:9000', + target: `http://localhost:${port1}`, }; beforeAll((done) => { @@ -216,7 +219,7 @@ describe('proxy option', () => { res.send('from proxy'); }); - listener = proxy.listen(9000); + listener = proxy.listen(port1); server = testServer.start( config, @@ -226,6 +229,7 @@ describe('proxy option', () => { '/proxy1': proxyTarget, '/proxy2': proxyTarget, }, + port: port3, }, done ); @@ -261,15 +265,16 @@ describe('proxy option', () => { proxy: [ { context: '/', - target: 'http://localhost:9003', + target: `http://localhost:${port4}`, ws: true, }, ], + port: port3, }, done ); - wsServer = new WebSocketServer({ port: 9003 }); + wsServer = new WebSocketServer({ port: port4 }); wsServer.on('connection', (server) => { server.on('message', (message) => { server.send(message); @@ -278,7 +283,7 @@ describe('proxy option', () => { }); beforeEach((done) => { - ws = new WebSocket('ws://localhost:8080/proxy3/socket'); + ws = new WebSocket(`ws://localhost:${port3}/proxy3/socket`); ws.on('message', (message) => { responseMessage = message; done(); @@ -303,7 +308,7 @@ describe('proxy option', () => { let req; let listener; const proxyTarget = { - target: 'http://localhost:9000', + target: `http://localhost:${port1}`, }; beforeAll((done) => { @@ -339,7 +344,7 @@ describe('proxy option', () => { res.send('DELETE method from proxy'); }); - listener = proxy.listen(9000); + listener = proxy.listen(port1); server = testServer.start( config, diff --git a/test/server/serverMode-option.test.js b/test/server/serverMode-option.test.js index dfec74e152..1b226e5a96 100644 --- a/test/server/serverMode-option.test.js +++ b/test/server/serverMode-option.test.js @@ -9,6 +9,7 @@ const SockJSServer = require('../../lib/servers/SockJSServer'); const config = require('../fixtures/simple-config/webpack.config'); const testServer = require('../helpers/test-server'); const BaseServer = require('../../lib/servers/BaseServer'); +const port = require('../ports-map')['serverMode-option']; describe('serverMode option', () => { let server; @@ -26,10 +27,11 @@ describe('serverMode option', () => { config, { serverMode: 'sockjs', + port, }, done ); - req = request('http://localhost:8080'); + req = request(`http://localhost:${port}`); }); it('sockjs path responds with a 200', async () => { @@ -43,10 +45,11 @@ describe('serverMode option', () => { config, { serverMode: require.resolve('../../lib/servers/SockJSServer'), + port, }, done ); - req = request('http://localhost:8080'); + req = request(`http://localhost:${port}`); }); it('sockjs path responds with a 200', async () => { @@ -60,10 +63,11 @@ describe('serverMode option', () => { config, { serverMode: SockJSServer, + port, }, done ); - req = request('http://localhost:8080'); + req = request(`http://localhost:${port}`); }); it('sockjs path responds with a 200', async () => { @@ -76,6 +80,7 @@ describe('serverMode option', () => { server = testServer.start( config, { + port, sockPath: '/foo/test/bar/', serverMode: class MySockJSServer extends BaseServer { constructor(serv) { diff --git a/test/server/servers/SockJSServer.test.js b/test/server/servers/SockJSServer.test.js index 11a9d07923..a099f16e0c 100644 --- a/test/server/servers/SockJSServer.test.js +++ b/test/server/servers/SockJSServer.test.js @@ -5,6 +5,7 @@ const express = require('express'); const SockJS = require('sockjs-client/dist/sockjs'); const SockJSServer = require('../../../lib/servers/SockJSServer'); const timer = require('../../helpers/timer'); +const port = require('../../ports-map').SockJSServer; describe('SockJSServer', () => { let socketServer; @@ -15,7 +16,7 @@ describe('SockJSServer', () => { const app = new express(); listeningApp = http.createServer(app); - listeningApp.listen(8080, 'localhost', () => { + listeningApp.listen(port, 'localhost', () => { const server = { log: { error: () => {}, @@ -44,7 +45,7 @@ describe('SockJSServer', () => { socketServer.close(connection); }); - const client = new SockJS('http://localhost:8080/sockjs-node'); + const client = new SockJS(`http://localhost:${port}/sockjs-node`); client.onmessage = (e) => { data.push(e.data); diff --git a/test/server/sockPath-option.test.js b/test/server/sockPath-option.test.js index b3a5566c8d..860e3818be 100644 --- a/test/server/sockPath-option.test.js +++ b/test/server/sockPath-option.test.js @@ -3,6 +3,7 @@ const request = require('supertest'); const config = require('../fixtures/simple-config/webpack.config'); const testServer = require('../helpers/test-server'); +const port = require('../ports-map')['sockPath-option']; describe('sockPath options', () => { let server; @@ -16,8 +17,8 @@ describe('sockPath options', () => { describe('default behavior', () => { beforeEach((done) => { - server = testServer.start(config, {}, done); - req = request('http://localhost:8080'); + server = testServer.start(config, { port }, done); + req = request(`http://localhost:${port}`); }); it('defaults to a path', () => { @@ -37,10 +38,11 @@ describe('sockPath options', () => { config, { sockPath: '/foo/test/bar/', + port, }, done ); - req = request('http://localhost:8080'); + req = request(`http://localhost:${port}`); }); it('sets the sock path correctly and strips leading and trailing /s', () => { diff --git a/test/server/stats-option.test.js b/test/server/stats-option.test.js index ac996ca703..45e7cc3035 100644 --- a/test/server/stats-option.test.js +++ b/test/server/stats-option.test.js @@ -3,6 +3,7 @@ const webpack = require('webpack'); const Server = require('../../lib/Server'); const config = require('../fixtures/simple-config/webpack.config'); +const port = require('../ports-map')['stats-option']; describe('stats option', () => { it(`should works with difference stats values (contains 'hash', 'assets', 'warnings' and 'errors')`, async () => { @@ -19,7 +20,7 @@ describe('stats option', () => { for (const stats of allStats) { const compiler = webpack(config); - const server = new Server(compiler, { stats }); + const server = new Server(compiler, { stats, port }); // eslint-disable-next-line no-await-in-loop await new Promise((resolve) => { @@ -30,7 +31,7 @@ describe('stats option', () => { }); compiler.run(() => {}); - server.listen(8080, 'localhost'); + server.listen(port, 'localhost'); }); } }); @@ -39,6 +40,7 @@ describe('stats option', () => { const compiler = webpack(config); const server = new Server(compiler, { stats: { warningsFilter: 'test' }, + port, }); compiler.hooks.done.tap('webpack-dev-server', (s) => { @@ -53,6 +55,6 @@ describe('stats option', () => { }); compiler.run(() => {}); - server.listen(8080, 'localhost'); + server.listen(port, 'localhost'); }); }); diff --git a/test/server/utils/createDomain.test.js b/test/server/utils/createDomain.test.js index 08b839c585..07a755859c 100644 --- a/test/server/utils/createDomain.test.js +++ b/test/server/utils/createDomain.test.js @@ -4,6 +4,7 @@ const webpack = require('webpack'); const internalIp = require('internal-ip'); const Server = require('../../../lib/Server'); const createDomain = require('../../../lib/utils/createDomain'); +const [port1, port2] = require('../../ports-map').createDomain; const config = require('./../../fixtures/simple-config/webpack.config'); describe('check utility functions', () => { @@ -15,9 +16,7 @@ describe('check utility functions', () => { }); afterEach((done) => { - server.close(() => { - done(); - }); + server.close(done); }); const tests = [ @@ -25,32 +24,32 @@ describe('check utility functions', () => { name: 'default', options: { host: 'localhost', - port: 8080, + port: port1, }, - expected: 'http://localhost:8080', + expected: `http://localhost:${port1}`, }, { name: 'no host option', options: { - port: 8080, + port: port1, }, - expected: 'http://localhost:8080', + expected: `http://localhost:${port1}`, }, { name: 'https', options: { host: 'localhost', - port: 8080, + port: port1, https: true, }, - expected: 'https://localhost:8080', + expected: `https://localhost:${port1}`, timeout: 60000, }, { name: 'override with public', options: { host: 'localhost', - port: 8080, + port: port1, public: 'myhost.test', }, expected: 'http://myhost.test', @@ -59,16 +58,16 @@ describe('check utility functions', () => { name: 'override with public (port)', options: { host: 'localhost', - port: 8080, - public: 'myhost.test:9090', + port: port1, + public: `myhost.test:${port2}`, }, - expected: 'http://myhost.test:9090', + expected: `http://myhost.test:${port2}`, }, { name: 'override with public (protocol)', options: { host: 'localhost', - port: 8080, + port: port1, public: 'https://myhost.test', }, expected: 'https://myhost.test', @@ -77,18 +76,18 @@ describe('check utility functions', () => { name: 'override with public (protocol + port)', options: { host: 'localhost', - port: 8080, - public: 'https://myhost.test:9090', + port: port1, + public: `https://myhost.test:${port2}`, }, - expected: 'https://myhost.test:9090', + expected: `https://myhost.test:${port2}`, }, { name: 'localIp', options: { useLocalIp: true, - port: 8080, + port: port1, }, - expected: `http://${internalIp.v4.sync() || 'localhost'}:8080`, + expected: `http://${internalIp.v4.sync() || 'localhost'}:${port1}`, }, ]; diff --git a/test/server/utils/routes.test.js b/test/server/utils/routes.test.js index 78d1c8b0eb..27115a9d4b 100644 --- a/test/server/utils/routes.test.js +++ b/test/server/utils/routes.test.js @@ -3,13 +3,14 @@ const request = require('supertest'); const testServer = require('../../helpers/test-server'); const config = require('../../fixtures/simple-config/webpack.config'); +const port = require('../../ports-map').routes; describe('routes util', () => { let server; let req; beforeAll((done) => { - server = testServer.startAwaitingCompilation(config, {}, done); + server = testServer.startAwaitingCompilation(config, { port }, done); req = request(server.app); });