N
Nuxt2w ago
Yuri

Vitest and useCookie

const layoutBannerTopIsHidden = useCookie<boolean>(CookieLayoutBannerTopIsHidden, { maxAge: 60 * 60 * 24 * 7, default: () => false })

if (layoutBannerTopIsHidden.value) {
closeInStore()
}
const layoutBannerTopIsHidden = useCookie<boolean>(CookieLayoutBannerTopIsHidden, { maxAge: 60 * 60 * 24 * 7, default: () => false })

if (layoutBannerTopIsHidden.value) {
closeInStore()
}
How I can mock cookie set ? I want to check two scenarios: 1. CookieLayoutBannerTopIsHidden is TRUE and banner is not showing 2. CookieLayoutBannerTopIsHidden is FALSE and banner not showing
2 Replies
kapa.ai
kapa.ai2w ago
Hi @Yuri. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To mock the useCookie composable in your Vitest tests for Nuxt, you can use the mockNuxtImport helper provided by Nuxt's test utils. Here's how you can set up your tests for the two scenarios you described:
import { describe, it, expect, beforeEach } from 'vitest'
import { mockNuxtImport } from '@nuxt/test-utils/runtime'

describe('Banner visibility tests', () => {
beforeEach(() => {
// Reset mocks before each test
vi.restoreAllMocks()
})

it('should not show banner when CookieLayoutBannerTopIsHidden is TRUE', async () => {
mockNuxtImport('useCookie', () => {
return () => ({ value: true })
})

// Your test implementation here
// Assert that the banner is not showing
})

it('should show banner when CookieLayoutBannerTopIsHidden is FALSE', async () => {
mockNuxtImport('useCookie', () => {
return () => ({ value: false })
})

// Your test implementation here
// Assert that the banner is showing
})
})
import { describe, it, expect, beforeEach } from 'vitest'
import { mockNuxtImport } from '@nuxt/test-utils/runtime'

describe('Banner visibility tests', () => {
beforeEach(() => {
// Reset mocks before each test
vi.restoreAllMocks()
})

it('should not show banner when CookieLayoutBannerTopIsHidden is TRUE', async () => {
mockNuxtImport('useCookie', () => {
return () => ({ value: true })
})

// Your test implementation here
// Assert that the banner is not showing
})

it('should show banner when CookieLayoutBannerTopIsHidden is FALSE', async () => {
mockNuxtImport('useCookie', () => {
return () => ({ value: false })
})

// Your test implementation here
// Assert that the banner is showing
})
})
In this setup: 1. We import the necessary Vitest functions and the mockNuxtImport helper. 2. We use beforeEach to reset all mocks before each test, ensuring a clean slate. 3. For each scenario, we use mockNuxtImport to mock the useCookie composable, returning an object with a value property set to either true or false. Note that mockNuxtImport can only be used once per mocked import per test file, as mentioned in the Nuxt documentation (https://nuxt.com/docs/getting-started/testing#mocknuxtimport). If you need to change the mock implementation between tests, you can use vi.hoisted as described in the documentation. Remember to implement the actual test logic and assertions based on your specific component and banner implementation. Also, keep in mind that for end-to-end testing, it's generally recommended to test user-visible behavior rather than implementation details, as mentioned in this Stack Overflow post (https://stackoverflow.com/questions/78731451). You might want to consider testing the actual visibility of the banner in the UI rather than mocking the cookie directly. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2w ago
<@740173469404364872> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Want results from more Discord servers?
Add your server