-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathonunhandledrejection.test.ts
More file actions
54 lines (43 loc) · 1.92 KB
/
onunhandledrejection.test.ts
File metadata and controls
54 lines (43 loc) · 1.92 KB
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
import * as SentryCore from '@sentry/core';
import type { Client } from '@sentry/core';
import { afterEach, describe, expect, it, vi } from 'vitest';
import {
makeUnhandledPromiseHandler,
onUnhandledRejectionIntegration,
} from '../../src/integrations/onunhandledrejection';
// don't log the test errors we're going to throw, so at a quick glance it doesn't look like the test itself has failed
global.console.warn = () => null;
global.console.error = () => null;
describe('unhandled promises', () => {
afterEach(() => {
vi.restoreAllMocks();
});
it('installs a global listener', () => {
const client = { getOptions: () => ({}) } as unknown as Client;
SentryCore.setCurrentClient(client);
const beforeListeners = process.listeners('unhandledRejection').length;
const integration = onUnhandledRejectionIntegration();
integration.setup!(client);
expect(process.listeners('unhandledRejection').length).toBe(beforeListeners + 1);
});
it('passes the rejection reason (not the promise) as originalException', () => {
const client = { getOptions: () => ({}) } as unknown as Client;
SentryCore.setCurrentClient(client);
const reason = new Error('boom');
const promise = Promise.reject(reason);
// swallow the rejection so it does not leak into the test runner
promise.catch(() => {});
const captureException = vi.spyOn(SentryCore, 'captureException').mockImplementation(() => 'test');
const handler = makeUnhandledPromiseHandler(client, { mode: 'warn', ignore: [] });
handler(reason, promise);
expect(captureException).toHaveBeenCalledTimes(1);
const [capturedReason, hint] = captureException.mock.calls[0]!;
expect(capturedReason).toBe(reason);
expect(hint?.originalException).toBe(reason);
expect(hint?.originalException).not.toBe(promise);
expect(hint?.mechanism).toEqual({
handled: false,
type: 'auto.node.onunhandledrejection',
});
});
});