File size: 3,565 Bytes
1e92f2d |
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
import { UNDEFINED_MARKER } from '../../shared/forward-logs-shared'
import {
preLogSerializationClone,
PROMISE_MARKER,
UNAVAILABLE_MARKER,
logStringify,
} from './forward-logs'
const safeStringify = (data: unknown) =>
logStringify(preLogSerializationClone(data))
describe('forward-logs serialization', () => {
describe('safeClone', () => {
it('should handle primitive values and null', () => {
expect(preLogSerializationClone(42)).toBe(42)
expect(preLogSerializationClone('hello')).toBe('hello')
expect(preLogSerializationClone(true)).toBe(true)
expect(preLogSerializationClone(null)).toBe(null)
expect(preLogSerializationClone(undefined)).toBe(UNDEFINED_MARKER)
})
it('should handle circular references', () => {
const obj: any = { a: 1 }
obj.self = obj
const cloned = preLogSerializationClone(obj)
expect(cloned.a).toBe(1)
expect(cloned.self).toBe(cloned)
})
it('should handle promises', () => {
const promise = Promise.resolve(42)
expect(preLogSerializationClone(promise)).toBe(PROMISE_MARKER)
})
it('should handle arrays', () => {
const arr = [1, 'test', undefined, null]
const cloned = preLogSerializationClone(arr)
expect(cloned).toEqual([1, 'test', UNDEFINED_MARKER, null])
})
it('should handle plain objects', () => {
const obj = { a: 1, b: undefined, c: 'test' }
const cloned = preLogSerializationClone(obj)
expect(cloned).toEqual({ a: 1, b: UNDEFINED_MARKER, c: 'test' })
})
it('should handle objects with getters that throw', () => {
const obj = {
normalProp: 'works',
get throwingGetter() {
throw new Error('Getter throws')
},
}
const cloned = preLogSerializationClone(obj)
expect(cloned.normalProp).toBe('works')
expect(cloned.throwingGetter).toBe(UNAVAILABLE_MARKER)
})
it('should handle non-plain objects as toString', () => {
const date = new Date('2023-01-01')
const regex = /test/gi
const error = new Error('Test error')
expect(preLogSerializationClone(date)).toBe('[object Date]')
expect(preLogSerializationClone(regex)).toBe('[object RegExp]')
expect(preLogSerializationClone(error)).toBe('[object Error]')
})
it('should handle array items that throw', () => {
const throwingProxy = new Proxy(
{},
{
get() {
throw new Error('Proxy throws')
},
}
)
const arr = [1, throwingProxy, 'normal']
const cloned = preLogSerializationClone(arr)
expect(cloned).toEqual([1, UNAVAILABLE_MARKER, 'normal'])
})
})
describe('logStringify', () => {
it('should stringify safe cloned data', () => {
expect(safeStringify(42)).toBe('42')
expect(safeStringify('hello')).toBe('"hello"')
expect(safeStringify(null)).toBe('null')
expect(safeStringify(undefined)).toBe(`"${UNDEFINED_MARKER}"`)
})
it('should handle objects with circular references', () => {
const obj: any = { a: 1 }
obj.self = obj
const result = safeStringify(obj)
expect(typeof result).toBe('string')
expect(result).toContain('"a":1')
})
it('should return UNAVAILABLE_MARKER on stringify failure', () => {
const problematicData = {
toJSON() {
throw new Error('toJSON throws')
},
}
const result = safeStringify(problematicData)
expect(result).toBe(`"${UNAVAILABLE_MARKER}"`)
})
})
})
|