From 9c55ffc97d08867b127f2b64f42ea39ad3e733fb Mon Sep 17 00:00:00 2001 From: gabrieledm Date: Wed, 26 Aug 2026 15:39:34 +0200 Subject: [PATCH 1/2] test: used setup function in 'error handling' tests --- .../src/caching/useCache.test.ts | 50 ++++++++++++------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/packages/tron-wallet-snap/src/caching/useCache.test.ts b/packages/tron-wallet-snap/src/caching/useCache.test.ts index f01f182c..736705eb 100644 --- a/packages/tron-wallet-snap/src/caching/useCache.test.ts +++ b/packages/tron-wallet-snap/src/caching/useCache.test.ts @@ -193,35 +193,49 @@ describe('useCache', () => { describe('error handling', () => { it('should propagate errors from the original function', async () => { - const error = new Error('Test error'); - actualExecutionSpy.mockRejectedValueOnce(error); + await withUseCache( + async ({ actualExecutionSpy, cachedTestFunction, cache }) => { + const error = new Error('Test error'); + actualExecutionSpy.mockRejectedValueOnce(error); - await expect(cachedTestFunction()).rejects.toThrow('Test error'); - expect(cache.set).not.toHaveBeenCalled(); + await expect(cachedTestFunction()).rejects.toThrow('Test error'); + expect(cache.set).not.toHaveBeenCalled(); + }, + ); }); it('should handle cache get errors gracefully', async () => { - jest.spyOn(cache, 'get').mockRejectedValueOnce(new Error('Cache error')); - actualExecutionSpy.mockResolvedValueOnce('test'); + await withUseCache( + async ({ actualExecutionSpy, cachedTestFunction, cache }) => { + jest + .spyOn(cache, 'get') + .mockRejectedValueOnce(new Error('Cache error')); + actualExecutionSpy.mockResolvedValueOnce('test'); - const result = await cachedTestFunction(); + const result = await cachedTestFunction(); - expect(result).toBe('test'); - expect(actualExecutionSpy).toHaveBeenCalledTimes(1); - expect(cache.set).toHaveBeenCalledWith('testFunction:', 'test', 1000); + expect(result).toBe('test'); + expect(actualExecutionSpy).toHaveBeenCalledTimes(1); + expect(cache.set).toHaveBeenCalledWith('testFunction:', 'test', 1000); + }, + ); }); it('should handle cache set errors gracefully', async () => { - jest.spyOn(cache, 'get').mockResolvedValue(undefined); - jest - .spyOn(cache, 'set') - .mockRejectedValueOnce(new Error('Cache set error')); - actualExecutionSpy.mockResolvedValueOnce('test'); + await withUseCache( + async ({ actualExecutionSpy, cachedTestFunction, cache }) => { + jest.spyOn(cache, 'get').mockResolvedValue(undefined); + jest + .spyOn(cache, 'set') + .mockRejectedValueOnce(new Error('Cache set error')); + actualExecutionSpy.mockResolvedValueOnce('test'); - const result = await cachedTestFunction(); + const result = await cachedTestFunction(); - expect(result).toBe('test'); - expect(actualExecutionSpy).toHaveBeenCalledTimes(1); + expect(result).toBe('test'); + expect(actualExecutionSpy).toHaveBeenCalledTimes(1); + }, + ); }); }); From e0c2ae1803e39c7ee0a0804e99d4126657a7f08c Mon Sep 17 00:00:00 2001 From: gabrieledm Date: Wed, 26 Aug 2026 15:44:35 +0200 Subject: [PATCH 2/2] test: removed usage of spyOn for cache mock --- packages/tron-wallet-snap/src/caching/useCache.test.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/packages/tron-wallet-snap/src/caching/useCache.test.ts b/packages/tron-wallet-snap/src/caching/useCache.test.ts index 736705eb..0ef139df 100644 --- a/packages/tron-wallet-snap/src/caching/useCache.test.ts +++ b/packages/tron-wallet-snap/src/caching/useCache.test.ts @@ -207,9 +207,7 @@ describe('useCache', () => { it('should handle cache get errors gracefully', async () => { await withUseCache( async ({ actualExecutionSpy, cachedTestFunction, cache }) => { - jest - .spyOn(cache, 'get') - .mockRejectedValueOnce(new Error('Cache error')); + cache.get.mockRejectedValueOnce(new Error('Cache error')); actualExecutionSpy.mockResolvedValueOnce('test'); const result = await cachedTestFunction(); @@ -224,10 +222,8 @@ describe('useCache', () => { it('should handle cache set errors gracefully', async () => { await withUseCache( async ({ actualExecutionSpy, cachedTestFunction, cache }) => { - jest.spyOn(cache, 'get').mockResolvedValue(undefined); - jest - .spyOn(cache, 'set') - .mockRejectedValueOnce(new Error('Cache set error')); + cache.get.mockResolvedValue(undefined); + cache.set.mockRejectedValueOnce(new Error('Cache set error')); actualExecutionSpy.mockResolvedValueOnce('test'); const result = await cachedTestFunction();