admin管理员组文章数量:1336208
I'm trying to test with vitest a page with zustand
But i'm getting this error :
TypeError: Cannot read properties of null (reading 'useRef')
❯ useRef ../../../../../../node_modules/react/cjs/react.development.js:1630:21
❯ useSyncExternalStoreWithSelector ../../../../../../node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.development.js:51:17
❯ useStore ../../../../../../../coucou/app%20copie2/node_modules/zustand/esm/index.mjs:17:17
❯ Module.useBoundStore ../../../../../../../coucou/%20copie2/node_modules/zustand/esm/index.mjs:34:51
❯ SaleDetailLayout ../../layout/SaleDetail.layout.tsx:9:20
7|
8| export const SaleDetailLayout = () => {
9| const loaded = usePayloadStore((state) => state.loaded)
My vitest config
import { defineConfig } from 'vitest/config'
import react from '@vitejs/plugin-react'
import path, { resolve } from 'path'
import svgr from 'vite-plugin-svgr'
import { fileURLToPath } from 'node:url'
const rootDir = fileURLToPath(new URL('.', import.meta.url))
// /config/
export default defineConfig({
plugins: [react(), svgr()],
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
test: {
include: ['**/*.test.tsx', '**/*.test.ts'],
environment: 'happy-dom',
css: true,
setupFiles: resolve(rootDir, './test-setup.ts'),
globals: true,
},
})
The component
import { usePayloadStore } from '../store/usePayloadStore'
export const SaleDetailLayout = () => {
const loaded = usePayloadStore((state) => state.loaded)
return <div>{loaded}</div>
}
The store :
import { create } from 'zustand'
export type PayloadState<T> = {
payload: T | undefined
loaded: boolean
setPayload: (payload: Partial<T>) => void
}
type Payload = {
saleID?: number
}
export const usePayloadStore = create<PayloadState<Payload>>((set: (payload: object) => void) => ({
payload: undefined,
loaded: false,
setPayload: (payload: Partial<Payload>) => set(() => ({ loaded: true, payload: payload })),
}))
And the test
it("display date", async () => {
render(<SaleDetailLayout />)
expect(await screen.findByText(/à 00:00/)).toBeInTheDocument()
})
It's related to zustand
, because if I use an other state management lib, I don't get the error (ex. with jotai
)
本文标签: Vitest and zustand Cannot read properties of null (reading 39useRef39)Stack Overflow
版权声明:本文标题:Vitest and zustand Cannot read properties of null (reading 'useRef') - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742399775a2467652.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论