Skip to content

Commit 48654b2

Browse files
authored
Merge pull request #1 from asabhaney/custom-serialize-deserialize
added tests related to custom serialization and deserialization errors
2 parents f2f99ed + 1c71cbd commit 48654b2

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/test/tests.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,24 @@ describe('RedisPubSub', () => {
308308
}
309309
});
310310

311+
it('custom serializer can throw an error', done => {
312+
const serializer = stub();
313+
serializer.throwWith(new Error('Custom serialization error'));
314+
315+
const pubSub = new RedisPubSub({...mockOptions, serializer });
316+
317+
try {
318+
pubSub.publish('TOPIC', {hello: 'world'}).then(() => {
319+
done(new Error('Expected error to be thrown upon publish'));
320+
}, err => {
321+
expect(err.message).to.eql('Custom serialization error');
322+
done();
323+
})
324+
} catch (e) {
325+
done(e);
326+
}
327+
});
328+
311329
it('allows to use a custom deserializer', done => {
312330
const deserializer = stub();
313331
const deserializedPayload = { hello: 'custom' };
@@ -331,6 +349,29 @@ describe('RedisPubSub', () => {
331349
}
332350
});
333351

352+
it('unparsed payload is returned if custom deserializer throws an error', done => {
353+
const deserializer = stub();
354+
deserializer.throwWith(new Error('Custom deserialization error'));
355+
356+
const pubSub = new RedisPubSub({...mockOptions, deserializer });
357+
358+
try {
359+
pubSub.subscribe('TOPIC', message => {
360+
try {
361+
expect(message).to.be.a('string');
362+
expect(message).to.eql('{"hello":"world"}');
363+
done();
364+
} catch (e) {
365+
done(e);
366+
}
367+
}).then(() => {
368+
pubSub.publish('TOPIC', {hello: 'world'});
369+
});
370+
} catch (e) {
371+
done(e);
372+
}
373+
});
374+
334375
it('throws if you try to unsubscribe with an unknown id', () => {
335376
const pubSub = new RedisPubSub(mockOptions);
336377
return expect(() => pubSub.unsubscribe(123))

0 commit comments

Comments
 (0)