diff --git a/src/index.ts b/src/index.ts index 53b8a20..6987b07 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,7 @@ import mergeWith from "./merge-with"; import joinArrays from "./join-arrays"; import unique from "./unique"; import { CustomizeRule, ICustomizeOptions, Key } from "./types"; -import { isPlainObject } from "./utils"; +import { isPlainObject, isUndefined } from "./utils"; function merge( firstConfiguration: Configuration | Configuration[], @@ -22,9 +22,8 @@ function mergeWithCustomize( firstConfiguration: Configuration | Configuration[], ...configurations: Configuration[] ): Configuration { - // No configuration at all - if (!firstConfiguration) { - return {} as Configuration; + if (isUndefined(firstConfiguration) || configurations.some(isUndefined)) { + throw new TypeError("Merging undefined is not supported"); } // @ts-ignore @@ -32,6 +31,11 @@ function mergeWithCustomize( throw new TypeError("Promises are not supported"); } + // No configuration at all + if (!firstConfiguration) { + return {} as Configuration; + } + if (configurations.length === 0) { if (Array.isArray(firstConfiguration)) { // Empty array @@ -39,6 +43,10 @@ function mergeWithCustomize( return {} as Configuration; } + if (firstConfiguration.some(isUndefined)) { + throw new TypeError("Merging undefined is not supported"); + } + // @ts-ignore if (firstConfiguration[0].then) { throw new TypeError("Promises are not supported"); diff --git a/src/utils.ts b/src/utils.ts index dea8b32..463506e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -17,4 +17,8 @@ function isPlainObject(a) { return typeof a === "object"; } -export { isRegex, isFunction, isPlainObject }; +function isUndefined(a) { + return typeof a === "undefined"; +} + +export { isRegex, isFunction, isPlainObject, isUndefined }; diff --git a/test/merge.test.ts b/test/merge.test.ts index da1aea8..5538680 100644 --- a/test/merge.test.ts +++ b/test/merge.test.ts @@ -19,8 +19,63 @@ function normalMergeTests(merge) { } function normalMergeTest(merge, loadersKey) { - it("should work with an empty configuration", function() { - assert.deepStrictEqual(merge(), {}); + it("should throw with an empty configuration", function() { + assert.throws(() => merge(), { + name: "TypeError", + message: "Merging undefined is not supported" + }); + }); + + it("should throw with undefined 1/4", function() { + assert.throws(() => merge(undefined), { + name: "TypeError", + message: "Merging undefined is not supported" + }); + }); + + it("should throw with undefined 2/4", function() { + assert.throws(() => merge([undefined]), { + name: "TypeError", + message: "Merging undefined is not supported" + }); + }); + + it("should throw with undefined 2/4", function() { + const result = { devServer: null }; + + assert.throws( + () => + merge( + { + devServer: { base: true } + }, + undefined, + result + ), + { + name: "TypeError", + message: "Merging undefined is not supported" + } + ); + }); + + it("should throw with undefined 3/4", function() { + const result = { devServer: null }; + + assert.throws( + () => + merge( + undefined, + { + devServer: { base: true } + }, + result + ), + { + name: "TypeError", + message: "Merging undefined is not supported" + } + ); }); it("should work with an empty array", function() {