Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,15 +653,22 @@ test('Should publish error when initial init fails', (done) => {
storageProvider,
};
const client = new UnleashClient(config);
client.start();

let errorReceived = false;
client.on(EVENTS.ERROR, (e: any) => {
expect(e).toBe(givenError);
done();
if (!errorReceived) {
errorReceived = true;
expect(e).toBe(givenError);
done();
}
});
client.start();
});

test('Should publish error when fetch fails', (done) => {
const givenError = new Error('Error');
const givenError = new Error(
JSON.stringify({ error: 'Error', type: 'fetch-toggles' })
);

jest.spyOn(global.console, 'error').mockImplementation(() => jest.fn());
fetchMock.mockReject(givenError);
Expand All @@ -674,7 +681,10 @@ test('Should publish error when fetch fails', (done) => {
const client = new UnleashClient(config);
client.start();
client.on(EVENTS.ERROR, (e: any) => {
expect(e).toBe(givenError);
expect(e).toStrictEqual({
type: 'fetch-toggles',
error: givenError,
});
done();
});
});
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ export class UnleashClient extends TinyEmitter {
this.connectionId = uuidv4();

this.metrics = new Metrics({
onError: this.emit.bind(this, EVENTS.ERROR),
onError: (err) =>
this.emit(EVENTS.ERROR, { type: 'metrics', error: err }),
onSent: this.emit.bind(this, EVENTS.SENT),
appName,
metricsInterval,
Expand Down Expand Up @@ -629,7 +630,10 @@ export class UnleashClient extends TinyEmitter {
e
);
this.sdkState = 'error';
this.emit(EVENTS.ERROR, e);
this.emit(EVENTS.ERROR, {
type: 'fetch-toggles',
error: e,
});
this.lastError = e;
}
} finally {
Expand Down