Skip to content

Commit d6a8c55

Browse files
committed
add tests
1 parent a7652f8 commit d6a8c55

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

packages/browser/test/unit/eventbuilder.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Client } from '@sentry/types';
22

33
import { defaultStackParser } from '../../src';
4-
import { eventFromPlainObject } from '../../src/eventbuilder';
4+
import { eventFromPlainObject, exceptionFromError } from '../../src/eventbuilder';
55

66
jest.mock('@sentry/core', () => {
77
const original = jest.requireActual('@sentry/core');
@@ -62,3 +62,25 @@ describe('eventFromPlainObject', () => {
6262
});
6363
});
6464
});
65+
66+
describe('exceptionFromError ', () => {
67+
it('correctly reads error type and value from built-in `Error` subclass', () => {
68+
const exceptionJSON = exceptionFromError(() => [], new TypeError("Expected type 'ChewToy', got type 'Shoe'"));
69+
70+
expect(exceptionJSON).toEqual({
71+
type: 'TypeError',
72+
value: "Expected type 'ChewToy', got type 'Shoe'",
73+
});
74+
});
75+
76+
it('correctly reads error type and value from user-defined `Error` subclass', () => {
77+
class DidNotFetch extends Error {}
78+
79+
const exceptionJSON = exceptionFromError(() => [], new DidNotFetch("Failed to fetch requested object: 'ball'"));
80+
81+
expect(exceptionJSON).toEqual({
82+
type: 'DidNotFetch',
83+
value: "Failed to fetch requested object: 'ball'",
84+
});
85+
});
86+
});

packages/node/test/eventbuilders.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Client } from '@sentry/types';
22

33
import { defaultStackParser, Scope } from '../src';
4-
import { eventFromUnknownInput } from '../src/eventbuilder';
4+
import { eventFromUnknownInput, exceptionFromError } from '../src/eventbuilder';
55

66
const testScope = new Scope();
77

@@ -74,3 +74,25 @@ describe('eventFromUnknownInput', () => {
7474
});
7575
});
7676
});
77+
78+
describe('exceptionFromError ', () => {
79+
it('correctly reads error type and value from built-in `Error` subclass', () => {
80+
const exceptionJSON = exceptionFromError(() => [], new TypeError("Expected type 'ChewToy', got type 'Shoe'"));
81+
82+
expect(exceptionJSON).toEqual({
83+
type: 'TypeError',
84+
value: "Expected type 'ChewToy', got type 'Shoe'",
85+
});
86+
});
87+
88+
it('correctly reads error type and value from user-defined `Error` subclass', () => {
89+
class DidNotFetch extends Error {}
90+
91+
const exceptionJSON = exceptionFromError(() => [], new DidNotFetch("Failed to fetch requested object: 'ball'"));
92+
93+
expect(exceptionJSON).toEqual({
94+
type: 'DidNotFetch',
95+
value: "Failed to fetch requested object: 'ball'",
96+
});
97+
});
98+
});

0 commit comments

Comments
 (0)