diff --git a/__tests__/patch.js b/__tests__/patch.js index c7dbeaf6b..7e26cb418 100644 --- a/__tests__/patch.js +++ b/__tests__/patch.js @@ -1596,3 +1596,12 @@ describe("RTK-5159: Patch path truncation bug", () => { expect(patches[0].path).toEqual(["queries", "queryKey", "data", "items", 1]) }) }) +test("applyPatches throws proper immer error when intermediate path value is null", () => { + // isObjectish(null) === true (typeof null === "object"), so the null check + // inside applyPatches_ was missing; navigating into null threw a native + // TypeError instead of immer's own "path doesn't resolve" error. + const isProd = process.env.NODE_ENV === "production" + expect(() => { + applyPatches({a: null}, [{op: "add", path: ["a", "b"], value: 1}]) + }).toThrow(isProd ? "18" : "Cannot apply patch, path doesn't resolve: a/b") +}) diff --git a/src/plugins/patches.ts b/src/plugins/patches.ts index ca6ded45a..08c7d0f62 100644 --- a/src/plugins/patches.ts +++ b/src/plugins/patches.ts @@ -345,7 +345,8 @@ export function enablePatches() { die(errorOffset + 3) if (isFunction(base) && p === PROTOTYPE) die(errorOffset + 3) base = get(base, p) - if (!isObjectish(base)) die(errorOffset + 2, path.join("/")) + if (base === null || !isObjectish(base)) + die(errorOffset + 2, path.join("/")) } const type = getArchtype(base)