Skip to content

Commit 48fa80e

Browse files
authored
test: run tests on WDS v4 (#513)
1 parent ba15023 commit 48fa80e

File tree

9 files changed

+708
-474
lines changed

9 files changed

+708
-474
lines changed

.circleci/config.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ anchors:
55
- '10.24'
66
- '12.22'
77
- '14.17'
8-
- '16.4'
8+
- '16.8'
99
- &webpack-version-enum
1010
- '4'
1111
- '5'
@@ -32,7 +32,17 @@ commands:
3232
- node-deps-v1-<< parameters.node-version >>-
3333
- run:
3434
name: Install project dependencies
35-
command: yarn install --cache-folder ~/.cache/yarn --frozen-lockfile
35+
command: |-
36+
# Ignore engines mismatch for Node.js v10.x
37+
extra_args=""
38+
if [[ "<< parameters.node-version >>" == 10* ]]; then
39+
extra_args+="--ignore-engines"
40+
fi
41+
42+
yarn install \
43+
--cache-folder ~/.cache/yarn \
44+
--frozen-lockfile \
45+
$extra_args
3646
- save_cache:
3747
key: node-deps-v1-<< parameters.node-version >>-{{ .Branch }}-{{ checksum "yarn.lock" }}
3848
paths:

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
},
3232
"globals": {
3333
"__DEBUG__": true,
34+
"WDS_VERSION": true,
3435
"WEBPACK_VERSION": true,
3536
"browser": true
3637
},

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,15 @@
9797
"prettier": "^2.3.0",
9898
"puppeteer": "^9.1.1",
9999
"react-refresh": "^0.10.0",
100+
"semver": "^7.3.5",
100101
"sourcemap-validator": "^2.1.0",
101102
"type-fest": "^1.4.0",
102103
"typescript": "4.4.3",
103104
"webpack": "^5.42.0",
104105
"webpack-cli": "^4.7.2",
105106
"webpack-cli.legacy": "npm:[email protected]",
106-
"webpack-dev-server": "^3.11.2",
107+
"webpack-dev-server": "^4.2.1",
108+
"webpack-dev-server.legacy": "npm:[email protected]",
107109
"webpack-hot-middleware": "^2.25.0",
108110
"webpack-plugin-serve": "^1.4.1",
109111
"webpack.legacy": "npm:[email protected]",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const moduleAlias = require('module-alias');
2+
3+
moduleAlias.addAliases({ 'webpack-dev-server': 'webpack-dev-server.legacy' });

