How can I clear cookies between every test using Jest and Supertest?
Asked Answered
S

1

5

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()
    })
})
Scudo answered 6/9, 2018 at 15:58 Comment(3)
How about deleting cookies with jQuery?Emigrant
@Emigrant 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.Scudo
@Emigrant in addition - Jest is a little different from the traditional DOM. For instance, using this answer didn't work either.Scudo
M
6

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.

Mcmann answered 6/9, 2018 at 19:52 Comment(1)
document is unknown in supertestsNick

© 2022 - 2024 — McMap. All rights reserved.