Skip to content

Commit e0cf2ef

Browse files
committed
fix(module-analytics): reject waitForAnalytic when a predicate matcher throws
A throwing predicate matcher surfaced on RxJS's error channel, but the subscribe observer had no error handler — the exception was reported globally and the returned promise stayed pending forever. Add an error handler that rejects through the same cleanup path, and cover it with a test.
1 parent adbcd62 commit e0cf2ef

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

packages/modules/analytics/src/__tests__/MockAnalyticsAdapter.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ describe('MockAnalyticsAdapter', () => {
103103
await expect(promise).rejects.toThrow('disposed before a matching event was recorded');
104104
});
105105

106+
it('rejects instead of hanging when a predicate matcher throws on a future event', async () => {
107+
const adapter = new MockAnalyticsAdapter();
108+
const boom = new Error('predicate boom');
109+
110+
const promise = adapter.waitForAnalytic(() => {
111+
throw boom;
112+
});
113+
adapter.registerAnalytic(createEvent('button-click'));
114+
115+
await expect(promise).rejects.toThrow(boom);
116+
});
117+
106118
it('does not interfere with events recorded by another adapter instance', () => {
107119
const adapterA = new MockAnalyticsAdapter();
108120
const adapterB = new MockAnalyticsAdapter();

packages/modules/analytics/src/mock/MockAnalyticsAdapter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ export class MockAnalyticsAdapter<T extends AnalyticsEvent = AnalyticsEvent>
123123
cleanup();
124124
resolve(event);
125125
},
126+
// A throwing predicate matcher surfaces here instead of hanging the promise forever.
127+
error: (err) => {
128+
cleanup();
129+
reject(err);
130+
},
126131
complete: () => {
127132
cleanup();
128133
reject(new Error('MockAnalyticsAdapter disposed before a matching event was recorded'));

0 commit comments

Comments
 (0)