prazdevs/pinia-plugin-persistedstate

How to import "persistedState" in Nuxt 3?

kerim1505k opened this issue · 5 comments

Hi.

In documentation it's saying

"By default, your stores will be persisted in cookies (using Nuxt's useCookie under the hood). You can configure what storage you want to use by using the storages available under the auto-imported persistedState variable."

but what if auto-imports are turned off in application? how to import "persistedState" variable then? I'm a little bit struggling on that problem cause I'm trying to use "paths" options to persist certain store values in localStorage and some values in cookies

https://nuxt.com/docs/guide/concepts/auto-imports#disabling-auto-imports

according to the docs, exports should still be available from #imports

Hello @prazdevs

because I am using Vitest for testing in Nuxt 3, how can I automatically import it into the configuration file? Here is my vitest.config.ts configuration file.

export default defineConfig({
  plugins: [
    Vue({
      reactivityTransform: true
    }),
    ViteAutoImport({
      imports: [
        'vue',
        'vue-router',
        'pinia',
        {
          '@pinia-plugin-persistedstate/nuxt/dist/runtime/storages': [
            'persistedState'
          ]
        }
      ],
      dts: './test/auto-imports.d.ts'
    })
  ],
});

Although I added persistedState in the configuration file, an error occurs when executing. The error is as follows,

src/pages/sample/test/test.spec.ts
Error: Missing "./dist/runtime/storages" specifier in "@pinia-plugin-persistedstate/nuxt" package
 - /node_modules/.pnpm/vite@4.5.0_@types+node@20.8.6_sass@1.69.2/node_modules/vite/dist/node/chunks/dep-bb8a8339.js:21445:25
 - /node_modules/.pnpm/vite@4.5.0_@types+node@20.8.6_sass@1.69.2/node_modules/vite/dist/node/chunks/dep-bb8a8339.js:21445:627

I hope to get your assistance. thanks very mush.

have you tried using https://github.com/danielroe/nuxt-vitest directly to avoid configuring imports and such ?

Any update on this one? I'm having the same issue writing unit tests

FAIL tests/Player.spec.ts [ tests/Player.spec.ts ] ReferenceError: persistedState is not defined ❯ store/routine.ts:101:14 }, persist: { storage: persistedState.localStorage, ^ },

@nuxt/test-utils is still undergoing big changes (in 3.9 probably) as they are internalizing nuxt-vitest (see nuxt/test-utils#644)

I'll close this for now and eventually look at it if i'm given a proper reproduction repo. I still recommend waiting for the next test-utils version to hopefully better handle the nuxt imports 🙏