Skip to content

Commit aeb65dc

Browse files
test: refactor
1 parent feedeab commit aeb65dc

File tree

22 files changed

+97
-67
lines changed

22 files changed

+97
-67
lines changed

test/cli/basic.test.js

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -66,40 +66,33 @@ describe("basic", () => {
6666
});
6767

6868
it("should accept the promise function of webpack.config.js", async () => {
69-
try {
70-
await testBin([
71-
"--config",
72-
path.resolve(
73-
__dirname,
74-
"../fixtures/promise-config/webpack.config.js"
75-
),
76-
"--port",
77-
port,
78-
]);
79-
} catch (error) {
80-
expect(error.stdout).toContain("main.js");
81-
expect(normalizeStderr(error.stderr, { ipv6: true })).toMatchSnapshot(
82-
"stderr"
83-
);
84-
}
69+
const { exitCode, stderr } = await testBin([
70+
"--config",
71+
path.resolve(
72+
__dirname,
73+
"../fixtures/cli-promise-config/webpack.config.js"
74+
),
75+
"--port",
76+
port,
77+
]);
78+
79+
expect(exitCode).toEqual(0);
80+
expect(normalizeStderr(stderr, { ipv6: true })).toMatchSnapshot("stderr");
8581
});
8682

8783
it("should work using multi compiler mode", async () => {
88-
try {
89-
await testBin([
90-
"--config",
91-
path.resolve(
92-
__dirname,
93-
"../fixtures/universal-compiler-config/webpack.config.js"
94-
),
95-
"--port",
96-
port,
97-
]);
98-
} catch (error) {
99-
expect(normalizeStderr(error.stderr, { ipv6: true })).toMatchSnapshot(
100-
"stderr"
101-
);
102-
}
84+
const { exitCode, stderr } = await testBin([
85+
"--config",
86+
path.resolve(
87+
__dirname,
88+
"../fixtures/cli-universal-compiler-config/webpack.config.js"
89+
),
90+
"--port",
91+
port,
92+
]);
93+
94+
expect(exitCode).toEqual(0);
95+
expect(normalizeStderr(stderr, { ipv6: true })).toMatchSnapshot("stderr");
10396
});
10497

10598
it("should exit the process when SIGINT is detected", (done) => {
@@ -229,7 +222,7 @@ describe("basic", () => {
229222
"--port",
230223
port,
231224
"--config",
232-
"./test/fixtures/dev-server/default-config.js",
225+
"./test/fixtures/cli-single-entry/webpack.config.js",
233226
]);
234227

235228
expect(exitCode).toEqual(0);
@@ -243,7 +236,7 @@ describe("basic", () => {
243236
"--port",
244237
port,
245238
"--config",
246-
"./test/fixtures/dev-server/multi-entry.js",
239+
"./test/fixtures/cli-multi-entry/webpack.config.js",
247240
"--stats",
248241
"verbose",
249242
]);
@@ -261,7 +254,7 @@ describe("basic", () => {
261254
"--port",
262255
port,
263256
"--config",
264-
"./test/fixtures/dev-server/empty-entry.js",
257+
"./test/fixtures/cli-empty-entry/webpack.config.js",
265258
]);
266259

267260
expect(exitCode).toEqual(0);
@@ -274,7 +267,7 @@ describe("basic", () => {
274267
"--port",
275268
port,
276269
"--config",
277-
"./test/fixtures/entry-as-descriptor/webpack.config",
270+
"./test/fixtures/cli-entry-as-descriptor/webpack.config",
278271
"--stats",
279272
"detailed",
280273
]);
@@ -329,7 +322,7 @@ describe("basic", () => {
329322
"--port",
330323
port,
331324
"--config",
332-
"./test/fixtures/dev-server/target-config.js",
325+
"./test/fixtures/cli-target-config/webpack.config.js",
333326
]);
334327

335328
expect(exitCode).toEqual(0);

test/fixtures/cli-colors-default-stats/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ module.exports = {
66
mode: "development",
77
context: __dirname,
88
entry: "./foo.js",
9-
plugins: [ExitOnDonePlugin],
9+
plugins: [new ExitOnDonePlugin()],
1010
};

test/fixtures/cli-colors-disabled/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ module.exports = {
99
},
1010
context: __dirname,
1111
entry: "./foo.js",
12-
plugins: [ExitOnDonePlugin],
12+
plugins: [new ExitOnDonePlugin()],
1313
};

test/fixtures/cli-colors-enabled/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ module.exports = {
99
},
1010
context: __dirname,
1111
entry: "./foo.js",
12-
plugins: [ExitOnDonePlugin],
12+
plugins: [new ExitOnDonePlugin()],
1313
};

