From bd73ff2128b543f8fe7eb4a93809457a002efa5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juho=20Veps=C3=A4l=C3=A4inen?= Date: Tue, 6 Oct 2020 14:18:43 +0200 Subject: [PATCH] refactor: Separate tests --- test/customize.test.ts | 417 +--------------------------------- test/merge-with-rules.test.ts | 416 +++++++++++++++++++++++++++++++++ 2 files changed, 417 insertions(+), 416 deletions(-) create mode 100644 test/merge-with-rules.test.ts diff --git a/test/customize.test.ts b/test/customize.test.ts index f9539b1..a0fc364 100644 --- a/test/customize.test.ts +++ b/test/customize.test.ts @@ -2,8 +2,7 @@ import assert from "assert"; import { customizeArray, customizeObject, - mergeWithCustomize, - mergeWithRules + mergeWithCustomize } from "../resolve"; import customizeTests from "../helpers/customize-tests"; import { CustomizeRule } from "../src/types"; @@ -17,422 +16,8 @@ describe("Merge strategy", function() { customizeTests(merge); mergeStrategySpecificTests(merge); - mergeNested(); }); -function mergeNested() { - it("should replace with nested notation", function() { - const base = { - entry: "demo", - module: { - rules: [ - { - test: /\.scss$/, - loaders: ["css-loader"] - }, - { - test: /\.css$/, - loaders: ["style-loader"] - } - ] - } - }; - const development = { - module: { - rules: [ - { - test: /\.scss$/, - loaders: ["style-loader"] - }, - { - test: /\.less$/, - loaders: ["css-loader"] - } - ] - } - }; - const result = { - entry: "demo", - module: { - rules: [ - { - test: /\.scss$/, - loaders: ["style-loader"] - }, - { - test: /\.css$/, - loaders: ["style-loader"] - }, - { - test: /\.less$/, - loaders: ["css-loader"] - } - ] - } - }; - - assert.deepStrictEqual( - mergeWithRules({ - module: { - rules: { - test: CustomizeRule.Match, - loaders: CustomizeRule.Replace - } - } - })(base, development), - result - ); - }); - - it("should append with nested notation", function() { - const base = { - module: { - rules: [ - { - test: /\.scss$/, - loaders: ["css-loader"] - } - ] - } - }; - const development = { - module: { - rules: [ - { - test: /\.scss$/, - loaders: ["style-loader"] - } - ] - } - }; - const result = { - module: { - rules: [ - { - test: /\.scss$/, - loaders: ["css-loader", "style-loader"] - } - ] - } - }; - - assert.deepStrictEqual( - mergeWithRules({ - module: { - rules: { - test: CustomizeRule.Match, - loaders: CustomizeRule.Append - } - } - })(base, development), - result - ); - }); - - it("should prepend with nested notation", function() { - const base = { - module: { - rules: [ - { - test: /\.scss$/, - loaders: ["css-loader"] - } - ] - } - }; - const development = { - module: { - rules: [ - { - test: /\.scss$/, - loaders: ["style-loader"] - } - ] - } - }; - const result = { - module: { - rules: [ - { - test: /\.scss$/, - loaders: ["style-loader", "css-loader"] - } - ] - } - }; - - assert.deepStrictEqual( - mergeWithRules({ - module: { - rules: { - test: CustomizeRule.Match, - loaders: CustomizeRule.Prepend - } - } - })(base, development), - result - ); - }); - - it("should merge #146", function() { - const a = { - module: { - rules: [ - { - test: /\.css$/, - use: [{ loader: "style-loader" }, { loader: "sass-loader" }] - } - ] - } - }; - const b = { - module: { - rules: [ - { - test: /\.css$/, - use: [ - { - loader: "style-loader", - options: { - modules: true - } - } - ] - } - ] - } - }; - const result = { - module: { - rules: [ - { - test: /\.css$/, - use: [ - { - loader: "style-loader", - options: { - modules: true - } - }, - { loader: "sass-loader" } - ] - } - ] - } - }; - - assert.deepStrictEqual( - mergeWithRules({ - module: { - rules: { - test: CustomizeRule.Match, - use: { - loader: CustomizeRule.Match, - options: CustomizeRule.Replace - } - } - } - })(a, b), - result - ); - }); - - it("should merge #149", function() { - const base = { - module: { - rules: [ - { - test: /\.scss$/, - loaders: [ - { - loader: "css-loader" - }, - { - loader: "resolve-url-loader" - } - ] - } - ] - } - }; - const development = { - module: { - rules: [ - { - test: /\.scss$/, - loaders: [ - { - loader: "css-loader", - options: { - sourceMap: true - } - } - ] - } - ] - } - }; - const result = { - module: { - rules: [ - { - test: /\.scss$/, - loaders: [ - { - loader: "css-loader", - options: { - sourceMap: true - } - }, - { - loader: "resolve-url-loader" - } - ] - } - ] - } - }; - - assert.deepStrictEqual( - mergeWithRules({ - module: { - rules: { - test: CustomizeRule.Match, - loaders: { - loader: CustomizeRule.Match, - options: CustomizeRule.Replace - } - } - } - })(base, development), - result - ); - }); - - it("should merge with a parser loader", function() { - const defaultConfig = { - module: { - rules: [ - { - parser: { - system: false - } - }, - { - test: /\.(js|ts)x?$/, - exclude: /node_modules/, - use: [ - { - loader: "babel-loader" - } - ] - }, - { - test: /\.css$/i, - include: [/node_modules/, /src/], - use: [ - { - loader: "style-loader" - }, - { - loader: "css-loader", - options: { - modules: false - } - } - ] - } - ] - } - }; - const localConfig = { - module: { - rules: [ - { - test: /\.html$/i, - use: [{ loader: "html-loader" }] - }, - { - test: /\.css$/i, - use: [ - { - loader: "style-loader" - }, - { - loader: "css-loader", - options: { - importLoaders: 1 - } - }, - { - loader: "postcss-loader" - } - ] - } - ] - } - }; - const result = { - module: { - rules: [ - { - parser: { - system: false - } - }, - { - test: /\.(js|ts)x?$/, - exclude: /node_modules/, - use: [ - { - loader: "babel-loader" - } - ] - }, - { - test: /\.css$/i, - include: [/node_modules/, /src/], - use: [ - { - loader: "style-loader", - options: undefined - }, - { - loader: "css-loader", - options: { - importLoaders: 1 - } - }, - { - loader: "postcss-loader" - } - ] - }, - { - test: /\.html$/i, - use: [{ loader: "html-loader" }] - } - ] - } - }; - - assert.deepStrictEqual( - mergeWithRules({ - module: { - rules: { - test: CustomizeRule.Match, - use: { - loader: CustomizeRule.Match, - options: CustomizeRule.Replace - } - } - } - })(defaultConfig, localConfig), - result - ); - }); -} - function mergeStrategySpecificTests(merge) { it("should work with nested arrays and prepend", function() { const a = { diff --git a/test/merge-with-rules.test.ts b/test/merge-with-rules.test.ts new file mode 100644 index 0000000..4c6d38b --- /dev/null +++ b/test/merge-with-rules.test.ts @@ -0,0 +1,416 @@ +import assert from "assert"; +import { mergeWithRules } from "../resolve"; +import { CustomizeRule } from "../src/types"; + +describe("Merge with rules", function() { + it("should replace with nested notation", function() { + const base = { + entry: "demo", + module: { + rules: [ + { + test: /\.scss$/, + loaders: ["css-loader"] + }, + { + test: /\.css$/, + loaders: ["style-loader"] + } + ] + } + }; + const development = { + module: { + rules: [ + { + test: /\.scss$/, + loaders: ["style-loader"] + }, + { + test: /\.less$/, + loaders: ["css-loader"] + } + ] + } + }; + const result = { + entry: "demo", + module: { + rules: [ + { + test: /\.scss$/, + loaders: ["style-loader"] + }, + { + test: /\.css$/, + loaders: ["style-loader"] + }, + { + test: /\.less$/, + loaders: ["css-loader"] + } + ] + } + }; + + assert.deepStrictEqual( + mergeWithRules({ + module: { + rules: { + test: CustomizeRule.Match, + loaders: CustomizeRule.Replace + } + } + })(base, development), + result + ); + }); + + it("should append with nested notation", function() { + const base = { + module: { + rules: [ + { + test: /\.scss$/, + loaders: ["css-loader"] + } + ] + } + }; + const development = { + module: { + rules: [ + { + test: /\.scss$/, + loaders: ["style-loader"] + } + ] + } + }; + const result = { + module: { + rules: [ + { + test: /\.scss$/, + loaders: ["css-loader", "style-loader"] + } + ] + } + }; + + assert.deepStrictEqual( + mergeWithRules({ + module: { + rules: { + test: CustomizeRule.Match, + loaders: CustomizeRule.Append + } + } + })(base, development), + result + ); + }); + + it("should prepend with nested notation", function() { + const base = { + module: { + rules: [ + { + test: /\.scss$/, + loaders: ["css-loader"] + } + ] + } + }; + const development = { + module: { + rules: [ + { + test: /\.scss$/, + loaders: ["style-loader"] + } + ] + } + }; + const result = { + module: { + rules: [ + { + test: /\.scss$/, + loaders: ["style-loader", "css-loader"] + } + ] + } + }; + + assert.deepStrictEqual( + mergeWithRules({ + module: { + rules: { + test: CustomizeRule.Match, + loaders: CustomizeRule.Prepend + } + } + })(base, development), + result + ); + }); + + it("should merge #146", function() { + const a = { + module: { + rules: [ + { + test: /\.css$/, + use: [{ loader: "style-loader" }, { loader: "sass-loader" }] + } + ] + } + }; + const b = { + module: { + rules: [ + { + test: /\.css$/, + use: [ + { + loader: "style-loader", + options: { + modules: true + } + } + ] + } + ] + } + }; + const result = { + module: { + rules: [ + { + test: /\.css$/, + use: [ + { + loader: "style-loader", + options: { + modules: true + } + }, + { loader: "sass-loader" } + ] + } + ] + } + }; + + assert.deepStrictEqual( + mergeWithRules({ + module: { + rules: { + test: CustomizeRule.Match, + use: { + loader: CustomizeRule.Match, + options: CustomizeRule.Replace + } + } + } + })(a, b), + result + ); + }); + + it("should merge #149", function() { + const base = { + module: { + rules: [ + { + test: /\.scss$/, + loaders: [ + { + loader: "css-loader" + }, + { + loader: "resolve-url-loader" + } + ] + } + ] + } + }; + const development = { + module: { + rules: [ + { + test: /\.scss$/, + loaders: [ + { + loader: "css-loader", + options: { + sourceMap: true + } + } + ] + } + ] + } + }; + const result = { + module: { + rules: [ + { + test: /\.scss$/, + loaders: [ + { + loader: "css-loader", + options: { + sourceMap: true + } + }, + { + loader: "resolve-url-loader" + } + ] + } + ] + } + }; + + assert.deepStrictEqual( + mergeWithRules({ + module: { + rules: { + test: CustomizeRule.Match, + loaders: { + loader: CustomizeRule.Match, + options: CustomizeRule.Replace + } + } + } + })(base, development), + result + ); + }); + + it("should merge with a parser loader", function() { + const defaultConfig = { + module: { + rules: [ + { + parser: { + system: false + } + }, + { + test: /\.(js|ts)x?$/, + exclude: /node_modules/, + use: [ + { + loader: "babel-loader" + } + ] + }, + { + test: /\.css$/i, + include: [/node_modules/, /src/], + use: [ + { + loader: "style-loader" + }, + { + loader: "css-loader", + options: { + modules: false + } + } + ] + } + ] + } + }; + const localConfig = { + module: { + rules: [ + { + test: /\.html$/i, + use: [{ loader: "html-loader" }] + }, + { + test: /\.css$/i, + use: [ + { + loader: "style-loader" + }, + { + loader: "css-loader", + options: { + importLoaders: 1 + } + }, + { + loader: "postcss-loader" + } + ] + } + ] + } + }; + const result = { + module: { + rules: [ + { + parser: { + system: false + } + }, + { + test: /\.(js|ts)x?$/, + exclude: /node_modules/, + use: [ + { + loader: "babel-loader" + } + ] + }, + { + test: /\.css$/i, + include: [/node_modules/, /src/], + use: [ + { + loader: "style-loader", + options: undefined + }, + { + loader: "css-loader", + options: { + importLoaders: 1 + } + }, + { + loader: "postcss-loader" + } + ] + }, + { + test: /\.html$/i, + use: [{ loader: "html-loader" }] + } + ] + } + }; + + assert.deepStrictEqual( + mergeWithRules({ + module: { + rules: { + test: CustomizeRule.Match, + use: { + loader: CustomizeRule.Match, + options: CustomizeRule.Replace + } + } + } + })(defaultConfig, localConfig), + result + ); + }); +});