diff --git a/__tests__/regressions.js b/__tests__/regressions.js index d3e63cac..329b0a09 100644 --- a/__tests__/regressions.js +++ b/__tests__/regressions.js @@ -239,5 +239,17 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) { bar: {x: 3} }) }) + + test("#807 new undefined member not stored", () => { + const state = {} + const newState = produce(state, draft => { + draft.baz = undefined + }) + expect(state).not.toBe(newState) + expect(Object.hasOwnProperty.call(newState, "baz")).toBe(true) + expect(newState).toEqual({ + baz: undefined + }) + }) }) } diff --git a/src/core/proxy.ts b/src/core/proxy.ts index 00254366..7aaaf4cd 100644 --- a/src/core/proxy.ts +++ b/src/core/proxy.ts @@ -157,7 +157,14 @@ export const objectTraps: ProxyHandler = { markChanged(state) } - if (state.copy_![prop] === value && typeof value !== "number") return true + if ( + state.copy_![prop] === value && + // special case: NaN + typeof value !== "number" && + // special case: handle new props with value 'undefined' + (value !== undefined || prop in state.copy_) + ) + return true // @ts-ignore state.copy_![prop] = value