@@ -1634,6 +1634,108 @@ describe('BaseClient', () => {
16341634 } ) ;
16351635 } ) ;
16361636
1637+ describe ( 'sendEvent' , ( ) => {
1638+ beforeEach ( ( ) => {
1639+ jest . useFakeTimers ( ) ;
1640+ } ) ;
1641+
1642+ afterEach ( ( ) => {
1643+ jest . useRealTimers ( ) ;
1644+ } ) ;
1645+
1646+ it ( 'emits `afterSendErrorEvent` when sending an error' , async ( ) => {
1647+ const client = new TestClient (
1648+ getDefaultTestClientOptions ( {
1649+ dsn : PUBLIC_DSN ,
1650+ enableSend : true ,
1651+ } ) ,
1652+ ) ;
1653+
1654+ // @ts -ignore
1655+ const mockSend = jest . spyOn ( client . _transport , 'send' ) ;
1656+
1657+ const errorEvent : Event = { message : 'error' } ;
1658+
1659+ const callback = jest . fn ( ) ;
1660+ client . on ( 'afterSendErrorEvent' , callback ) ;
1661+
1662+ client . sendEvent ( errorEvent ) ;
1663+ jest . runAllTimers ( ) ;
1664+ // Wait for two ticks
1665+ // note that for whatever reason, await new Promise(resolve => setTimeout(resolve, 0)) causes the test to hang
1666+ await undefined ;
1667+ await undefined ;
1668+
1669+ expect ( mockSend ) . toBeCalledTimes ( 1 ) ;
1670+ expect ( callback ) . toBeCalledTimes ( 1 ) ;
1671+ expect ( callback ) . toBeCalledWith ( errorEvent , { } ) ;
1672+ } ) ;
1673+
1674+ it ( 'still triggers `afterSendErrorEvent` when transport.send rejects' , async ( ) => {
1675+ expect . assertions ( 3 ) ;
1676+
1677+ const client = new TestClient (
1678+ getDefaultTestClientOptions ( {
1679+ dsn : PUBLIC_DSN ,
1680+ enableSend : true ,
1681+ } ) ,
1682+ ) ;
1683+
1684+ // @ts -ignore
1685+ const mockSend = jest . spyOn ( client . _transport , 'send' ) . mockImplementation ( ( ) => {
1686+ return Promise . reject ( 'send error' ) ;
1687+ } ) ;
1688+
1689+ const errorEvent : Event = { message : 'error' } ;
1690+
1691+ const callback = jest . fn ( ) ;
1692+ client . on ( 'afterSendErrorEvent' , callback ) ;
1693+
1694+ client . sendEvent ( errorEvent ) ;
1695+ jest . runAllTimers ( ) ;
1696+ // Wait for two ticks
1697+ // note that for whatever reason, await new Promise(resolve => setTimeout(resolve, 0)) causes the test to hang
1698+ await undefined ;
1699+ await undefined ;
1700+
1701+ expect ( mockSend ) . toBeCalledTimes ( 1 ) ;
1702+ expect ( callback ) . toBeCalledTimes ( 1 ) ;
1703+ expect ( callback ) . toBeCalledWith ( errorEvent , undefined ) ;
1704+ } ) ;
1705+
1706+ it ( 'passes the response to the hook' , async ( ) => {
1707+ expect . assertions ( 3 ) ;
1708+
1709+ const client = new TestClient (
1710+ getDefaultTestClientOptions ( {
1711+ dsn : PUBLIC_DSN ,
1712+ enableSend : true ,
1713+ } ) ,
1714+ ) ;
1715+
1716+ // @ts -ignore
1717+ const mockSend = jest . spyOn ( client . _transport , 'send' ) . mockImplementation ( ( ) => {
1718+ return Promise . resolve ( { statusCode : 200 } ) ;
1719+ } ) ;
1720+
1721+ const errorEvent : Event = { message : 'error' } ;
1722+
1723+ const callback = jest . fn ( ) ;
1724+ client . on ( 'afterSendErrorEvent' , callback ) ;
1725+
1726+ client . sendEvent ( errorEvent ) ;
1727+ jest . runAllTimers ( ) ;
1728+ // Wait for two ticks
1729+ // note that for whatever reason, await new Promise(resolve => setTimeout(resolve, 0)) causes the test to hang
1730+ await undefined ;
1731+ await undefined ;
1732+
1733+ expect ( mockSend ) . toBeCalledTimes ( 1 ) ;
1734+ expect ( callback ) . toBeCalledTimes ( 1 ) ;
1735+ expect ( callback ) . toBeCalledWith ( errorEvent , { statusCode : 200 } ) ;
1736+ } ) ;
1737+ } ) ;
1738+
16371739 describe ( 'captureSession()' , ( ) => {
16381740 test ( 'sends sessions to the client' , ( ) => {
16391741 expect . assertions ( 1 ) ;
0 commit comments