From d03b7f5f503acfd847d060dd86c5d9cc4b8c3751 Mon Sep 17 00:00:00 2001 From: Bobby Bonestell Date: Sat, 15 Jun 2024 13:27:01 -0600 Subject: [PATCH] fix(test): Fixed improper mocking of ioredis resources in unit tests (#11) * Updated Redlock to accept db configuration option to specify redis db index which to use for single-instance configurations with cluster mode disabled * Updated README.md to correct Redis references statements for ioredis, and added details for configuring redlock to use a specific DB index for single instance with cluster mode disabled * Updated tests, created unit tests in unit.spec.ts and renamed index.spec.ts to system.spec.ts to distinguish unit from system tests. Updated package.json accordingly. Added system tests to cover use cases for configuring a specific DB number. Added unit test coverage for Redlock settings and acquire, extend, and release methods. * Updated yarn.lock to resolve high severity dependency vulnerability * Resolved mistake mocking ioredis resources which lead to unit tests attempting to connect to a redis host when instantiating an object of the Redis class --- src/unit.spec.ts | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/unit.spec.ts b/src/unit.spec.ts index 1b30c5f..cc3b8d8 100644 --- a/src/unit.spec.ts +++ b/src/unit.spec.ts @@ -1,8 +1,11 @@ -import Redis from 'ioredis'; -import { beforeEach, describe, expect, test, vi } from 'vitest'; +import { Redis } from 'ioredis'; +import { beforeAll, describe, expect, test, vi } from 'vitest'; import { Redlock, Lock, ExecutionError, Settings } from './index'; +// Mock all resources from ioredis +vi.mock('ioredis'); + describe('Redlock Settings', () => { test('Default settings are applied if none are provided', () => { const client = new Redis(); @@ -40,12 +43,14 @@ describe('Redlock Settings', () => { expect(redlock.settings.db).toBe(0); }); - test.each([-1, 16, 0.5, 3.1514])('Redis DB defaults to 0 when value (%s) is outside of acceptable range (0-15)', (db: number) => { - const client = new Redis(); - const redlock = new Redlock([client], { db }); - expect(redlock.settings.db).toBe(0); - }); - + test.each([-1, 16, 0.5, 3.1514])( + 'Redis DB defaults to 0 when value (%s) is outside of acceptable range (0-15)', + (db: number) => { + const client = new Redis(); + const redlock = new Redlock([client], { db }); + expect(redlock.settings.db).toBe(0); + }, + ); }); describe('Redlock', () => { @@ -59,16 +64,8 @@ describe('Redlock', () => { automaticExtensionThreshold: 500, }; - beforeEach(() => { - redisClient = { - evalsha: vi.fn(), - get: vi.fn(), - set: vi.fn(), - quit: vi.fn(), - acquireLock: vi.fn(), - releaseLock: vi.fn(), - extendLock: vi.fn() - } as Redis; + beforeAll(() => { + redisClient = new Redis(); redlock = new Redlock([redisClient], defaultSettings); });