|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { extractErrorContext } from '../../../src/runtime/utils'; |
| 3 | + |
| 4 | +describe('extractErrorContext', () => { |
| 5 | + it('returns empty object for undefined or empty context', () => { |
| 6 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 7 | + // @ts-ignore |
| 8 | + expect(extractErrorContext(undefined)).toEqual({}); |
| 9 | + expect(extractErrorContext({})).toEqual({}); |
| 10 | + }); |
| 11 | + |
| 12 | + it('extracts properties from errorContext and drops them if missing', () => { |
| 13 | + const context = { |
| 14 | + event: { |
| 15 | + _method: 'GET', |
| 16 | + _path: '/test', |
| 17 | + }, |
| 18 | + tags: ['tag1', 'tag2'], |
| 19 | + }; |
| 20 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 21 | + // @ts-ignore |
| 22 | + expect(extractErrorContext(context)).toEqual({ |
| 23 | + method: 'GET', |
| 24 | + path: '/test', |
| 25 | + tags: ['tag1', 'tag2'], |
| 26 | + }); |
| 27 | + |
| 28 | + const partialContext = { |
| 29 | + event: { |
| 30 | + _path: '/test', |
| 31 | + }, |
| 32 | + }; |
| 33 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 34 | + // @ts-ignore |
| 35 | + expect(extractErrorContext(partialContext)).toEqual({ path: '/test' }); |
| 36 | + }); |
| 37 | + |
| 38 | + it('handles errorContext.tags correctly, including when absent or of unexpected type', () => { |
| 39 | + const contextWithTags = { |
| 40 | + tags: ['tag1', 'tag2'], |
| 41 | + }; |
| 42 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 43 | + // @ts-ignore |
| 44 | + expect(extractErrorContext(contextWithTags)).toEqual({ |
| 45 | + tags: ['tag1', 'tag2'], |
| 46 | + }); |
| 47 | + |
| 48 | + const contextWithoutTags = { |
| 49 | + event: {}, |
| 50 | + }; |
| 51 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 52 | + // @ts-ignore |
| 53 | + expect(extractErrorContext(contextWithoutTags)).toEqual({}); |
| 54 | + |
| 55 | + const contextWithInvalidTags = { |
| 56 | + event: {}, |
| 57 | + tags: 'not-an-array', |
| 58 | + }; |
| 59 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 60 | + // @ts-ignore |
| 61 | + expect(extractErrorContext(contextWithInvalidTags)).toEqual({}); |
| 62 | + }); |
| 63 | + |
| 64 | + it('gracefully handles unexpected context structure without throwing errors', () => { |
| 65 | + const weirdContext1 = { |
| 66 | + unexpected: 'value', |
| 67 | + }; |
| 68 | + const weirdContext2 = ['value']; |
| 69 | + const weirdContext3 = 123; |
| 70 | + |
| 71 | + expect(() => extractErrorContext(weirdContext1)).not.toThrow(); |
| 72 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 73 | + // @ts-ignore |
| 74 | + expect(() => extractErrorContext(weirdContext2)).not.toThrow(); |
| 75 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 76 | + // @ts-ignore |
| 77 | + expect(() => extractErrorContext(weirdContext3)).not.toThrow(); |
| 78 | + }); |
| 79 | +}); |
0 commit comments