Skip to content

test(middleware/immer): add runtime tests for immer middleware#3471

Merged
dai-shi merged 2 commits into
pmndrs:mainfrom
KimHyeongRae0:test/middleware-immer-runtime-tests
Apr 21, 2026
Merged

test(middleware/immer): add runtime tests for immer middleware#3471
dai-shi merged 2 commits into
pmndrs:mainfrom
KimHyeongRae0:test/middleware-immer-runtime-tests

Conversation

@KimHyeongRae0

Copy link
Copy Markdown
Contributor

Related Bug Reports or Discussions

N/A — coverage-only test additions. No source changes.

Summary

The immer middleware (src/middleware/immer.ts) has no dedicated runtime test file. tests/middlewareTypes.test.tsx only exercises it at the type level, leaving the store.setState override (lines 77–83) entirely uncovered at runtime. Other middleware (devtools, persist, subscribeWithSelector) already have dedicated runtime test files.

This mirrors the pattern accepted in #3442 (vanilla had subscribe coverage, React did not → add the missing runtime tests).

Tests added (tests/immer.test.tsx)

  • should mutate state via an immer draft (function updater) — covers the produce(updater) path
  • should merge state when given a non-function updater — covers the : updater path
  • should replace the entire state when replace flag is true — covers replace flag propagation
  • should produce nested state updates via a draft without mutating the previous state — verifies immer's structural sharing
  • should work with vanilla createStore — covers the vanilla surface

Coverage delta for src/middleware/immer.ts

Before After
Statements 71.42% 100%
Branches 0% 100%
Functions 66.66% 100%
Lines 66.66% 100%

Suite total: 207 → 212 tests, all passing.

Check List

  • pnpm run fix for formatting and linting code and docs

@vercel

vercel Bot commented Apr 20, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
zustand-demo Ready Ready Preview, Comment Apr 21, 2026 2:04am

Request Review

@codesandbox-ci

codesandbox-ci Bot commented Apr 20, 2026

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@pkg-pr-new

pkg-pr-new Bot commented Apr 20, 2026

Copy link
Copy Markdown

commit: 62a421f

@dai-shi

dai-shi commented Apr 20, 2026

Copy link
Copy Markdown
Member

@sukvvon Would you like to give a review?

Comment thread tests/immer.test.tsx Outdated
expect(useBoundStore.getState().count).toBe(1)
})