test/fixtures/dev-server/empty-entry.js renamed to test/fixtures/cli-empty-entry/webpack.config.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,5 @@ module.exports = {
66
mode: "development",
77
stats: { orphanModules: true, preset: "detailed" },
88
entry: {},
9-
devServer: {
10-
webSocketServer: "ws",
11-
},
12-
plugins: [ExitOnDonePlugin],
9+
plugins: [new ExitOnDonePlugin()],
1310
};

test/fixtures/entry-as-descriptor/webpack.config.js renamed to test/fixtures/cli-entry-as-descriptor/webpack.config.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,5 @@ module.exports = {
1010
import: "./foo.js",
1111
},
1212
},
13-
plugins: [ExitOnDonePlugin],
14-
infrastructureLogging: {
15-
level: "warn",
16-
},
13+
plugins: [new ExitOnDonePlugin()],
1714
};

test/fixtures/cli-multi-entry/bar.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
console.log("I am bar");

test/fixtures/cli-multi-entry/foo.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
console.log("I am foo");

test/fixtures/dev-server/multi-entry.js renamed to test/fixtures/cli-multi-entry/webpack.config.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,5 @@ module.exports = {
1111
foo: resolve(__dirname, "./foo.js"),
1212
bar: resolve(__dirname, "./bar.js"),
1313
},
14-
devServer: {
15-
webSocketServer: "ws",
16-
},
17-
plugins: [ExitOnDonePlugin],
14+
plugins: [new ExitOnDonePlugin()],
1815
};
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
"use strict";
22

33
const { join } = require("path");
4+
const ExitOnDonePlugin = require("../../helpers/ExitOnDonePlugin");
45

56
module.exports = () =>
67
new Promise((resolve) => {
78
resolve({
89
mode: "development",
910
entry: join(__dirname, "foo.js"),
11+
plugins: [new ExitOnDonePlugin()],
1012
});
1113
});

test/fixtures/cli-single-entry/foo.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
console.log("I am foo");

test/fixtures/dev-server/default-config.js renamed to test/fixtures/cli-single-entry/webpack.config.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,5 @@ module.exports = {
77
mode: "development",
88
stats: "detailed",
99
entry: resolve(__dirname, "./foo.js"),
10-
devServer: {
11-
webSocketServer: "ws",
12-
},
13-
plugins: [ExitOnDonePlugin],
10+
plugins: [new ExitOnDonePlugin()],
1411
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
console.log("I am foo");

test/fixtures/dev-server/target-config.js renamed to test/fixtures/cli-target-config/webpack.config.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,5 @@ module.exports = {
88
stats: "detailed",
99
entry: resolve(__dirname, "./foo.js"),
1010
target: ["web"],
11-
output: {
12-
chunkLoading: false,
13-
wasmLoading: false,
14-
workerChunkLoading: false,
15-
},
16-
devServer: {
17-
webSocketServer: "ws",
18-
},
19-
plugins: [ExitOnDonePlugin],
11+
plugins: [new ExitOnDonePlugin()],
2012
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
console.log("Hello from the client");
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
console.log("Hello from the server");
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"use strict";
2+
3+
const ExitOnDonePlugin = require("../../helpers/ExitOnDonePlugin");
4+
5+
module.exports = [
6+
{
7+
mode: "development",
8+
context: __dirname,
9+
stats: "none",
10+
entry: "./client.js",
11+
output: {
12+
path: "/",
13+
filename: "client.js",
14+
},
15+
},
16+
{
17+
mode: "development",
18+
context: __dirname,
19+
target: "node",
20+
stats: "none",
21+
entry: "./server.js",
22+
output: {
23+
path: "/",
24+
filename: "server.js",
25+
},
26+
plugins: [new ExitOnDonePlugin()],
27+
},
28+
];

test/fixtures/cli/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ module.exports = {
77
stats: "detailed",
88
context: __dirname,
99
entry: "./foo.js",
10-
plugins: [ExitOnDonePlugin],
10+
plugins: [new ExitOnDonePlugin()],
1111
};

test/fixtures/universal-compiler-config/webpack.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ module.exports = [
1010
path: "/",
1111
filename: "client.js",
1212
},
13+
infrastructureLogging: {
14+
level: "warn",
15+
},
1316
},
1417
{
1518
mode: "development",
@@ -21,5 +24,8 @@ module.exports = [
2124
path: "/",
2225
filename: "server.js",
2326
},
27+
infrastructureLogging: {
28+
level: "warn",
29+
},
2430
},
2531
];

test/helpers/ExitOnDonePlugin.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
"use strict";
22

3-
module.exports = {
3+
module.exports = class ExitOnDonePlugin {
4+
// eslint-disable-next-line class-methods-use-this
45
apply(compiler) {
56
compiler.hooks.done.tap("webpack-dev-server", (stats) => {
67
let exitCode = 0;
8+
79
if (stats.hasErrors()) {
810
exitCode = 1;
911
}
12+
1013
setTimeout(() => process.exit(exitCode));
1114
});
12-
},
15+
}
1316
};

0 commit comments

Comments
 (0)