UNPKG

syntropylog

Version:

An instance manager with observability for Node.js applications

170 lines (164 loc) • 5 kB
/** * FILE: src/testing/BeaconRedisMock.ts * DESCRIPTION: A mock implementation of IBeaconRedis for use in unit tests. * This mock is framework agnostic and works with both Vitest and Jest. */ /** * Creates a simple agnostic mock function without spy capabilities */ function createAgnosticMockFn(implementation) { const mockFn = (...args) => { if (implementation) { return implementation(...args); } return undefined; }; // Basic mock properties mockFn.mockClear = () => { }; mockFn.mockReset = () => { }; mockFn.mockImplementation = (impl) => { return createAgnosticMockFn(impl); }; mockFn.mockReturnValue = (value) => { return createAgnosticMockFn(() => value); }; mockFn.mockResolvedValue = (value) => { return createAgnosticMockFn(() => Promise.resolve(value)); }; mockFn.mockRejectedValue = (value) => { return createAgnosticMockFn(() => Promise.reject(value)); }; return mockFn; } // Function that throws error for eval in transaction - outside of any mock context const throwEvalError = () => { throw new Error('EVAL not supported in transaction (mocked BeaconRedisMock)'); }; export class BeaconRedisMock { spyFn = null; // Core methods - will be initialized in constructor getInstanceName; connect; disconnect; quit; updateConfig; multi; get; set; del; exists; expire; ttl; incr; decr; incrBy; decrBy; hGet; hSet; hGetAll; hDel; hExists; hIncrBy; lPush; rPush; lPop; rPop; lRange; lLen; lTrim; sAdd; sMembers; sIsMember; sRem; sCard; zAdd; zRange; zRangeWithScores; zRem; zCard; zScore; subscribe; unsubscribe; publish; ping; info; eval; constructor(spyFn) { this.spyFn = spyFn || null; // Initialize mocks after spyFn is set this.getInstanceName = this.createMock(); this.connect = this.createMock().mockResolvedValue(undefined); this.disconnect = this.createMock().mockResolvedValue(undefined); this.quit = this.createMock().mockResolvedValue(undefined); this.updateConfig = this.createMock(); this.multi = this.createMock().mockReturnValue(this.createTransactionObject()); this.get = this.createMock(); this.set = this.createMock(); this.del = this.createMock(); this.exists = this.createMock(); this.expire = this.createMock(); this.ttl = this.createMock(); this.incr = this.createMock(); this.decr = this.createMock(); this.incrBy = this.createMock(); this.decrBy = this.createMock(); this.hGet = this.createMock(); this.hSet = this.createMock(); this.hGetAll = this.createMock(); this.hDel = this.createMock(); this.hExists = this.createMock(); this.hIncrBy = this.createMock(); this.lPush = this.createMock(); this.rPush = this.createMock(); this.lPop = this.createMock(); this.rPop = this.createMock(); this.lRange = this.createMock(); this.lLen = this.createMock(); this.lTrim = this.createMock(); this.sAdd = this.createMock(); this.sMembers = this.createMock(); this.sIsMember = this.createMock(); this.sRem = this.createMock(); this.sCard = this.createMock(); this.zAdd = this.createMock(); this.zRange = this.createMock(); this.zRangeWithScores = this.createMock(); this.zRem = this.createMock(); this.zCard = this.createMock(); this.zScore = this.createMock(); this.subscribe = this.createMock(); this.unsubscribe = this.createMock(); this.publish = this.createMock(); this.ping = this.createMock(); this.info = this.createMock(); this.eval = this.createMock(); } // Create transaction object outside of mock to avoid hoisting issues createTransactionObject() { return { exec: this.createMock().mockResolvedValue([]), // eval is not implemented in transactions, so it should throw eval: throwEvalError, }; } createMock(implementation) { if (!this.spyFn) { throw new Error(` 🚨 SPY FUNCTION NOT INJECTED! 😔 To use spy functions like toHaveBeenCalled(), toHaveBeenCalledWith(), etc. YOU MUST inject your spy function in the constructor: // For Vitest: const mockRedis = new BeaconRedisMock(vi.fn); // For Jest: const mockRedis = new BeaconRedisMock(jest.fn); // For Jasmine: const mockRedis = new BeaconRedisMock(jasmine.createSpy); // Without spy (basic functionality only): const mockRedis = new BeaconRedisMock(); DON'T FORGET AGAIN! 😤 `); } return this.spyFn(implementation); } } //# sourceMappingURL=BeaconRedisMock.js.map