Skip to content

Commit 502ea29

Browse files
committed
feat: add more tests to improve coverage
1 parent d72af75 commit 502ea29

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

test/IMQClient.extra.spec.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*!
2+
* IMQClient Extra Unit Tests (non-RPC, using send stubs)
3+
*/
4+
import './mocks';
5+
import { logger } from './mocks';
6+
import { expect } from 'chai';
7+
import * as sinon from 'sinon';
8+
import { IMQClient, IMQDelay, IMQMetadata, remote, AFTER_HOOK_ERROR, BEFORE_HOOK_ERROR } from '..';
9+
10+
class ExtraClient extends IMQClient {
11+
@remote()
12+
public async greet(name?: string, imqMetadata?: IMQMetadata, imqDelay?: IMQDelay) {
13+
return this.remoteCall<string>(...arguments);
14+
}
15+
@remote()
16+
public async fail(imqDelay?: IMQDelay) {
17+
return this.remoteCall<any>(...arguments);
18+
}
19+
}
20+
21+
describe('IMQClient (extra branches without service)', () => {
22+
let client: ExtraClient;
23+
24+
afterEach(async () => {
25+
await client?.destroy();
26+
sinon.restore();
27+
});
28+
29+
it('should warn on BEFORE_HOOK_ERROR and continue call', async function() {
30+
this.timeout(5000);
31+
const warn = sinon.stub(logger, 'warn');
32+
client = new ExtraClient({ logger, beforeCall: async () => { throw new Error('before'); } });
33+
await client.start();
34+
const imq: any = (client as any).imq;
35+
sinon.stub(imq, 'send').callsFake(async (to: string, request: any, delay?: number) => {
36+
const id = 'ID1';
37+
setImmediate(() => imq.emit('message', { to: id, request, data: 'ok' }));
38+
return id;
39+
});
40+
const res = await client.greet('imq');
41+
expect(res).to.equal('ok');
42+
expect(warn.called).to.equal(true);
43+
expect(String(warn.firstCall.args[0])).to.contain(BEFORE_HOOK_ERROR);
44+
});
45+
46+
it('should warn on AFTER_HOOK_ERROR for resolve and reject paths', async () => {
47+
const warn = sinon.stub(logger, 'warn');
48+
client = new ExtraClient({ logger, afterCall: async () => { throw new Error('after'); } });
49+
await client.start();
50+
const imq: any = (client as any).imq;
51+
const send = sinon.stub(imq, 'send');
52+
// success path
53+
send.onFirstCall().callsFake(async (to: string, request: any) => {
54+
const id = 'ID2';
55+
setImmediate(() => imq.emit('message', { to: id, request, data: 'success' }));
56+
return id;
57+
});
58+
// reject path
59+
send.onSecondCall().callsFake(async (to: string, request: any) => {
60+
const id = 'ID3';
61+
setImmediate(() => imq.emit('message', { to: id, request, error: new Error('boom') }));
62+
return id;
63+
});
64+
const ok = await client.greet('ok');
65+
expect(ok).to.equal('success');
66+
try { await client.fail(); } catch (e) { /* expected */ }
67+
// both paths should warn due to afterCall throwing
68+
expect(warn.callCount).to.be.greaterThan(0);
69+
expect(warn.getCalls().map(c => String(c.args[0])).join(' ')).to.contain(AFTER_HOOK_ERROR);
70+
});
71+
72+
it('should emit event when resolver is missing', async () => {
73+
client = new ExtraClient({ logger });
74+
await client.start();
75+
const evt = sinon.spy();
76+
client.on('greet', evt);
77+
(client as any).imq.emit('message', {
78+
to: 'unknown-id',
79+
request: { method: 'greet' },
80+
data: { foo: 'bar' },
81+
});
82+
expect(evt.calledOnce).to.equal(true);
83+
});
84+
85+
it('should sanitize invalid IMQDelay and pass IMQMetadata through request', async () => {
86+
client = new ExtraClient({ logger });
87+
await client.start();
88+
const imq: any = (client as any).imq;
89+
const sendStub = sinon.stub(imq, 'send').callsFake(async (to: string, request: any, delay?: number) => {
90+
expect(delay).to.equal(0);
91+
expect(request.metadata).to.be.instanceOf(IMQMetadata);
92+
const id = 'ID4';
93+
setImmediate(() => imq.emit('message', { to: id, request, data: 'x' }));
94+
return id;
95+
});
96+
const meta = new IMQMetadata({ a: 1 } as any);
97+
const res = await client.greet('z', meta as any, new IMQDelay(-100) as any);
98+
expect(res).to.equal('x');
99+
expect(sendStub.calledOnce).to.equal(true);
100+
});
101+
});

0 commit comments

Comments
 (0)