Skip to content

Commit

Permalink
test: add case for --target-reset with multiple targets (#3347)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 authored and alexander-akait committed Nov 15, 2022
1 parent a9ce5d0 commit deb8843
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`--target flag should reset target from node to async-node with --target-reset: stderr 1`] = `""`;
exports[`--target flag should reset the \`target\` option when the \`--target-reset\` is used for multiple targets: stderr 1`] = `""`;

exports[`--target flag should reset the \`target\` option when the \`--target-reset\` is used: stderr 1`] = `""`;

exports[`--target flag should throw an error for incompatible multiple targets: stderr 1`] = `
"[webpack-cli] Error: For the selected environment is no default script chunk format available:
Expand Down
8 changes: 8 additions & 0 deletions test/build/target/flag-test/multiple-target.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const WebpackCLITestPlugin = require("../../../utils/webpack-cli-test-plugin");

module.exports = {
entry: "./index.js",
mode: "development",
target: ["web", "es5"],
plugins: [new WebpackCLITestPlugin()],
};
40 changes: 29 additions & 11 deletions test/build/target/flag-test/target-flag.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,42 +91,60 @@ describe("--target flag", () => {
expect(stdout).toContain(`target: [ 'node', 'async-node' ]`);
});

it("should throw an error for invalid target in multiple syntax", async () => {
it("should reset the `target` option when the `--target-reset` is used", async () => {
const { exitCode, stderr, stdout } = await run(__dirname, [
"--config",
"target.web.config.js",
"--target-reset",
"--target",
"node",
]);

expect(exitCode).toBe(0);
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
expect(stdout).toContain(`target: [ 'node' ]`);
});

it("should reset the `target` option when the `--target-reset` is used for multiple targets", async () => {
const { exitCode, stderr, stdout } = await run(__dirname, [
"--config",
"multiple-target.config.js",
"--target-reset",
"--target",
"node",
"--target",
"invalid",
"async-node",
]);

expect(exitCode).toBe(2);
expect(exitCode).toBe(0);
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
expect(normalizeStdout(stdout)).toMatchSnapshot("stdout");
expect(stdout).toContain(`target: [ 'node', 'async-node' ]`);
});

it("should throw an error for incompatible multiple targets", async () => {
it("should throw an error for invalid target in multiple syntax", async () => {
const { exitCode, stderr, stdout } = await run(__dirname, [
"--target",
"node",
"--target",
"web",
"invalid",
]);

expect(exitCode).toBe(2);
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
expect(normalizeStdout(stdout)).toMatchSnapshot("stdout");
});

it("should reset target from node to async-node with --target-reset", async () => {
it("should throw an error for incompatible multiple targets", async () => {
const { exitCode, stderr, stdout } = await run(__dirname, [
"--target-reset",
"--target",
"async-node",
"node",
"--target",
"web",
]);

expect(exitCode).toBe(0);
expect(exitCode).toBe(2);
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
expect(stdout).toContain(`target: [ 'async-node' ]`);
expect(normalizeStdout(stdout)).toMatchSnapshot("stdout");
});

it("should throw error if target is an empty array", async () => {
Expand Down
8 changes: 8 additions & 0 deletions test/build/target/flag-test/target.web.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const WebpackCLITestPlugin = require("../../../utils/webpack-cli-test-plugin");

module.exports = {
entry: "./index.js",
mode: "development",
target: "web",
plugins: [new WebpackCLITestPlugin()],
};

0 comments on commit deb8843

Please sign in to comment.