test-utils: Setting cookies in context does not appear to be working

I am trying to set and test cookies. I have a simple test that loads a page where the only content is the value of a cookie. In the browser, it works even with server-side rendering, but in the test, I am not able to get the cookie value to appear.

I have tried using the Playwright context, hoping that the Nuxt test utils would recognize it somehow. I have also tried using the storageState.cookies to set the cookies, but the test always fails.

Can anyone point me in the right direction?

Example using context:

it('renders cookie value', async () => {
  await createBrowser()
  const browser = await getBrowser()
  const context = await browser.newContext()
  await context.addCookies([
    {
      name: 'test',
      value: 'test cookie',
      path: '/',
      domain: 'localhost'
    }
  ])

  const page = await createPage('/')

  expect(await page.innerHTML('body')).toContain('test cookie')
  await context.close()
  await browser.close()
})

Example using storageState.cookies

it('renders cookie value', async () => {
  const page = await createPage('/', {
    storageState: {
      cookies: [
        {
          name: 'test',
          value: 'test cookie',
          path: '/',
          domain: 'localhost',
          expires: -1,
          httpOnly: false,
          secure: false,
          sameSite: 'None'
        }
      ],
      origins: []
    }
  })

  expect(await page.innerHTML('body')).toContain('test cookie')
})

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 1
  • Comments: 16 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Running into the same issue unfortunately. This makes it impossible to test with cookies. Would greatly appreciate if this get fixed! 🙏

My issue was that I was not set up correctly with test-utils - I had only added the npm package and not added it as a module, or set vitest’s environment to Nuxt - just in case someone makes the same mistake as me. The docs for getting set up with testing are actually there now which helped a lot, when I had set this up originally they were not there.