Repository: pmndrs/zustand
File: src/middleware/persist.ts (TypeScript)

Code before:
          if (storageValue) {
            return options.deserialize(storageValue)
          }
        })
        .then((deserializedStorageValue) => {
          if (deserializedStorageValue) {
            if (
              typeof deserializedStorageValue.version === 'number' &&
              deserializedStorageValue.version !== options.version
            ) {
              if (options.migrate) {
                return options.migrate(
                  deserializedStorageValue.state,
                  deserializedStorageValue.version
                )
              }
              console.error(
                `State loaded from storage couldn't be migrated since no migrate function was provided`
              )
            } else {
              return deserializedStorageValue.state
            }
          }
        })
        .then((migratedState) => {
          stateFromStorage = options.merge(migratedState as S, get())

          set(stateFromStorage as S, true)
          return setItem()
        })
        .then(() => {
          postRehydrationCallback?.(stateFromStorage, undefined)
          hasHydrated = true
          finishHydrationListeners.forEach((cb) => cb(stateFromStorage as S))
        })
        .catch((e: Error) => {
          postRehydrationCallback?.(undefined, e)
        })
    }

    api.persist = {
      setOptions: (newOptions) => {
        options = {
          ...options,
          ...newOptions,
        }

        if (newOptions.getStorage) {
          storage = newOptions.getStorage()
        }
      },

Review comment (suggestion):
Maybe this could fix your issue and the tests?

```suggestion
          stateFromStorage = options.merge(migratedState as S, configResult.getState())
```

Code after:
          if (storageValue) {
            return options.deserialize(storageValue)
          }
        })
        .then((deserializedStorageValue) => {
          if (deserializedStorageValue) {
            if (
              typeof deserializedStorageValue.version === 'number' &&
              deserializedStorageValue.version !== options.version
            ) {
              if (options.migrate) {
                return options.migrate(
                  deserializedStorageValue.state,
                  deserializedStorageValue.version
                )
              }
              console.error(
                `State loaded from storage couldn't be migrated since no migrate function was provided`
              )
            } else {
              return deserializedStorageValue.state
            }
          }
        })
        .then((migratedState) => {
          stateFromStorage = options.merge(
            migratedState as S,
            get() ?? configResult
          )

          set(stateFromStorage as S, true)
          return setItem()
        })
        .then(() => {
          postRehydrationCallback?.(stateFromStorage, undefined)
          hasHydrated = true
          finishHydrationListeners.forEach((cb) => cb(stateFromStorage as S))
        })
        .catch((e: Error) => {
          postRehydrationCallback?.(undefined, e)
        })
    }

    api.persist = {
      setOptions: (newOptions) => {
        options = {
          ...options,
          ...newOptions,
        }

        if (newOptions.getStorage) {