Skip to content

Commit 50a9bb0

Browse files
authored
test: don't use --runInBand and improve execution performance (#2005)
1 parent 0e0c340 commit 50a9bb0

38 files changed

+352
-150
lines changed

globalSetupTest.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
// eslint-disable-next-line import/no-extraneous-dependencies
4+
const tcpPortUsed = require('tcp-port-used');
5+
const ports = require('./test/ports-map');
6+
7+
async function validatePorts() {
8+
const samples = [];
9+
10+
Object.entries(ports).forEach(([key, value]) => {
11+
const arr = Array.isArray(value) ? value : [value];
12+
13+
arr.forEach((port) => {
14+
const check = tcpPortUsed.check(port, 'localhost').then((inUse) => {
15+
if (inUse) throw new Error(`${port} has already used. [${key}]`);
16+
});
17+
18+
samples.push(check);
19+
});
20+
});
21+
22+
try {
23+
await Promise.all(samples);
24+
} catch (e) {
25+
// eslint-disable-next-line no-console
26+
console.error(e);
27+
process.exit(1);
28+
}
29+
}
30+
31+
module.exports = validatePorts;

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ module.exports = {
77
moduleFileExtensions: ['js', 'json'],
88
testMatch: ['**/test/**/*.test.js'],
99
setupFilesAfterEnv: ['<rootDir>/setupTest.js'],
10+
globalSetup: '<rootDir>/globalSetupTest.js',
1011
};

package-lock.json

Lines changed: 55 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"lint": "npm-run-all -l -p \"lint:**\"",
2020
"commitlint": "commitlint --from=master",
2121
"security": "npm audit",
22-
"test:only": "jest --runInBand",
22+
"test:only": "jest",
2323
"test:coverage": "npm run test:only -- --coverage",
2424
"test:watch": "npm run test:coverage --watch",
2525
"test": "npm run test:coverage",
@@ -103,6 +103,7 @@
103103
"standard-version": "^6.0.1",
104104
"style-loader": "^0.23.1",
105105
"supertest": "^4.0.2",
106+
"tcp-port-used": "^1.0.1",
106107
"url-loader": "^2.0.0",
107108
"webpack": "^4.33.0",
108109
"webpack-cli": "^3.3.3",

test/cli/cli.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,10 @@ describe('CLI', () => {
128128
const cliPath = resolve(__dirname, '../../bin/webpack-dev-server.js');
129129
const examplePath = resolve(__dirname, '../../examples/cli/public');
130130

131-
const cp = execa('node', [cliPath], { cwd: examplePath });
132-
const cp2 = execa('node', [cliPath], { cwd: examplePath });
131+
const cp = execa('node', [cliPath, '--colors=false'], { cwd: examplePath });
132+
const cp2 = execa('node', [cliPath, '--colors=false'], {
133+
cwd: examplePath,
134+
});
133135

134136
const runtime = {
135137
cp: {

test/client/clients/SockJSClient.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const express = require('express');
55
const sockjs = require('sockjs');
66
const SockJSClient = require('../../../client-src/clients/SockJSClient');
77
const timer = require('../../helpers/timer');
8+
const port = require('../../ports-map').sockJSClient;
89

910
describe('SockJSClient', () => {
1011
let socketServer;
@@ -15,7 +16,7 @@ describe('SockJSClient', () => {
1516
const app = new express();
1617

1718
listeningApp = http.createServer(app);
18-
listeningApp.listen(8080, 'localhost', () => {
19+
listeningApp.listen(port, 'localhost', () => {
1920
socketServer = sockjs.createServer();
2021
socketServer.installHandlers(listeningApp, {
2122
prefix: '/sockjs-node',
@@ -33,7 +34,7 @@ describe('SockJSClient', () => {
3334
connection.close();
3435
});
3536

36-
const client = new SockJSClient('http://localhost:8080/sockjs-node');
37+
const client = new SockJSClient(`http://localhost:${port}/sockjs-node`);
3738
const data = [];
3839

3940
client.onOpen(() => {

test/e2e/Client.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const reloadConfig = require('../fixtures/reload-config/webpack.config');
99
const { writeAsync } = require('../helpers/fs');
1010
const testServer = require('../helpers/test-server');
1111
const runBrowser = require('../helpers/run-browser');
12+
const port = require('../ports-map').Client;
1213

1314
const cssFilePath = resolve(__dirname, '../fixtures/reload-config/main.css');
1415

@@ -20,7 +21,7 @@ describe('reload', () => {
2021
'body { background-color: rgb(0, 0, 255); }'
2122
);
2223
const options = {
23-
port: 9000,
24+
port,
2425
host: '0.0.0.0',
2526
inline: true,
2627
hot: true,
@@ -41,7 +42,7 @@ describe('reload', () => {
4142
const { page, browser } = await runBrowser();
4243
let refreshed = false;
4344

44-
page.goto('http://localhost:9000/main');
45+
page.goto(`http://localhost:${port}/main`);
4546
await page.waitForNavigation({ waitUntil: 'load' });
4647

4748
{
@@ -59,7 +60,7 @@ describe('reload', () => {
5960
if (
6061
req.isNavigationRequest() &&
6162
req.frame() === page.mainFrame() &&
62-
req.url() === 'http://localhost:9000/main'
63+
req.url() === `http://localhost:${port}/main`
6364
) {
6465
refreshed = true;
6566
}

0 commit comments

Comments
 (0)