-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.spec.js
94 lines (70 loc) · 2.68 KB
/
index.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const sinon = require('sinon');
const assert = require('assert');
const { Client } = require('hato');
const { Encoding, Retry, RPC } = require('hato/plugins');
const Sentry = require('@sentry/node');
const plugin = require('.');
const createTrigger = (times = 1) => {
let c = 0, inc, completed = new Promise(
(resolve) => inc = () => ++c >= times && resolve());
return { inc, completed };
};
describe('Sentry plugin', function() {
let client = null;
const sandbox = sinon.createSandbox();
afterEach(() => {
sandbox.restore();
return client && client.close();
});
it('should reject if Sentry access point is not provided', () => {
assert.throws(
() => new Client('amqp://guest:guest@127.0.0.1:5672', {
plugins: [new plugin({ Sentry: null })]
}),
assert.AssertionError
);
});
it('should report any error to Sentry', async function() {
client = new Client('amqp://guest:guest@127.0.0.1:5672', {
plugins: [
new Encoding('json'),
new plugin({ Sentry })
]
});
await client.start();
const spyScope = sandbox.spy(Sentry, 'withScope');
const spyReport = sandbox.spy(Sentry, 'captureException');
const { inc, completed } = createTrigger();
await client.subscribe('it.always.fails', () => {
inc();
throw new Error('My route somehow have uncaught exception :(');
});
await client.publish('it.always.fails', { hello: 'world' });
await completed;
assert(spyScope.calledOnce);
assert(spyReport.calledOnce);
});
it('should report any error to Sentry (retry)', async function() {
const retries = 3;
client = new Client('amqp://guest:guest@127.0.0.1:5672', {
plugins: [
new Retry({ min: 5, retries, strategy: 'constant' }),
new RPC(),
new Encoding('json'),
new plugin({ Sentry })
]
});
await client.start();
const spyScope = sandbox.spy(Sentry, 'withScope');
const spyReport = sandbox.spy(Sentry, 'captureException');
const { inc, completed } = createTrigger(retries + 1);
await client.subscribe('it.always.fails', () => {
inc();
throw new Error('My route somehow have uncaught exception :(');
});
await client.rpc('it.always.fails', { hello: 'world' }).catch(() => {});
await completed;
assert.strictEqual(spyScope.callCount, retries + 1);
assert.strictEqual(spyReport.callCount, retries + 1);
});
});