it('should merge state when given a non-function updater', () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('should merge state when given a non-function updater', () => {
it('should not apply produce when updater is not a function', () => {

The name "merge" feels slightly misleading here — merging is actually vanilla setState's behavior, while what this test truly covers is the right-hand branch at immer.ts:79, where a non-function updater bypasses produce. Something like this would make the intent clearer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the catch — you're right that "merge" conflates this with vanilla setState semantics, when the test is really about the non-function branch at immer.ts:79 bypassing produce. Renamed to should not apply produce when updater is not a function in 62a421f.

…produce bypass

The previous name ("should merge state when given a non-function updater")
implied merge semantics, which is vanilla setState's behavior. What the test
actually exercises is immer.ts:79, where a non-function updater bypasses
`produce`. Renaming to "should not apply produce when updater is not a
function" per @sukvvon's review feedback.

@sukvvon sukvvon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dai-shi Checked out and verified locally — immer.ts coverage goes 66% → 100% and tests 207 → 212 exactly as described. The should produce nested state updates via a draft without mutating the previous state case nicely covers immer's structural sharing. Looks good to merge.

@dai-shi dai-shi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dai-shi dai-shi merged commit 95d3f33 into pmndrs:main Apr 21, 2026
33 checks passed
@KimHyeongRae0

Copy link
Copy Markdown
Contributor Author

Thanks @dai-shi for the quick merge, and @sukvvon for taking the time to check it out locally — really appreciate the thoughtful rename suggestion and the coverage verification.

@KimHyeongRae0 KimHyeongRae0 deleted the test/middleware-immer-runtime-tests branch April 21, 2026 03:25
mergify Bot added a commit to robfrank/linklift that referenced this pull request Jun 10, 2026
Bumps [zustand](https://github.com/pmndrs/zustand) from 5.0.12 to 5.0.14.
Release notes

*Sourced from [zustand's releases](https://github.com/pmndrs/zustand/releases).*

> v5.0.14
> -------
>
> This release fixes a type issue in devtools.
>
> What's Changed
> --------------
>
> * fix(devtools): improve type inference for Devtools initializer by [`@​dbritto-dev`](https://github.com/dbritto-dev) in [pmndrs/zustand#3511](https://redirect.github.com/pmndrs/zustand/pull/3511)
>
> New Contributors
> ----------------
>
> * [`@​TheSeydiCharyyev`](https://github.com/TheSeydiCharyyev) made their first contribution in [pmndrs/zustand#3487](https://redirect.github.com/pmndrs/zustand/pull/3487)
> * [`@​brofrong`](https://github.com/brofrong) made their first contribution in [pmndrs/zustand#3496](https://redirect.github.com/pmndrs/zustand/pull/3496)
> * [`@​hyun907`](https://github.com/hyun907) made their first contribution in [pmndrs/zustand#3506](https://redirect.github.com/pmndrs/zustand/pull/3506)
>
> **Full Changelog**: <pmndrs/zustand@v5.0.13...v5.0.14>
>
> v5.0.13
> -------
>
> This release includes an improvement in the devtools middleware.
>
> What's Changed
> --------------
>
> * refactor(devtools): remove duplicate module augmentation by [`@​mahmoodhamdi`](https://github.com/mahmoodhamdi) in [pmndrs/zustand#3443](https://redirect.github.com/pmndrs/zustand/pull/3443)
> * fix(devtools): support Firefox/Safari stack format in findCallerName by [`@​SBolsec`](https://github.com/SBolsec) in [pmndrs/zustand#3469](https://redirect.github.com/pmndrs/zustand/pull/3469)
>
> New Contributors
> ----------------
>
> * [`@​mahmoodhamdi`](https://github.com/mahmoodhamdi) made their first contribution in [pmndrs/zustand#3443](https://redirect.github.com/pmndrs/zustand/pull/3443)
> * [`@​FelixEckl-vireq`](https://github.com/FelixEckl-vireq) made their first contribution in [pmndrs/zustand#3466](https://redirect.github.com/pmndrs/zustand/pull/3466)
> * [`@​KimHyeongRae0`](https://github.com/KimHyeongRae0) made their first contribution in [pmndrs/zustand#3471](https://redirect.github.com/pmndrs/zustand/pull/3471)
> * [`@​lstak`](https://github.com/lstak) made their first contribution in [pmndrs/zustand#3483](https://redirect.github.com/pmndrs/zustand/pull/3483)
> * [`@​AlexRixten`](https://github.com/AlexRixten) made their first contribution in [pmndrs/zustand#3474](https://redirect.github.com/pmndrs/zustand/pull/3474)
> * [`@​SBolsec`](https://github.com/SBolsec) made their first contribution in [pmndrs/zustand#3469](https://redirect.github.com/pmndrs/zustand/pull/3469)
>
> **Full Changelog**: <pmndrs/zustand@v5.0.12...v5.0.13>


Commits

* [`bfb2a9e`](pmndrs/zustand@bfb2a9e) 5.0.14
* [`62b2aff`](pmndrs/zustand@62b2aff) chore(deps): update dev dependencies ([#3513](https://redirect.github.com/pmndrs/zustand/issues/3513))
* [`ad77bd3`](pmndrs/zustand@ad77bd3) fix(devtools): improve type inference for Devtools initializer ([#3511](https://redirect.github.com/pmndrs/zustand/issues/3511))
* [`8476d2c`](pmndrs/zustand@8476d2c) update pnpm etc ([#3512](https://redirect.github.com/pmndrs/zustand/issues/3512))
* [`d690ec2`](pmndrs/zustand@d690ec2) docs(combine): add object constraints to T and U in signature ([#3506](https://redirect.github.com/pmndrs/zustand/issues/3506))
* [`fd8c601`](pmndrs/zustand@fd8c601) docs(react): add Action constraint to redux middleware signature ([#3492](https://redirect.github.com/pmndrs/zustand/issues/3492))
* [`2ce8226`](pmndrs/zustand@2ce8226) docs(immer): fix setPerson updater type in usage examples ([#3502](https://redirect.github.com/pmndrs/zustand/issues/3502))
* [`038b938`](pmndrs/zustand@038b938) docs(updating-state): use curried create form with explicit state type ([#3503](https://redirect.github.com/pmndrs/zustand/issues/3503))
* [`60a91b4`](pmndrs/zustand@60a91b4) docs(devtools): add missing devtools import to troubleshooting example ([#3501](https://redirect.github.com/pmndrs/zustand/issues/3501))
* [`efad169`](pmndrs/zustand@efad169) Update FUNDING.json
* Additional commits viewable in [compare view](pmndrs/zustand@v5.0.12...v5.0.14)
  
[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility\_score?dependency-name=zustand&package-manager=npm\_and\_yarn&previous-version=5.0.12&new-version=5.0.14)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)
Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.
[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)
---
Dependabot commands and options
  
You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot show  ignore conditions` will show all of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants