webpack/test/Cli.basictest.js

420 lines
8.6 KiB
JavaScript
Raw Normal View History

"use strict";
2020-03-10 20:30:18 +08:00
const { getArguments, processArguments } = require("../").cli;
2020-03-09 21:18:34 +08:00
describe("Cli", () => {
it("should generate the correct cli flags", () => {
2020-03-10 20:30:18 +08:00
expect(getArguments()).toMatchSnapshot();
2020-03-09 21:18:34 +08:00
});
2020-03-10 20:30:18 +08:00
2021-12-12 17:36:44 +08:00
it("should generate the correct cli flags with custom schema", () => {
const schema = {
title: "custom CLI options",
type: "object",
additionalProperties: false,
properties: {
2022-01-14 20:19:48 +08:00
"with-reset-description": {
type: "array",
items: {
type: "string"
},
description: "original description",
cli: {
resetDescription: "custom reset"
}
},
2021-12-12 17:36:44 +08:00
"with-cli-description": {
type: "string",
description: "original description",
2022-01-14 20:19:48 +08:00
cli: {
description: "description for CLI option"
}
2021-12-12 17:36:44 +08:00
},
"with-negative-description": {
type: "boolean",
description: "original description",
2022-01-14 20:19:48 +08:00
cli: {
negatedDescription: "custom negative description"
}
2021-12-12 17:36:44 +08:00
},
"with-both-cli-and-negative-description": {
type: "boolean",
description: "original description",
2022-01-14 20:19:48 +08:00
cli: {
description: "description for CLI option",
negatedDescription: "custom negative description"
}
2021-12-12 17:36:44 +08:00
}
}
};
expect(getArguments(schema)).toMatchSnapshot();
});
2020-03-10 20:30:18 +08:00
const test = (name, values, config, fn) => {
it(`should correctly process arguments for ${name}`, () => {
const args = getArguments();
const problems = processArguments(args, config, values);
fn(expect(problems || config));
});
};
test("none", {}, {}, (e) => e.toMatchInlineSnapshot("Object {}"));
2020-03-10 20:30:18 +08:00
test("root boolean", { bail: true }, {}, (e) =>
2020-03-10 20:30:18 +08:00
e.toMatchInlineSnapshot(`
Object {
"bail": true,
}
`)
);
test("root single item of multiple", { entry: "./a.js" }, {}, (e) =>
2020-03-10 20:30:18 +08:00
e.toMatchInlineSnapshot(`
Object {
"entry": Array [
"./a.js",
],
}
`)
);
test(
"root single item of multiple with existing item",
{ entry: "./a.js" },
{ entry: "./old.js" },
(e) =>
2020-03-10 20:30:18 +08:00
e.toMatchInlineSnapshot(`
Object {
"entry": Array [
"./old.js",
"./a.js",
],
}
`)
);
test(
"root single item of multiple with existing items",
{ entry: "./a.js" },
{ entry: ["./old1.js", "./old2.js"] },
(e) =>
2020-03-10 20:30:18 +08:00
e.toMatchInlineSnapshot(`
Object {
"entry": Array [
"./old1.js",
"./old2.js",
"./a.js",
],
}
`)
);
test("root multiple items", { entry: ["./a.js", "./b.js"] }, {}, (e) =>
2020-03-10 20:30:18 +08:00
e.toMatchInlineSnapshot(`
Object {
"entry": Array [
"./a.js",
"./b.js",
],
}
`)
);
test(
"root multiple items with existing item",
{ entry: ["./a.js", "./b.js"] },
{ entry: "./old.js" },
(e) =>
2020-03-10 20:30:18 +08:00
e.toMatchInlineSnapshot(`
Object {
"entry": Array [
"./old.js",
"./a.js",
"./b.js",
],
}
`)
);
test(
"root multiple items with existing items",
{ entry: ["./a.js", "./b.js"] },
{ entry: ["./old1.js", "./old2.js"] },
(e) =>
2020-03-10 20:30:18 +08:00
e.toMatchInlineSnapshot(`
Object {
"entry": Array [
"./old1.js",
"./old2.js",
"./a.js",
"./b.js",
],
}
`)
);
test("nested boolean", { "experiments-top-level-await": true }, {}, (e) =>
2020-03-10 20:30:18 +08:00
e.toMatchInlineSnapshot(`
Object {
"experiments": Object {
"topLevelAwait": true,
},
}
`)
);
test(
"nested regexp",
{ "stats-warnings-filter": ["/module/", "path"] },
{},
(e) =>
2020-03-10 20:30:18 +08:00
e.toMatchInlineSnapshot(`
2020-07-07 23:19:23 +08:00
Object {
"stats": Object {
"warningsFilter": Array [
/module/,
"path",
],
},
}
`)
2020-03-10 20:30:18 +08:00
);
test(
"nested multiple",
{
"module-rules-test": ["/\\.css$/", "/\\.js$/"],
"module-rules-use": ["css-loader", "babel-loader"]
},
{},
(e) =>
2020-03-10 20:30:18 +08:00
e.toMatchInlineSnapshot(`
2020-07-07 23:19:23 +08:00
Object {
"module": Object {
"rules": Array [
Object {
"test": /\\\\\\.css\\$/,
"use": "css-loader",
},
Object {
"test": /\\\\\\.js\\$/,
"use": "babel-loader",
},
],
},
}
`)
);
test(
"reset array",
{
"stats-warnings-filter-reset": true,
"stats-warnings-filter": "path",
"module-rules-reset": true,
"module-rules-test": ["/\\.css$/", "/\\.js$/"],
"module-rules-use": ["css-loader", "babel-loader"]
},
{
stats: { warningsFilter: [/a/, /b/] },
module: {
rules: [
{
test: /\.js$/,
use: "typescript-loader"
}
]
}
},
(e) =>
e.toMatchInlineSnapshot(`
2020-07-07 23:19:23 +08:00
Object {
"module": Object {
"rules": Array [
Object {
"test": /\\\\\\.css\\$/,
"use": "css-loader",
},
Object {
"test": /\\\\\\.js\\$/,
"use": "babel-loader",
},
],
},
"stats": Object {
"warningsFilter": Array [
"path",
],
},
}
`)
2020-03-26 23:41:50 +08:00
);
test(
"numbers",
{
"watch-options-aggregate-timeout": 100,
"watch-options-poll": "100",
"output-chunk-load-timeout": "20000"
2020-03-26 23:41:50 +08:00
},
{},
(e) =>
2020-03-26 23:41:50 +08:00
e.toMatchInlineSnapshot(`
2020-07-07 23:19:23 +08:00
Object {
"output": Object {
"chunkLoadTimeout": 20000,
2020-07-07 23:19:23 +08:00
},
"watchOptions": Object {
"aggregateTimeout": 100,
"poll": 100,
2020-07-07 23:19:23 +08:00
},
}
`)
2020-03-26 23:41:50 +08:00
);
test(
"booleans and enums",
{
"optimization-used-exports": true,
"output-compare-before-emit": false,
"output-iife": "true",
"output-library-name": ["hello", "world"],
"output-library-umd-named-define": "false",
"stats-logging": "verbose",
amd: "false"
},
{},
(e) =>
2020-03-26 23:41:50 +08:00
e.toMatchInlineSnapshot(`
2020-07-07 23:19:23 +08:00
Object {
"amd": false,
"optimization": Object {
"usedExports": true,
},
"output": Object {
"compareBeforeEmit": false,
"iife": true,
"library": Object {
"name": Array [
"hello",
"world",
],
"umdNamedDefine": false,
},
},
"stats": Object {
"logging": "verbose",
},
}
`)
2020-03-26 23:41:50 +08:00
);
// cspell:ignore filsystem
2020-03-26 23:41:50 +08:00
test(
"errors",
{
"output-library-name": "non-object",
"resolve-loader-unsafe-cache": [true, false],
"output-chunk-load-timeout": "20000x",
2020-03-26 23:41:50 +08:00
"cache-type": "filsystem",
"entry-reset": false,
"module-unknown-context-reg-exp": "ab?c*",
"module-wrapped-context-reg-exp": 123,
"my-argument": true
},
{
output: {
library: "hello"
}
},
(e) =>
2020-03-26 23:41:50 +08:00
e.toMatchInlineSnapshot(`
2020-07-07 23:19:23 +08:00
Array [
Object {
"argument": "output-library-name",
"index": undefined,
"path": "output",
"type": "unexpected-non-object-in-path",
"value": "non-object",
},
Object {
"argument": "resolve-loader-unsafe-cache",
"index": 0,
"path": "resolveLoader.unsafeCache",
"type": "multiple-values-unexpected",
"value": true,
},
Object {
"argument": "resolve-loader-unsafe-cache",
"index": 1,
"path": "resolveLoader.unsafeCache",
"type": "multiple-values-unexpected",
"value": false,
},
Object {
"argument": "output-chunk-load-timeout",
2020-07-07 23:19:23 +08:00
"expected": "number",
"index": undefined,
"path": "output.chunkLoadTimeout",
2020-07-07 23:19:23 +08:00
"type": "invalid-value",
"value": "20000x",
2020-07-07 23:19:23 +08:00
},
Object {
"argument": "cache-type",
"expected": "memory",
"index": undefined,
"path": "cache.type",
"type": "invalid-value",
"value": "filsystem",
},
Object {
"argument": "cache-type",
"expected": "filesystem",
"index": undefined,
"path": "cache.type",
"type": "invalid-value",
"value": "filsystem",
},
Object {
"argument": "entry-reset",
"expected": "true (will reset the previous value to an empty array)",
"index": undefined,
"path": "entry",
"type": "invalid-value",
"value": false,
},
Object {
"argument": "module-unknown-context-reg-exp",
"expected": "regular expression (example: /ab?c*/)",
"index": undefined,
"path": "module.unknownContextRegExp",
"type": "invalid-value",
"value": "ab?c*",
},
Object {
"argument": "module-unknown-context-reg-exp",
"expected": "true | false",
"index": undefined,
"path": "module.unknownContextRegExp",
"type": "invalid-value",
"value": "ab?c*",
},
Object {
"argument": "module-wrapped-context-reg-exp",
"expected": "regular expression (example: /ab?c*/)",
"index": undefined,
"path": "module.wrappedContextRegExp",
"type": "invalid-value",
"value": 123,
},
Object {
"argument": "my-argument",
"path": "",
"type": "unknown-argument",
},
]
`)
2020-03-10 20:30:18 +08:00
);
2020-03-09 21:18:34 +08:00
});