Skip to content

Commit

Permalink
fix(test) fix issue with cache test after switching to async function
Browse files Browse the repository at this point in the history
  • Loading branch information
prisca-c committed Dec 24, 2023
1 parent ef745e8 commit 817734d
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions tests/cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,39 @@ test.group('Cache class', (group) => {
})

test('get() should return undefined if key does not exist', async ({ assert }) => {
const cacheValue = cache.get(key)
const cacheValue = await cache.get(key)
assert.isUndefined(cacheValue)
})

test('get() should return the value if key exists', async ({ assert }) => {
cache.set(key, value)
const cacheValue = cache.get(key)
await cache.set(key, value)
const cacheValue = await cache.get(key)
assert.deepEqual(cacheValue, value)
})

test('set() should set a key-value pair', async ({ assert }) => {
cache.set(key, value)
const cacheValue = cache.get(key)
await cache.set(key, value)
const cacheValue = await cache.get(key)
assert.deepEqual(cacheValue, value)
})

test('set() should set a key-value pair with a ttl and delete it after the ttl', async ({
assert,
}, done) => {
const ttl = 1000
cache.set(key, value, ttl)
let cacheValue = cache.get(key)
await cache.set(key, value, ttl)
let cacheValue = await cache.get(key)
assert.deepEqual(cacheValue, value)

setTimeout(() => {
cacheValue = cache.get(key)
setTimeout(async () => {
cacheValue = await cache.get(key)
assert.isUndefined(cacheValue)
done()
}, ttl + 100) // Wait a bit longer than the TTL to ensure the key-value pair has been removed
}).waitForDone()

test('delete() should delete a key-value pair', async ({ assert }) => {
cache.set(key, value)
await cache.set(key, value)
const cacheValue = cache.delete(key)
assert.isTrue(cacheValue)
})
Expand All @@ -54,14 +54,14 @@ test.group('Cache class', (group) => {
})

test('clear() should clear the cache', async ({ assert }) => {
cache.set(key, value)
await cache.set(key, value)
cache.clear()
const cacheValue = cache.get(key)
const cacheValue = await cache.get(key)
assert.isUndefined(cacheValue)
})

test('has() should return true if key exists', async ({ assert }) => {
cache.set(key, value)
await cache.set(key, value)
const cacheValue = cache.has(key)
assert.isTrue(cacheValue)
})
Expand All @@ -72,7 +72,7 @@ test.group('Cache class', (group) => {
})

test('all() should return all the keys', async ({ assert }) => {
cache.set(key, value)
await cache.set(key, value)
const keys = cache.all()
assert.deepEqual(keys, [[key, value]])
})
Expand Down

0 comments on commit 817734d

Please sign in to comment.