Skip to content

Commit

Permalink
fix race condition in cookie.maxAge test (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
autopulated authored Apr 10, 2024
1 parent c220026 commit 8554dca
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions test/cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,19 +568,31 @@ test('Cookie', t => {
t.equal('maxAge' in json, false)
})

// the clock ticks while we're testing, so to prevent occasional test
// failures where these tests tick over 1ms, take the time difference into
// account:

t.test('maxAge calculated from expires', t => {
t.plan(2)

const startT = Date.now()
const cookie = new Cookie({ expires: new Date(Date.now() + 1000) })
t.equal(cookie.maxAge <= 1000, true)
const maxAge = cookie.maxAge
const duration = Date.now() - startT

t.equal(maxAge <= 1000 && maxAge >= 1000 - duration, true)
t.equal(cookie.originalMaxAge, null)
})

t.test('maxAge set by maxAge', t => {
t.plan(2)

const startT = Date.now()
const cookie = new Cookie({ maxAge: 1000 })
t.equal(cookie.maxAge, 1000)
const maxAge = cookie.maxAge
const duration = Date.now() - startT

t.equal(maxAge <= 1000 && maxAge >= 1000 - duration, true)
t.equal(cookie.originalMaxAge, 1000)
})
})

0 comments on commit 8554dca

Please sign in to comment.