test/helpers/sandbox/configs.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function getPackageJson(esModule = false) {
4040
*/
4141
function getWDSConfig(srcDir) {
4242
return `
43-
const { DefinePlugin } = require('webpack');
43+
const { DefinePlugin, ProgressPlugin } = require('webpack');
4444
const ReactRefreshPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
4545
4646
module.exports = {
@@ -72,12 +72,22 @@ module.exports = {
7272
},
7373
plugins: [
7474
new DefinePlugin({ __react_refresh_test__: true }),
75+
new ProgressPlugin((percentage) => {
76+
if (percentage === 1) {
77+
console.log("Webpack compilation complete.");
78+
}
79+
}),
7580
new ReactRefreshPlugin(),
7681
],
7782
resolve: {
78-
alias: {
79-
webpack: '${WEBPACK_VERSION === 4 ? 'webpack.legacy' : 'webpack'}'
80-
},
83+
alias: ${JSON.stringify(
84+
{
85+
...(WEBPACK_VERSION === 4 && { webpack: 'webpack.legacy' }),
86+
...(WDS_VERSION === 3 && { 'webpack-dev-server': 'webpack-dev-server.legacy' }),
87+
},
88+
null,
89+
2
90+
)},
8191
extensions: ['.js', '.jsx'],
8292
},
8393
};

test/helpers/sandbox/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const { getIndexHTML, getPackageJson, getWDSConfig } = require('./configs');
66
const { killTestProcess, spawnWebpackServe } = require('./spawn');
77

88
// Extends the timeout for tests using the sandbox
9-
jest.setTimeout(1000 * 60 * 5);
9+
jest.setTimeout(1000 * 60);
1010

1111
// Setup a global "queue" of cleanup handlers to allow auto-teardown of tests,
1212
// even when they did not run the cleanup function.
@@ -69,15 +69,18 @@ async function getSandbox({ esModule = false, id = nanoid(), initialFiles = new
6969
// Get sandbox directory paths
7070
const sandboxDir = path.join(rootSandboxDir, id);
7171
const srcDir = path.join(sandboxDir, 'src');
72+
const publicDir = path.join(sandboxDir, 'public');
7273
// In case of an ID clash, remove the existing sandbox directory
7374
await fse.remove(sandboxDir);
7475
// Create the sandbox source directory
7576
await fse.mkdirp(srcDir);
77+
// Create the sandbox public directory
78+
await fse.mkdirp(publicDir);
7679

7780
// Write necessary files to sandbox
78-
await fse.writeFile(path.join(srcDir, 'package.json'), getPackageJson(esModule));
7981
await fse.writeFile(path.join(sandboxDir, 'webpack.config.js'), getWDSConfig(srcDir));
80-
await fse.writeFile(path.join(sandboxDir, 'index.html'), getIndexHTML(port));
82+
await fse.writeFile(path.join(publicDir, 'index.html'), getIndexHTML(port));
83+
await fse.writeFile(path.join(srcDir, 'package.json'), getPackageJson(esModule));
8184
await fse.writeFile(
8285
path.join(srcDir, 'index.js'),
8386
esModule
@@ -91,7 +94,7 @@ async function getSandbox({ esModule = false, id = nanoid(), initialFiles = new
9194
}
9295

9396
// TODO: Add handling for webpack-hot-middleware and webpack-plugin-serve
94-
const app = await spawnWebpackServe(port, sandboxDir);
97+
const app = await spawnWebpackServe(port, { public: publicDir, root: sandboxDir, src: srcDir });
9598
/** @type {import('puppeteer').Page} */
9699
const page = await browser.newPage();
97100

test/helpers/sandbox/spawn.js

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function spawnTestProcess(processPath, argv, options = {}) {
6565
NODE_ENV: 'development',
6666
...options.env,
6767
};
68-
const successRegex = new RegExp(options.successMessage || 'compiled successfully', 'i');
68+
const successRegex = new RegExp(options.successMessage || 'webpack compilation complete.', 'i');
6969

7070
return new Promise((resolve, reject) => {
7171
const instance = spawn(processPath, argv, { cwd, env });
@@ -95,7 +95,10 @@ function spawnTestProcess(processPath, argv, options = {}) {
9595
*/
9696
function handleStderr(data) {
9797
const message = data.toString();
98-
process.stderr.write(message);
98+
99+
if (__DEBUG__) {
100+
process.stderr.write(message);
101+
}
99102
}
100103

101104
instance.stdout.on('data', handleStdout);
@@ -119,35 +122,45 @@ function spawnTestProcess(processPath, argv, options = {}) {
119122

120123
/**
121124
* @param {number} port
122-
* @param {string} directory
125+
* @param {Object} dirs
126+
* @param {string} dirs.public
127+
* @param {string} dirs.root
128+
* @param {string} dirs.src
123129
* @param {SpawnOptions} [options]
124130
* @returns {Promise<import('child_process').ChildProcess | void>}
125131
*/
126-
function spawnWebpackServe(port, directory, options = {}) {
132+
function spawnWebpackServe(port, dirs, options = {}) {
127133
const webpackBin = getPackageExecutable('webpack-cli');
134+
135+
const NODE_OPTIONS = [
136+
// This requires a script to alias `webpack` and `webpack-cli` -
137+
// both v4 and v5 is installed side by side,
138+
// so we have to ensure that they resolve to the `legacy` variant.
139+
WEBPACK_VERSION === 4 && `--require "${require.resolve('./aliasLegacyWebpack')}"`,
140+
// This requires a script to alias `webpack-dev-server` for Node.js v10.x -
141+
// both v3 and v4 is installed side by side,
142+
// so we have to ensure that it resolves to the `legacy` variant.
143+
WDS_VERSION === 3 && `--require "${require.resolve('./aliasLegacyWebpackDevServer')}"`,
144+
]
145+
.filter(Boolean)
146+
.join(' ');
147+
128148
return spawnTestProcess(
129149
webpackBin,
130150
[
131151
'serve',
152+
'--no-color',
132153
'--config',
133-
path.resolve(directory, 'webpack.config.js'),
134-
'--content-base',
135-
directory,
154+
path.join(dirs.root, 'webpack.config.js'),
155+
WDS_VERSION === 3 ? '--content-base' : '--static-directory',
156+
dirs.public,
136157
'--hot',
137158
'--port',
138159
port,
139160
],
140161
{
141162
...options,
142-
env: {
143-
...options.env,
144-
...(WEBPACK_VERSION === 4 && {
145-
// This requires a script to alias `webpack` and `webpack-cli` -
146-
// both v4 and v5 is installed side by side,
147-
// so we have to ensure that they resolve to the `legacy` variant.
148-
NODE_OPTIONS: `--require "${require.resolve('./aliasLegacyWebpack')}"`,
149-
}),
150-
},
163+
env: { ...options.env, ...(NODE_OPTIONS && { NODE_OPTIONS }) },
151164
}
152165
);
153166
}

test/jest-environment.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const NodeEnvironment = require('jest-environment-node');
22
const puppeteer = require('puppeteer');
3+
const semver = require('semver');
34
const yn = require('yn');
45

56
class SandboxEnvironment extends NodeEnvironment {
@@ -8,6 +9,7 @@ class SandboxEnvironment extends NodeEnvironment {
89

910
this.global.__DEBUG__ = yn(process.env.DEBUG);
1011
this.global.WEBPACK_VERSION = parseInt(process.env.WEBPACK_VERSION || '5', 10);
12+
this.global.WDS_VERSION = semver.major(process.version) < 12 ? 3 : 4;
1113

1214
const wsEndpoint = process.env.PUPPETEER_WS_ENDPOINT;
1315
if (!wsEndpoint) {

0 commit comments

Comments
 (0)