1 import { getSafeArray, getSafeErrorObject, getSafeObject, getSafeValue, isError } from './getSafeErrorObject';
3 describe('getSafeValue', () => {
4 const error = new Error('blah');
7 { name: 'number', value: 3, expected: 3 },
8 { name: 'string', value: 'abc', expected: 'abc' },
9 { name: 'boolean', value: true, expected: true },
10 { name: 'null', value: null, expected: null },
11 { name: 'undefined', value: undefined, expected: undefined },
12 { name: 'array', value: [1, 2, 3], expected: [1, 2, 3] },
13 { name: 'array (deep)', value: [1, 2, 3, [4, 5]], expected: [1, 2, 3, [4, 5]] },
14 { name: 'object', value: { key: 'value' }, expected: { key: 'value' } },
16 name: 'object (deep)',
17 value: { key: 'value', other: { stuff: [1, 2, 3] } },
18 expected: { key: 'value', other: { stuff: [1, 2, 3] } },
20 { name: 'error', value: error, expected: getSafeErrorObject(error) },
21 { name: 'unsupported type', value: Symbol('oh no'), expected: undefined },
24 testCases.forEach((testCase) => {
25 it(`should accept '${testCase.name}'`, () => {
26 expect(getSafeValue(testCase.value)).toStrictEqual(testCase.expected);
31 describe('getSafeErrorObject', () => {
32 it(`should accept errors`, () => {
33 const value = new Error('blah');
34 const expected = { name: 'Error', message: 'blah', context: undefined };
36 const result = getSafeValue(value);
38 expect(result).toHaveProperty('stack');
39 expect(result).toMatchObject(expected);
42 it(`should accept errors with Drive context`, () => {
43 const value = new Error('blah');
44 (value as any).context = { something: true };
45 (value as any).isEnrichedError = true;
46 (value as any).sentryMessage = 'oh no';
52 context: { something: true },
53 isEnrichedError: true,
54 sentryMessage: 'oh no',
57 const result = getSafeValue(value);
59 expect(result).toHaveProperty('stack');
60 expect(result).toMatchObject(expected);
63 it(`should accept errors with nested errors in Drive context`, () => {
64 const value = new Error('blah');
65 (value as any).context = { e: new Error('oh no') };
69 context: { e: { name: 'Error', message: 'oh no' } },
72 const result = getSafeValue(value);
74 expect(result).toHaveProperty('stack');
75 expect(result).toHaveProperty('context.e.stack');
76 expect(result).toMatchObject(expected);
80 describe('getSafeObject', () => {
82 { name: 'object', value: { key: 'value' }, expected: { key: 'value' } },
84 name: 'object (deep)',
85 value: { key: 'value', other: { stuff: [1, 2, 3] } },
86 expected: { key: 'value', other: { stuff: [1, 2, 3] } },
90 testCases.forEach((testCase) => {
91 it(`should accept '${testCase.name}'`, () => {
92 expect(getSafeObject(testCase.value)).toStrictEqual(testCase.expected);
97 describe('getSafeArray', () => {
99 { name: 'array', value: [1, 2, 3], expected: [1, 2, 3] },
100 { name: 'array (deep)', value: [1, 2, 3, [4, 5]], expected: [1, 2, 3, [4, 5]] },
101 { name: 'array (objects)', value: [1, 2, 3, [{ blah: true }]], expected: [1, 2, 3, [{ blah: true }]] },
104 testCases.forEach((testCase) => {
105 it(`should accept '${testCase.name}'`, () => {
106 expect(getSafeArray(testCase.value)).toStrictEqual(testCase.expected);
111 describe('isError', () => {
112 it('should return true for Error', () => {
113 expect(isError(new Error('oh no'))).toBe(true);
115 it('should return true for Error-like', () => {
116 expect(isError({ name: 'Error', message: 'oh no' })).toBe(true);