Skip to content

Commit

Permalink
fix(test): Fixed improper mocking of ioredis resources in unit tests (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
rbonestell authored Jun 15, 2024
1 parent 39644cf commit d03b7f5
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions src/unit.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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', () => {
Expand All @@ -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);
});

Expand Down

0 comments on commit d03b7f5

Please sign in to comment.