3

I have a suite of tests that all pass if they are run individually. However, the tests fail due to a check on the value of a cookie if they are run in parallel.

The problem is that supertest's cookies are not cleared between each test.

Is there a way to clear cookies between each test using supertest? This is related to this open issue that doesn't offer a solution.

I've tried both:

afterEach(() => {
  request(app)
    .set('Cookie', [])
})

and:

afterEach(() => {
  request(app)
    .set('Cookie', '')
})

...to no avail.

Here are the two tests that run fine individually but fail if run in parallel:

test('It should respond 302 to the wrong url', (done) => {
  const accessiblePages = {get_member_accessible_pages: []}
  nock('http://localhost:8000')
    .log(console.log)
    .get('/auth/me/')
    .reply(200, accessiblePages)
  // Must use j: to work with cookie parser
  const cookie = 'userInfo=j:' + encodeURIComponent(
    JSON.stringify(accessiblePages))
  request(app)
    .get('/first-url')
    .set('Cookie', cookie)
    .expect(302)
    .expect('Location', '/new-url')
    .then((response) => {
      expect(response.statusCode).toBe(302)
      done()
    })
})

and

test('It should set an updated cookie for the logged in user', (done) => {
  const accessiblePages = {get_member_accessible_pages: [123, 456]}
  nock('http://localhost:8000')
    .log(console.log)
    .get('/auth/me/')
    .reply(200, accessiblePages)
  // Must use j: to work with cookie parser
  const userInfoCookie = 'userInfo=j:' + encodeURIComponent(
    JSON.stringify(accessiblePages))
  const accessTokenCookie = 'accessToken=accessToken'
  const requestCookie = [userInfoCookie, accessTokenCookie].join(';')
  const responseCookieValue = accessiblePages
  request(app)
    .get('/first-url')
    .set('Cookie', requestCookie)
    .expect(200)
    .then((response) => {
      expect(response.statusCode).toBe(200)
      const cookies = setCookie.parse(response)
      expect(cookieParser.JSONCookie(cookies[0].value))
        .toEqual(responseCookieValue) // FAILS HERE - shows accessiblePages variable from the previous test.
      done()
    })
})
YPCrumble
  • 26,610
  • 23
  • 107
  • 172
  • How about [deleting cookies with jQuery](https://stackoverflow.com/questions/3671659/jquery-delete-cookies)? – KaiserKatze Sep 06 '18 at 16:00
  • @KaiserKatze that's not jQuery - it's a plugin for jQuery called jquery-cookie. I'd prefer not to add two third party libraries that I don't need just to get my tests to work. – YPCrumble Sep 06 '18 at 16:03
  • @KaiserKatze in addition - Jest is a little different from the traditional DOM. For instance, using [this answer](https://stackoverflow.com/a/27374365/2532070) didn't work either. – YPCrumble Sep 06 '18 at 16:07

1 Answers1

5

You can try to explicitly clean cookies that you set in your tests. It can be done via NPM module 'js-cookies' or via simple helper function:

function removeCookie(name) {
  document.cookie = `${name}=1; expires=1 Jan 1970 00:00:00 GMT;`
}

If you want to clean all cookies at once you may use this solution.