nuxt / nuxt/test-utils

Setting cookies in context does not appear to be working

Open
#325 16 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
TypeScript
Stars
437
Forks
120
Avg merge
22h 52m
Merged PRs (30d)
23

Description

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')
})

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the shown createBrowser, getBrowser, and createPage entry points and compare how Playwright context cookies and storageState are passed into the page. Done means a test can set the test cookie and page.innerHTML('body') contains "test cookie".

Written by the indexing model from the issue text.

Assessment

Tech stack
nuxtjs, playwright, typescript
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.