Skip to content

Commit

Permalink
test: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Aug 16, 2021
1 parent feedeab commit aeb65dc
Show file tree
Hide file tree
Showing 22 changed files with 97 additions and 67 deletions.
65 changes: 29 additions & 36 deletions test/cli/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,40 +66,33 @@ describe("basic", () => {
});

it("should accept the promise function of webpack.config.js", async () => {
try {
await testBin([
"--config",
path.resolve(
__dirname,
"../fixtures/promise-config/webpack.config.js"
),
"--port",
port,
]);
} catch (error) {
expect(error.stdout).toContain("main.js");
expect(normalizeStderr(error.stderr, { ipv6: true })).toMatchSnapshot(
"stderr"
);
}
const { exitCode, stderr } = await testBin([
"--config",
path.resolve(
__dirname,
"../fixtures/cli-promise-config/webpack.config.js"
),
"--port",
port,
]);

expect(exitCode).toEqual(0);
expect(normalizeStderr(stderr, { ipv6: true })).toMatchSnapshot("stderr");
});

it("should work using multi compiler mode", async () => {
try {
await testBin([
"--config",
path.resolve(
__dirname,
"../fixtures/universal-compiler-config/webpack.config.js"
),
"--port",
port,
]);
} catch (error) {
expect(normalizeStderr(error.stderr, { ipv6: true })).toMatchSnapshot(
"stderr"
);
}
const { exitCode, stderr } = await testBin([
"--config",
path.resolve(
__dirname,
"../fixtures/cli-universal-compiler-config/webpack.config.js"
),
"--port",
port,
]);

expect(exitCode).toEqual(0);
expect(normalizeStderr(stderr, { ipv6: true })).toMatchSnapshot("stderr");
});

it("should exit the process when SIGINT is detected", (done) => {
Expand Down Expand Up @@ -229,7 +222,7 @@ describe("basic", () => {
"--port",
port,
"--config",
"./test/fixtures/dev-server/default-config.js",
"./test/fixtures/cli-single-entry/webpack.config.js",
]);

expect(exitCode).toEqual(0);
Expand All @@ -243,7 +236,7 @@ describe("basic", () => {
"--port",
port,
"--config",
"./test/fixtures/dev-server/multi-entry.js",
"./test/fixtures/cli-multi-entry/webpack.config.js",
"--stats",
"verbose",
]);
Expand All @@ -261,7 +254,7 @@ describe("basic", () => {
"--port",
port,
"--config",
"./test/fixtures/dev-server/empty-entry.js",
"./test/fixtures/cli-empty-entry/webpack.config.js",
]);

expect(exitCode).toEqual(0);
Expand All @@ -274,7 +267,7 @@ describe("basic", () => {
"--port",
port,
"--config",
"./test/fixtures/entry-as-descriptor/webpack.config",
"./test/fixtures/cli-entry-as-descriptor/webpack.config",
"--stats",
"detailed",
]);
Expand Down Expand Up @@ -329,7 +322,7 @@ describe("basic", () => {
"--port",
port,
"--config",
"./test/fixtures/dev-server/target-config.js",
"./test/fixtures/cli-target-config/webpack.config.js",
]);

expect(exitCode).toEqual(0);
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/cli-colors-default-stats/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ module.exports = {
mode: "development",
context: __dirname,
entry: "./foo.js",
plugins: [ExitOnDonePlugin],
plugins: [new ExitOnDonePlugin()],
};
2 changes: 1 addition & 1 deletion test/fixtures/cli-colors-disabled/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ module.exports = {
},
context: __dirname,
entry: "./foo.js",
plugins: [ExitOnDonePlugin],
plugins: [new ExitOnDonePlugin()],
};
2 changes: 1 addition & 1 deletion test/fixtures/cli-colors-enabled/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ module.exports = {
},
context: __dirname,
entry: "./foo.js",
plugins: [ExitOnDonePlugin],
plugins: [new ExitOnDonePlugin()],
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@ module.exports = {
mode: "development",
stats: { orphanModules: true, preset: "detailed" },
entry: {},
devServer: {
webSocketServer: "ws",
},
plugins: [ExitOnDonePlugin],
plugins: [new ExitOnDonePlugin()],
};
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,5 @@ module.exports = {
import: "./foo.js",
},
},
plugins: [ExitOnDonePlugin],
infrastructureLogging: {
level: "warn",
},
plugins: [new ExitOnDonePlugin()],
};
3 changes: 3 additions & 0 deletions test/fixtures/cli-multi-entry/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

