Skip to content

Commit 20b3e38

Browse files
authored
fix(integrations): Fix type/async errors in Offline tests (#4755)
This fixes a number of longstanding types errors in the tests for the `Offline` integration. It also simplifies some of the async code (to avoid mixing async functions and `done()`) and changes a mock so it is no longer assigning to a read-only property. Getting these fixed will help unmuddy the waters with respect to the `Offline` test errors which are cropping up in our TS upgrade.
1 parent 2209dab commit 20b3e38

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

packages/integrations/src/offline.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ type LocalForage = {
1111
callback?: (err: any, result: U) => void,
1212
): Promise<U>;
1313
removeItem(key: string, callback?: (err: any) => void): Promise<void>;
14+
length(): Promise<number>;
1415
};
1516

17+
export type Item = { key: string; value: Event };
18+
1619
/**
1720
* cache offline errors and send when connected
1821
*/

packages/integrations/test/offline.test.ts

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
import { Event, EventProcessor, Hub, Integration } from '@sentry/types';
1+
import { Event, EventProcessor, Hub, Integration, IntegrationClass } from '@sentry/types';
22
import * as utils from '@sentry/utils';
33

4-
import { Offline } from '../src/offline';
4+
import { Item, Offline } from '../src/offline';
55

66
// mock localforage methods
77
jest.mock('localforage', () => ({
88
createInstance(_options: { name: string }): any {
9-
let items: { key: string; value: Event }[] = [];
9+
let items: Item[] = [];
1010

1111
return {
12-
async getItem(key: string): Event {
12+
async getItem(key: string): Promise<Item | void> {
1313
return items.find(item => item.key === key);
1414
},
15-
async iterate(callback: () => void): void {
15+
async iterate(callback: (event: Event, key: string, index: number) => void): Promise<void> {
1616
items.forEach((item, index) => {
1717
callback(item.value, item.key, index);
1818
});
1919
},
20-
async length(): number {
20+
async length(): Promise<number> {
2121
return items.length;
2222
},
23-
async removeItem(key: string): void {
23+
async removeItem(key: string): Promise<void> {
2424
items = items.filter(item => item.key !== key);
2525
},
26-
async setItem(key: string, value: Event): void {
26+
async setItem(key: string, value: Event): Promise<void> {
2727
items.push({
2828
key,
2929
value,
@@ -33,7 +33,7 @@ jest.mock('localforage', () => ({
3333
},
3434
}));
3535

36-
let integration: Integration;
36+
let integration: Offline;
3737
let online: boolean;
3838

3939
describe('Offline', () => {
@@ -52,15 +52,10 @@ describe('Offline', () => {
5252
});
5353

5454
describe('when there are already events in the cache from a previous offline session', () => {
55-
beforeEach(done => {
55+
beforeEach(async () => {
5656
const event = { message: 'previous event' };
5757

58-
integration.offlineEventStore
59-
.setItem('previous', event)
60-
.then(() => {
61-
done();
62-
})
63-
.catch(error => error);
58+
await integration.offlineEventStore.setItem('previous', event);
6459
});
6560

6661
it('sends stored events', async () => {
@@ -130,16 +125,14 @@ describe('Offline', () => {
130125
});
131126

132127
describe('when connectivity is restored', () => {
133-
it('sends stored events', async done => {
128+
it('sends stored events', async () => {
134129
initIntegration();
135130
setupOnce();
136131
prepopulateEvents(1);
137132
processEvents();
138133
processEventListeners();
139134

140135
expect(await integration.offlineEventStore.length()).toEqual(0);
141-
142-
setImmediate(done);
143136
});
144137
});
145138
});
@@ -150,7 +143,7 @@ let eventProcessors: EventProcessor[];
150143
let events: Event[];
151144

152145
/** JSDoc */
153-
function addGlobalEventProcessor(callback: () => void): void {
146+
function addGlobalEventProcessor(callback: EventProcessor): void {
154147
eventProcessors.push(callback);
155148
}
156149

@@ -160,11 +153,11 @@ function getCurrentHub(): Hub {
160153
captureEvent(_event: Event): string {
161154
return 'an-event-id';
162155
},
163-
getIntegration(_integration: Integration): any {
156+
getIntegration<T extends Integration>(_integration: IntegrationClass<T>): T | null {
164157
// pretend integration is enabled
165-
return true;
158+
return {} as T;
166159
},
167-
};
160+
} as Hub;
168161
}
169162

170163
/** JSDoc */
@@ -173,14 +166,17 @@ function initIntegration(options: { maxStoredEvents?: number } = {}): void {
173166
eventProcessors = [];
174167
events = [];
175168

176-
utils.getGlobalObject = jest.fn(() => ({
177-
addEventListener: (_windowEvent, callback) => {
178-
eventListeners.push(callback);
179-
},
180-
navigator: {
181-
onLine: online,
182-
},
183-
}));
169+
jest.spyOn(utils, 'getGlobalObject').mockImplementation(
170+
() =>
171+
({
172+
addEventListener: (_windowEvent, callback) => {
173+
eventListeners.push(callback);
174+
},
175+
navigator: {
176+
onLine: online,
177+
},
178+
} as any),
179+
);
184180

185181
integration = new Offline(options);
186182
}

0 commit comments

Comments
 (0)