Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(persist): fix async migrate on persist middleware #2877

Merged
merged 7 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test-multiple-builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ jobs:
- name: Patch for CJS
if: ${{ matrix.build == 'cjs' }}
run: |
sed -i~ "s/resolve('\.\/src\(.*\)\.ts')/resolve('\.\/dist\1.js')/" vitest.config.ts
sed -i~ "s/resolve('\.\/src\(.*\)\.ts')/resolve('\.\/dist\1.js')/" vitest.config.mts
dbritto-dev marked this conversation as resolved.
Show resolved Hide resolved
- name: Patch for ESM
if: ${{ matrix.build == 'esm' }}
run: |
sed -i~ "s/resolve('\.\/src\(.*\)\.ts')/resolve('\.\/dist\/esm\1.mjs')/" vitest.config.ts
sed -i~ "s/resolve('\.\/src\(.*\)\.ts')/resolve('\.\/dist\/esm\1.mjs')/" vitest.config.mts
sed -i~ "1s/^/import.meta.env=import.meta.env||{};import.meta.env.MODE='${NODE_ENV}';/" tests/*.tsx
env:
NODE_ENV: ${{ matrix.env }}
Expand Down
15 changes: 8 additions & 7 deletions src/middleware/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,14 @@ const persistImpl: PersistImpl = (config, baseOptions) => (set, get, api) => {
deserializedStorageValue.version !== options.version
) {
if (options.migrate) {
return [
true,
options.migrate(
deserializedStorageValue.state,
deserializedStorageValue.version,
),
] as const
const migration = options.migrate(
deserializedStorageValue.state,
deserializedStorageValue.version,
)
if (migration instanceof Promise) {
return migration.then((result) => [true, result] as const)
}
return [true, migration] as const
}
console.error(
`State loaded from storage couldn't be migrated since no migrate function was provided`,
Expand Down
4 changes: 2 additions & 2 deletions tests/persistAsync.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,10 @@ describe('persist middleware with async configuration', () => {
})
})

it('can migrate persisted state', async () => {
it('can async migrate persisted state', async () => {
const setItemSpy = vi.fn()
const onRehydrateStorageSpy = vi.fn()
const migrateSpy = vi.fn(() => ({ count: 99 }))
const migrateSpy = vi.fn(() => Promise.resolve({ count: 99 }))

const storage = {
getItem: async () =>
Expand Down
2 changes: 1 addition & 1 deletion tests/persistSync.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('persist middleware with sync configuration', () => {
expect(onRehydrateStorageSpy2).toBeCalledWith({ count: 42 }, undefined)
})

it('can migrate persisted state', () => {
it('can non-async migrate persisted state', () => {
const setItemSpy = vi.fn()
const onRehydrateStorageSpy = vi.fn()
const migrateSpy = vi.fn(() => ({ count: 99 }))
Expand Down
2 changes: 1 addition & 1 deletion vitest.config.ts → vitest.config.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve } from 'path'
import { resolve } from 'node:path'
// eslint-disable-next-line import/extensions
import { defineConfig } from 'vitest/config'

Expand Down
Loading