diff --git a/src/PollingBlockTracker.test.ts b/src/PollingBlockTracker.test.ts index e3c3616..e367efa 100644 --- a/src/PollingBlockTracker.test.ts +++ b/src/PollingBlockTracker.test.ts @@ -371,7 +371,8 @@ describe('PollingBlockTracker', () => { }); METHODS_TO_ADD_LISTENER.forEach((methodToAddListener) => { - it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should not throw if, while making the request for the latest block number, the provider throws an Error`, async () => { + it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should not throw if, while making the request for the latest block number, the provider throws`, async () => { + const thrownError = new Error('boom'); recordCallsToSetTimeout({ numAutomaticCalls: 1 }); await withPollingBlockTracker( @@ -381,7 +382,7 @@ describe('PollingBlockTracker', () => { { methodName: 'eth_blockNumber', implementation: () => { - throw new Error('boom'); + throw thrownError; }, }, { @@ -392,60 +393,20 @@ describe('PollingBlockTracker', () => { }, }, async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); + const errorListener = jest.fn(); + blockTracker[methodToAddListener]('error', errorListener); const promiseForLatestBlock = blockTracker.getLatestBlock(); - const caughtError = await promiseForCaughtError; - expect(caughtError.message).toMatch( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: boom\n/u, - ); - const latestBlock = await promiseForLatestBlock; - expect(latestBlock).toBe('0x0'); - }, - ); - }); - - it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should not throw if, while making the request for the latest block number, the provider throws a string`, async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withPollingBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - implementation: () => { - throw 'boom'; - }, - }, - { - methodName: 'eth_blockNumber', - result: '0x0', - }, - ], - }, - }, - async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); - - const promiseForLatestBlock = blockTracker.getLatestBlock(); - - const caughtError = await promiseForCaughtError; - expect(caughtError.message).toMatch( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nboom/u, - ); const latestBlock = await promiseForLatestBlock; + expect(errorListener).toHaveBeenCalledWith(thrownError); expect(latestBlock).toBe('0x0'); }, ); }); it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should not throw if, while making the request for the latest block number, the provider rejects with an error`, async () => { + const thrownError = new Error('boom'); recordCallsToSetTimeout({ numAutomaticCalls: 1 }); await withPollingBlockTracker( @@ -454,7 +415,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - error: new Error('boom'), + error: thrownError, }, { methodName: 'eth_blockNumber', @@ -464,59 +425,21 @@ describe('PollingBlockTracker', () => { }, }, async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); + const errorListener = jest.fn(); + blockTracker[methodToAddListener]('error', errorListener); const promiseForLatestBlock = blockTracker.getLatestBlock(); - const caughtError = await promiseForCaughtError; - expect(caughtError.message).toMatch( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: boom/u, - ); const latestBlock = await promiseForLatestBlock; + expect(errorListener).toHaveBeenCalledWith(thrownError); expect(latestBlock).toBe('0x0'); }, ); }); }); - it('should log an error if, while making a request for the latest block number, the provider throws an Error and there is nothing listening to "error"', async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withPollingBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - implementation: () => { - throw new Error('boom'); - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - jest.spyOn(console, 'error').mockImplementation(EMPTY_FUNCTION); - - blockTracker.getLatestBlock(); - await new Promise((resolve) => { - blockTracker.on('_waitingForNextIteration', resolve); - }); - - expect(console.error).toHaveBeenCalledWith( - expect.objectContaining({ - message: expect.stringMatching( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: boom/u, - ), - }), - ); - }, - ); - }); - - it('should log an error the request for the latest block number throws a string and there is nothing listening to "error"', async () => { + it('should log an error if, while making a request for the latest block number, the provider throws and there is nothing listening to "error"', async () => { + const thrownError = new Error('boom'); recordCallsToSetTimeout({ numAutomaticCalls: 1 }); await withPollingBlockTracker( @@ -526,7 +449,7 @@ describe('PollingBlockTracker', () => { { methodName: 'eth_blockNumber', implementation: () => { - throw 'boom'; + throw thrownError; }, }, ], @@ -541,17 +464,14 @@ describe('PollingBlockTracker', () => { }); expect(console.error).toHaveBeenCalledWith( - expect.objectContaining({ - message: expect.stringMatching( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nboom/u, - ), - }), + 'Error updating latest block: boom', ); }, ); }); - it('should log an error if, while requesting the latest block number, the provider rejects with an error and there is nothing listening to "error"', async () => { + it('should log an error if, while requesting the latest block number, the provider rejects and there is nothing listening to "error"', async () => { + const thrownError = new Error('boom'); recordCallsToSetTimeout({ numAutomaticCalls: 1 }); await withPollingBlockTracker( @@ -560,7 +480,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - error: new Error('boom'), + error: thrownError, }, ], }, @@ -574,11 +494,7 @@ describe('PollingBlockTracker', () => { }); expect(console.error).toHaveBeenCalledWith( - expect.objectContaining({ - message: expect.stringMatching( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: boom/u, - ), - }), + 'Error updating latest block: boom', ); }, ); @@ -1217,7 +1133,8 @@ describe('PollingBlockTracker', () => { ); }); - it(`should emit the "error" event and should not kill the block tracker if, while making the request for the latest block number, the provider throws an Error`, async () => { + it(`should emit the "error" event and should not kill the block tracker if, while making the request for the latest block number, the provider throws`, async () => { + const thrownError = new Error('boom'); recordCallsToSetTimeout({ numAutomaticCalls: 1 }); await withPollingBlockTracker( @@ -1227,7 +1144,7 @@ describe('PollingBlockTracker', () => { { methodName: 'eth_blockNumber', implementation: () => { - throw new Error('boom'); + throw thrownError; }, }, { @@ -1238,64 +1155,22 @@ describe('PollingBlockTracker', () => { }, }, async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); + const errorListener = jest.fn(); + blockTracker[methodToAddListener]('error', errorListener); const promiseForLatestBlock = new Promise((resolve) => { blockTracker[methodToAddListener]('latest', resolve); }); - const caughtError = await promiseForCaughtError; - expect(caughtError.message).toMatch( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: boom\n/u, - ); - const latestBlock = await promiseForLatestBlock; - expect(latestBlock).toBe('0x0'); - }, - ); - }); - - it(`should emit the "error" event and should not kill the block tracker if, while making the request for the latest block number, the provider throws a string`, async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withPollingBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - implementation: () => { - throw 'boom'; - }, - }, - { - methodName: 'eth_blockNumber', - result: '0x0', - }, - ], - }, - }, - async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); - - const promiseForLatestBlock = new Promise((resolve) => { - blockTracker[methodToAddListener]('latest', resolve); - }); - - const caughtError = await promiseForCaughtError; - expect(caughtError.message).toMatch( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nboom/u, - ); const latestBlock = await promiseForLatestBlock; + expect(errorListener).toHaveBeenCalledWith(thrownError); expect(latestBlock).toBe('0x0'); }, ); }); it(`should emit the "error" event and should not kill the block tracker if, while making the request for the latest block number, the provider rejects with an error`, async () => { + const thrownError = new Error('boom'); recordCallsToSetTimeout({ numAutomaticCalls: 1 }); await withPollingBlockTracker( @@ -1304,7 +1179,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - error: new Error('boom'), + error: thrownError, }, { methodName: 'eth_blockNumber', @@ -1314,64 +1189,22 @@ describe('PollingBlockTracker', () => { }, }, async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); + const errorListener = jest.fn(); + blockTracker[methodToAddListener]('error', errorListener); const promiseForLatestBlock = new Promise((resolve) => { blockTracker[methodToAddListener]('latest', resolve); }); - const caughtError = await promiseForCaughtError; - expect(caughtError.message).toMatch( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: boom/u, - ); const latestBlock = await promiseForLatestBlock; + expect(errorListener).toHaveBeenCalledWith(thrownError); expect(latestBlock).toBe('0x0'); }, ); }); - it('should log an error if, while making a request for the latest block number, the provider throws an Error and there is nothing listening to "error"', async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withPollingBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - implementation: () => { - throw new Error('boom'); - }, - }, - { - methodName: 'eth_blockNumber', - result: '0x0', - }, - ], - }, - }, - async ({ blockTracker }) => { - jest.spyOn(console, 'error').mockImplementation(EMPTY_FUNCTION); - - blockTracker[methodToAddListener]('latest', EMPTY_FUNCTION); - await new Promise((resolve) => { - blockTracker.on('_waitingForNextIteration', resolve); - }); - - expect(console.error).toHaveBeenCalledWith( - expect.objectContaining({ - message: expect.stringMatching( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: boom/u, - ), - }), - ); - }, - ); - }); - - it('should log an error if, while making a request for the latest block number, the provider throws a string and there is nothing listening to "error"', async () => { + it('should log an error if, while making a request for the latest block number, the provider throws and there is nothing listening to "error"', async () => { + const thrownError = new Error('boom'); recordCallsToSetTimeout({ numAutomaticCalls: 1 }); await withPollingBlockTracker( @@ -1381,7 +1214,7 @@ describe('PollingBlockTracker', () => { { methodName: 'eth_blockNumber', implementation: () => { - throw 'boom'; + throw thrownError; }, }, { @@ -1400,17 +1233,14 @@ describe('PollingBlockTracker', () => { }); expect(console.error).toHaveBeenCalledWith( - expect.objectContaining({ - message: expect.stringMatching( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nboom/u, - ), - }), + 'Error updating latest block: boom', ); }, ); }); it('should log an error if, while making the request for the latest block number, the provider rejects with an error and there is nothing listening to "error"', async () => { + const thrownError = new Error('boom'); recordCallsToSetTimeout({ numAutomaticCalls: 1 }); await withPollingBlockTracker( @@ -1419,7 +1249,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - error: new Error('boom'), + error: thrownError, }, { methodName: 'eth_blockNumber', @@ -1437,11 +1267,7 @@ describe('PollingBlockTracker', () => { }); expect(console.error).toHaveBeenCalledWith( - expect.objectContaining({ - message: expect.stringMatching( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: boom/u, - ), - }), + 'Error updating latest block: boom', ); }, ); @@ -1849,7 +1675,8 @@ describe('PollingBlockTracker', () => { ); }); - it(`should emit the "error" event and should not kill the block tracker if, while making the request for the latest block number, the provider throws an Error`, async () => { + it(`should emit the "error" event and should not kill the block tracker if, while making the request for the latest block number, the provider throws`, async () => { + const thrownError = new Error('boom'); recordCallsToSetTimeout({ numAutomaticCalls: 1 }); await withPollingBlockTracker( @@ -1859,7 +1686,7 @@ describe('PollingBlockTracker', () => { { methodName: 'eth_blockNumber', implementation: () => { - throw new Error('boom'); + throw thrownError; }, }, { @@ -1870,64 +1697,22 @@ describe('PollingBlockTracker', () => { }, }, async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); + const errorListener = jest.fn(); + blockTracker[methodToAddListener]('error', errorListener); const promiseForSync = new Promise((resolve) => { blockTracker[methodToAddListener]('sync', resolve); }); - const caughtError = await promiseForCaughtError; - expect(caughtError.message).toMatch( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: boom\n/u, - ); - const sync = await promiseForSync; - expect(sync).toStrictEqual({ oldBlock: null, newBlock: '0x0' }); - }, - ); - }); - - it(`should emit the "error" event and should not kill the block tracker if, while making the request for the latest block number, the provider throws a string`, async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withPollingBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - implementation: () => { - throw 'boom'; - }, - }, - { - methodName: 'eth_blockNumber', - result: '0x0', - }, - ], - }, - }, - async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); - - const promiseForSync = new Promise((resolve) => { - blockTracker[methodToAddListener]('sync', resolve); - }); - - const caughtError = await promiseForCaughtError; - expect(caughtError.message).toMatch( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nboom/u, - ); const sync = await promiseForSync; + expect(errorListener).toHaveBeenCalledWith(thrownError); expect(sync).toStrictEqual({ oldBlock: null, newBlock: '0x0' }); }, ); }); it(`should emit the "error" event and should not kill the block tracker if, while making the request for the latest block number, the provider rejects with an error`, async () => { + const thrownError = new Error('boom'); recordCallsToSetTimeout({ numAutomaticCalls: 1 }); await withPollingBlockTracker( @@ -1946,60 +1731,22 @@ describe('PollingBlockTracker', () => { }, }, async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); + const errorListener = jest.fn(); + blockTracker[methodToAddListener]('error', errorListener); const promiseForSync = new Promise((resolve) => { blockTracker[methodToAddListener]('sync', resolve); }); - const caughtError = await promiseForCaughtError; - expect(caughtError.message).toMatch( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: boom/u, - ); const sync = await promiseForSync; + expect(errorListener).toHaveBeenCalledWith(thrownError); expect(sync).toStrictEqual({ oldBlock: null, newBlock: '0x0' }); }, ); }); - it('should log an error if, while making a request for the latest block number, the provider throws an Error and there is nothing listening to "error"', async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withPollingBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - implementation: () => { - throw new Error('boom'); - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - jest.spyOn(console, 'error').mockImplementation(EMPTY_FUNCTION); - - blockTracker[methodToAddListener]('sync', EMPTY_FUNCTION); - await new Promise((resolve) => { - blockTracker.on('_waitingForNextIteration', resolve); - }); - - expect(console.error).toHaveBeenCalledWith( - expect.objectContaining({ - message: expect.stringMatching( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: boom/u, - ), - }), - ); - }, - ); - }); - - it('should log an error if, while making a request for the latest block number, the provider throws a string and there is nothing listening to "error"', async () => { + it('should log an error if, while making a request for the latest block number, the provider throws and there is nothing listening to "error"', async () => { + const thrownError = new Error('boom'); recordCallsToSetTimeout({ numAutomaticCalls: 1 }); await withPollingBlockTracker( @@ -2009,7 +1756,7 @@ describe('PollingBlockTracker', () => { { methodName: 'eth_blockNumber', implementation: () => { - throw 'boom'; + throw thrownError; }, }, ], @@ -2024,17 +1771,14 @@ describe('PollingBlockTracker', () => { }); expect(console.error).toHaveBeenCalledWith( - expect.objectContaining({ - message: expect.stringMatching( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nboom/u, - ), - }), + 'Error updating latest block: boom', ); }, ); }); it('should log an error if, while making the request for the latest block number, the provider rejects with an error and there is nothing listening to "error"', async () => { + const thrownError = new Error('boom'); recordCallsToSetTimeout({ numAutomaticCalls: 1 }); await withPollingBlockTracker( @@ -2043,7 +1787,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - error: new Error('boom'), + error: thrownError, }, ], }, @@ -2057,11 +1801,7 @@ describe('PollingBlockTracker', () => { }); expect(console.error).toHaveBeenCalledWith( - expect.objectContaining({ - message: expect.stringMatching( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: boom/u, - ), - }), + 'Error updating latest block: boom', ); }, ); @@ -2666,46 +2406,8 @@ describe('PollingBlockTracker', () => { }); METHODS_TO_ADD_LISTENER.forEach((methodToAddListener) => { - it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should not throw if, while making the request for the latest block number, the provider throws an Error`, async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withPollingBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - implementation: () => { - throw new Error('boom'); - }, - }, - { - methodName: 'eth_blockNumber', - result: '0x0', - }, - ], - }, - }, - async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); - - const promiseForLatestBlock = new Promise((resolve) => { - blockTracker.once('latest', resolve); - }); - - const caughtError = await promiseForCaughtError; - expect(caughtError.message).toMatch( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: boom\n/u, - ); - const latestBlock = await promiseForLatestBlock; - expect(latestBlock).toBe('0x0'); - }, - ); - }); - - it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should not throw if, while making the request for the latest block number, the provider throws a string`, async () => { + it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should not throw if, while making the request for the latest block number, the provider throws`, async () => { + const thrownError = new Error('boom'); recordCallsToSetTimeout({ numAutomaticCalls: 1 }); await withPollingBlockTracker( @@ -2715,7 +2417,7 @@ describe('PollingBlockTracker', () => { { methodName: 'eth_blockNumber', implementation: () => { - throw 'boom'; + throw thrownError; }, }, { @@ -2726,25 +2428,22 @@ describe('PollingBlockTracker', () => { }, }, async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); + const errorListener = jest.fn(); + blockTracker[methodToAddListener]('error', errorListener); const promiseForLatestBlock = new Promise((resolve) => { blockTracker.once('latest', resolve); }); - const caughtError = await promiseForCaughtError; - expect(caughtError.message).toMatch( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nboom/u, - ); const latestBlock = await promiseForLatestBlock; + expect(errorListener).toHaveBeenCalledWith(thrownError); expect(latestBlock).toBe('0x0'); }, ); }); it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should not throw if, while making the request for the latest block number, the provider rejects with an error`, async () => { + const thrownError = new Error('boom'); recordCallsToSetTimeout({ numAutomaticCalls: 1 }); await withPollingBlockTracker( @@ -2753,7 +2452,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - error: new Error('boom'), + error: thrownError, }, { methodName: 'eth_blockNumber', @@ -2763,65 +2462,23 @@ describe('PollingBlockTracker', () => { }, }, async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); + const errorListener = jest.fn(); + blockTracker[methodToAddListener]('error', errorListener); const promiseForLatestBlock = new Promise((resolve) => { blockTracker.once('latest', resolve); }); - const caughtError = await promiseForCaughtError; - expect(caughtError.message).toMatch( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: boom/u, - ); const latestBlock = await promiseForLatestBlock; + expect(errorListener).toHaveBeenCalledWith(thrownError); expect(latestBlock).toBe('0x0'); }, ); }); }); - it('should log an error if, while making a request for the latest block number, the provider throws an Error and there is nothing listening to "error"', async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withPollingBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - implementation: () => { - throw new Error('boom'); - }, - }, - { - methodName: 'eth_blockNumber', - result: '0x0', - }, - ], - }, - }, - async ({ blockTracker }) => { - jest.spyOn(console, 'error').mockImplementation(EMPTY_FUNCTION); - - blockTracker.once('latest', EMPTY_FUNCTION); - await new Promise((resolve) => { - blockTracker.on('_waitingForNextIteration', resolve); - }); - - expect(console.error).toHaveBeenCalledWith( - expect.objectContaining({ - message: expect.stringMatching( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: boom/u, - ), - }), - ); - }, - ); - }); - - it('should log an error if, while making a request for the latest block number, the provider throws a string and there is nothing listening to "error"', async () => { + it('should log an error if, while making a request for the latest block number, the provider throws and there is nothing listening to "error"', async () => { + const thrownError = new Error('boom'); recordCallsToSetTimeout({ numAutomaticCalls: 1 }); await withPollingBlockTracker( @@ -2831,7 +2488,7 @@ describe('PollingBlockTracker', () => { { methodName: 'eth_blockNumber', implementation: () => { - throw 'boom'; + throw thrownError; }, }, { @@ -2850,17 +2507,14 @@ describe('PollingBlockTracker', () => { }); expect(console.error).toHaveBeenCalledWith( - expect.objectContaining({ - message: expect.stringMatching( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nboom/u, - ), - }), + 'Error updating latest block: boom', ); }, ); }); it('should log an error if, while making the request for the latest block number, the provider rejects with an error and there is nothing listening to "error"', async () => { + const thrownError = new Error('boom'); recordCallsToSetTimeout({ numAutomaticCalls: 1 }); await withPollingBlockTracker( @@ -2869,7 +2523,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - error: new Error('boom'), + error: thrownError, }, { methodName: 'eth_blockNumber', @@ -2887,11 +2541,7 @@ describe('PollingBlockTracker', () => { }); expect(console.error).toHaveBeenCalledWith( - expect.objectContaining({ - message: expect.stringMatching( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: boom/u, - ), - }), + 'Error updating latest block: boom', ); }, ); @@ -2953,46 +2603,8 @@ describe('PollingBlockTracker', () => { }); METHODS_TO_ADD_LISTENER.forEach((methodToAddListener) => { - it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should not throw if, while making the request for the latest block number, the provider throws an Error`, async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withPollingBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - implementation: () => { - throw new Error('boom'); - }, - }, - { - methodName: 'eth_blockNumber', - result: '0x0', - }, - ], - }, - }, - async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); - - const promiseForSync = new Promise((resolve) => { - blockTracker.once('sync', resolve); - }); - - const caughtError = await promiseForCaughtError; - expect(caughtError.message).toMatch( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: boom\n/u, - ); - const sync = await promiseForSync; - expect(sync).toStrictEqual({ oldBlock: null, newBlock: '0x0' }); - }, - ); - }); - - it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should not throw if, while making the request for the latest block number, the provider throws a string`, async () => { + it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should not throw if, while making the request for the latest block number, the provider throws`, async () => { + const thrownError = new Error('boom'); recordCallsToSetTimeout({ numAutomaticCalls: 1 }); await withPollingBlockTracker( @@ -3002,7 +2614,7 @@ describe('PollingBlockTracker', () => { { methodName: 'eth_blockNumber', implementation: () => { - throw 'boom'; + throw thrownError; }, }, { @@ -3013,25 +2625,22 @@ describe('PollingBlockTracker', () => { }, }, async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); + const errorListener = jest.fn(); + blockTracker[methodToAddListener]('error', errorListener); const promiseForSync = new Promise((resolve) => { blockTracker.once('sync', resolve); }); - const caughtError = await promiseForCaughtError; - expect(caughtError.message).toMatch( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nboom/u, - ); const sync = await promiseForSync; + expect(errorListener).toHaveBeenCalledWith(thrownError); expect(sync).toStrictEqual({ oldBlock: null, newBlock: '0x0' }); }, ); }); it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should not throw if, while making the request for the latest block number, the provider rejects with an error`, async () => { + const thrownError = new Error('boom'); recordCallsToSetTimeout({ numAutomaticCalls: 1 }); await withPollingBlockTracker( @@ -3040,7 +2649,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - error: new Error('boom'), + error: thrownError, }, { methodName: 'eth_blockNumber', @@ -3050,65 +2659,23 @@ describe('PollingBlockTracker', () => { }, }, async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); + const errorListener = jest.fn(); + blockTracker[methodToAddListener]('error', errorListener); const promiseForSync = new Promise((resolve) => { blockTracker.once('sync', resolve); }); - const caughtError = await promiseForCaughtError; - expect(caughtError.message).toMatch( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: boom/u, - ); const sync = await promiseForSync; + expect(errorListener).toHaveBeenCalledWith(thrownError); expect(sync).toStrictEqual({ oldBlock: null, newBlock: '0x0' }); }, ); }); }); - it('should log an error if, while making a request for the latest block number, the provider throws an Error and there is nothing listening to "error"', async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withPollingBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - implementation: () => { - throw new Error('boom'); - }, - }, - { - methodName: 'eth_blockNumber', - result: '0x0', - }, - ], - }, - }, - async ({ blockTracker }) => { - jest.spyOn(console, 'error').mockImplementation(EMPTY_FUNCTION); - - blockTracker.once('sync', EMPTY_FUNCTION); - await new Promise((resolve) => { - blockTracker.on('_waitingForNextIteration', resolve); - }); - - expect(console.error).toHaveBeenCalledWith( - expect.objectContaining({ - message: expect.stringMatching( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: boom/u, - ), - }), - ); - }, - ); - }); - - it('should log an error if, while making a request for the latest block number, the provider throws a string and there is nothing listening to "error"', async () => { + it('should log an error if, while making a request for the latest block number, the provider throws and there is nothing listening to "error"', async () => { + const thrownError = new Error('boom'); recordCallsToSetTimeout({ numAutomaticCalls: 1 }); await withPollingBlockTracker( @@ -3118,7 +2685,7 @@ describe('PollingBlockTracker', () => { { methodName: 'eth_blockNumber', implementation: () => { - throw 'boom'; + throw thrownError; }, }, { @@ -3137,17 +2704,14 @@ describe('PollingBlockTracker', () => { }); expect(console.error).toHaveBeenCalledWith( - expect.objectContaining({ - message: expect.stringMatching( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nboom/u, - ), - }), + 'Error updating latest block: boom', ); }, ); }); it('should log an error if, while making the request for the latest block number, the provider rejects with an error and there is nothing listening to "error"', async () => { + const thrownError = new Error('boom'); recordCallsToSetTimeout({ numAutomaticCalls: 1 }); await withPollingBlockTracker( @@ -3156,7 +2720,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - error: new Error('boom'), + error: thrownError, }, { methodName: 'eth_blockNumber', @@ -3174,11 +2738,7 @@ describe('PollingBlockTracker', () => { }); expect(console.error).toHaveBeenCalledWith( - expect.objectContaining({ - message: expect.stringMatching( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: boom/u, - ), - }), + 'Error updating latest block: boom', ); }, ); diff --git a/src/PollingBlockTracker.ts b/src/PollingBlockTracker.ts index b0ad70d..9b60d33 100644 --- a/src/PollingBlockTracker.ts +++ b/src/PollingBlockTracker.ts @@ -319,17 +319,11 @@ export class PollingBlockTracker try { await this._updateLatestBlock(); - } catch (err: any) { - const newErr = new Error( - `PollingBlockTracker - encountered an error while attempting to update latest block:\n${ - err.stack ?? err - }`, - ); - + } catch (error: unknown) { try { - this.emit('error', newErr); - } catch (emitErr) { - console.error(newErr); + this.emit('error', error); + } catch { + console.error(`Error updating latest block: ${getErrorMessage(error)}`); } interval = this._retryTimeout;