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