console.log("I am bar");
3 changes: 3 additions & 0 deletions test/fixtures/cli-multi-entry/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

console.log("I am foo");
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,5 @@ module.exports = {
foo: resolve(__dirname, "./foo.js"),
bar: resolve(__dirname, "./bar.js"),
},
devServer: {
webSocketServer: "ws",
},
plugins: [ExitOnDonePlugin],
plugins: [new ExitOnDonePlugin()],
};
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"use strict";

const { join } = require("path");
const ExitOnDonePlugin = require("../../helpers/ExitOnDonePlugin");

module.exports = () =>
new Promise((resolve) => {
resolve({
mode: "development",
entry: join(__dirname, "foo.js"),
plugins: [new ExitOnDonePlugin()],
});
});
3 changes: 3 additions & 0 deletions test/fixtures/cli-single-entry/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

console.log("I am foo");
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,5 @@ module.exports = {
mode: "development",
stats: "detailed",
entry: resolve(__dirname, "./foo.js"),
devServer: {
webSocketServer: "ws",
},
plugins: [ExitOnDonePlugin],
plugins: [new ExitOnDonePlugin()],
};
3 changes: 3 additions & 0 deletions test/fixtures/cli-target-config/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

console.log("I am foo");
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,5 @@ module.exports = {
stats: "detailed",
entry: resolve(__dirname, "./foo.js"),
target: ["web"],
output: {
chunkLoading: false,
wasmLoading: false,
workerChunkLoading: false,
},
devServer: {
webSocketServer: "ws",
},
plugins: [ExitOnDonePlugin],
plugins: [new ExitOnDonePlugin()],
};
3 changes: 3 additions & 0 deletions test/fixtures/cli-universal-compiler-config/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

console.log("Hello from the client");
3 changes: 3 additions & 0 deletions test/fixtures/cli-universal-compiler-config/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

console.log("Hello from the server");
28 changes: 28 additions & 0 deletions test/fixtures/cli-universal-compiler-config/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"use strict";

const ExitOnDonePlugin = require("../../helpers/ExitOnDonePlugin");

module.exports = [
{
mode: "development",
context: __dirname,
stats: "none",
entry: "./client.js",
output: {
path: "/",
filename: "client.js",
},
},
{
mode: "development",
context: __dirname,
target: "node",
stats: "none",
entry: "./server.js",
output: {
path: "/",
filename: "server.js",
},
plugins: [new ExitOnDonePlugin()],
},
];
2 changes: 1 addition & 1 deletion test/fixtures/cli/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ module.exports = {
stats: "detailed",
context: __dirname,
entry: "./foo.js",
plugins: [ExitOnDonePlugin],
plugins: [new ExitOnDonePlugin()],
};
6 changes: 6 additions & 0 deletions test/fixtures/universal-compiler-config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module.exports = [
path: "/",
filename: "client.js",
},
infrastructureLogging: {
level: "warn",
},
},
{
mode: "development",
Expand All @@ -21,5 +24,8 @@ module.exports = [
path: "/",
filename: "server.js",
},
infrastructureLogging: {
level: "warn",
},
},
];
7 changes: 5 additions & 2 deletions test/helpers/ExitOnDonePlugin.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
"use strict";

module.exports = {
module.exports = class ExitOnDonePlugin {
// eslint-disable-next-line class-methods-use-this
apply(compiler) {
compiler.hooks.done.tap("webpack-dev-server", (stats) => {
let exitCode = 0;

if (stats.hasErrors()) {
exitCode = 1;
}

setTimeout(() => process.exit(exitCode));
});
},
}
};

0 comments on commit aeb65dc

Please sign in to comment.