From e47f4352cf188538081dd35e35f6fabab749961b Mon Sep 17 00:00:00 2001 From: Shantanu Verma Date: Fri, 17 Nov 2023 16:30:00 +0530 Subject: [PATCH] fix: uninitialized object --- lib/cache/index.ts | 2 +- tests/batched.test.ts | 5 +++-- tests/index.test.ts | 7 +++++++ tests/multilevel.test.ts | 12 ++---------- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/cache/index.ts b/lib/cache/index.ts index 406e75d..4a77b8f 100644 --- a/lib/cache/index.ts +++ b/lib/cache/index.ts @@ -46,7 +46,7 @@ export default class MultilevelCache { const redisCacheResults = await this.redisCache.batchGet(redisQueryKeys); let redisIdx = 0; - let out: any[]; + const out: any[] = []; inMemoryResults.forEach((val, i) => { if (val === null && redisIdx < redisCacheResults.length) { out[i] = redisCacheResults[redisIdx]; diff --git a/tests/batched.test.ts b/tests/batched.test.ts index 135b879..018a0d3 100644 --- a/tests/batched.test.ts +++ b/tests/batched.test.ts @@ -1,5 +1,6 @@ import Redis from "ioredis" import SugarCache from "../lib/main"; +import { logger } from './index.test'; describe('Batched operations', () => { const redis = new Redis({ @@ -58,8 +59,8 @@ describe('Batched operations', () => { const cachedVals = await cache.mget(keys); expect(cachedVals).toStrictEqual([null, null]); - const v0 = cache.get(keys[0]); - const v1 = cache.get(keys[1]); + const v0 = await cache.get(keys[0]); + const v1 = await cache.get(keys[1]); expect(v0).toBeNull(); expect(v1).toBeNull(); diff --git a/tests/index.test.ts b/tests/index.test.ts index bd04e6c..41f71b1 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -1,6 +1,13 @@ import Redis from 'ioredis'; import SugarCache from '../lib/main'; +export const logger = { + info: (message: string, ...args: any[]) => console.log(message), + debug: (message: string, ...args: any[]) => console.log(message), + warn: (message: string, ...args: any[]) => console.log(message), + error: (message: string, ...args: any[]) => console.log(message), +} + describe('Functional tests', () => { const redis = new Redis({ port: 6379, diff --git a/tests/multilevel.test.ts b/tests/multilevel.test.ts index c95c760..01eccff 100644 --- a/tests/multilevel.test.ts +++ b/tests/multilevel.test.ts @@ -1,14 +1,9 @@ import Redis from "ioredis"; import SugarCache from "../lib/main"; +import { logger } from './index.test'; const resourceId = 'resource-UUID'; -const logger = { - info: (message: string, ...args: any[]) => console.log(message), - debug: (message: string, ...args: any[]) => console.log(message), - warn: (message: string, ...args: any[]) => console.log(message), - error: (message: string, ...args: any[]) => console.log(message), -} const redis = new Redis({ port: 6379, host: '127.0.0.1', @@ -17,7 +12,7 @@ const redis = new Redis({ const mockRedisReturnVal = 'REDIS_VAL'; describe('Multilevel caching', () => { - const cacheWithMockedRedis = new SugarCache(redis, { namespace: 'multilevel', inMemoryCache: { enable: true, memoryThresholdPercentage: 0.9} }, logger); + const cacheWithMockedRedis = new SugarCache(redis, { namespace: 'multilevel', inMemoryCache: { enable: true, memoryThresholdPercentage: 0.9} }); class Controller { static mockLatency = 500; @@ -33,7 +28,6 @@ describe('Multilevel caching', () => { ttl: 100 }) async get_fixedTTL(resourceId: string) { - console.log('executing function'); await new Promise((resolve) => setTimeout(resolve, Controller.mockLatency)); return Controller.returnVal; } @@ -74,8 +68,6 @@ describe('Multilevel caching', () => { const result = await controller.get_fixedTTL(resourceId); // Value is returned from memory, so should be the actual value being returned by controller expect(result).toStrictEqual(Controller.returnVal); - - console.log('Test should complete here'); }, // Since there are two controller calls and the second one is cached, upper bound is 2*mockLatency Controller.mockLatency * 2)