|
1 | 1 | import { BacktraceTestClient } from '../mocks/BacktraceTestClient.js'; |
| 2 | +import { testHttpClient } from '../mocks/testHttpClient.js'; |
2 | 3 |
|
3 | 4 | describe('Attributes tests', () => { |
| 5 | + beforeEach(() => { |
| 6 | + jest.mocked(testHttpClient.postError).mockClear(); |
| 7 | + }); |
| 8 | + |
4 | 9 | describe('Client attribute add', () => { |
5 | 10 | it('Should add an attribute to the client cache', () => { |
6 | 11 | const client = BacktraceTestClient.buildFakeClient(); |
@@ -80,4 +85,93 @@ describe('Attributes tests', () => { |
80 | 85 | expect(scopedAttributeGetFunction).toHaveBeenCalledTimes(2); |
81 | 86 | }); |
82 | 87 | }); |
| 88 | + |
| 89 | + describe('Non-serializable attributes', () => { |
| 90 | + it('Should convert Date attribute to ISO string', async () => { |
| 91 | + const client = BacktraceTestClient.buildFakeClient(); |
| 92 | + const date = new Date(); |
| 93 | + |
| 94 | + await client.send(new Error('test'), { date }); |
| 95 | + |
| 96 | + const [[, json]] = (client.requestHandler.postError as jest.Mock).mock.calls; |
| 97 | + const body = JSON.parse(json); |
| 98 | + expect(body.attributes.date).toEqual(date.toISOString()); |
| 99 | + }); |
| 100 | + |
| 101 | + it('Should convert URL attribute to string', async () => { |
| 102 | + const client = BacktraceTestClient.buildFakeClient(); |
| 103 | + const url = new URL('https://example.com/path?q=1'); |
| 104 | + |
| 105 | + await client.send(new Error('test'), { url }); |
| 106 | + |
| 107 | + const [[, json]] = (client.requestHandler.postError as jest.Mock).mock.calls; |
| 108 | + const body = JSON.parse(json); |
| 109 | + expect(body.attributes.url).toEqual(url.toString()); |
| 110 | + }); |
| 111 | + |
| 112 | + it('Should handle URL instance as annotation', async () => { |
| 113 | + const client = BacktraceTestClient.buildFakeClient(); |
| 114 | + |
| 115 | + await client.send(new Error('test'), { |
| 116 | + destroyedClassInstance: { ...new URL('https://example.com') }, |
| 117 | + }); |
| 118 | + |
| 119 | + const [[, json]] = (client.requestHandler.postError as jest.Mock).mock.calls; |
| 120 | + expect(() => JSON.parse(json)).not.toThrow(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('Should handle Object.create with URL prototype', async () => { |
| 124 | + const client = BacktraceTestClient.buildFakeClient(); |
| 125 | + |
| 126 | + await client.send(new Error('test'), { |
| 127 | + createdObjectViaPrototype: Object.create(URL.prototype), |
| 128 | + }); |
| 129 | + |
| 130 | + const [[, json]] = (client.requestHandler.postError as jest.Mock).mock.calls; |
| 131 | + expect(() => JSON.parse(json)).not.toThrow(); |
| 132 | + }); |
| 133 | + |
| 134 | + it('Should return submission error for object with broken toJSON', async () => { |
| 135 | + const client = BacktraceTestClient.buildFakeClient(); |
| 136 | + |
| 137 | + const result = await client.send(new Error('test'), { |
| 138 | + brokenToJSON: { |
| 139 | + toJSON() { |
| 140 | + throw new Error('broken toJSON'); |
| 141 | + }, |
| 142 | + }, |
| 143 | + }); |
| 144 | + |
| 145 | + expect(result.status).toEqual('Unknown'); |
| 146 | + }); |
| 147 | + |
| 148 | + it('Should handle spread class instance with private fields', async () => { |
| 149 | + class Strict { |
| 150 | + #data = 'secret'; |
| 151 | + toJSON() { |
| 152 | + return this.#data; |
| 153 | + } |
| 154 | + } |
| 155 | + const client = BacktraceTestClient.buildFakeClient(); |
| 156 | + |
| 157 | + await client.send(new Error('test'), { |
| 158 | + strict: { ...new Strict() }, |
| 159 | + }); |
| 160 | + |
| 161 | + const [[, json]] = (client.requestHandler.postError as jest.Mock).mock.calls; |
| 162 | + expect(() => JSON.parse(json)).not.toThrow(); |
| 163 | + }); |
| 164 | + |
| 165 | + it('Should handle revoked proxy nested in object', async () => { |
| 166 | + const { proxy, revoke } = Proxy.revocable({ toJSON: () => 'ok' }, {}); |
| 167 | + revoke(); |
| 168 | + const client = BacktraceTestClient.buildFakeClient(); |
| 169 | + |
| 170 | + const result = await client.send(new Error('test'), { |
| 171 | + revokedProxy: { data: proxy }, |
| 172 | + }); |
| 173 | + |
| 174 | + expect(result.status).toEqual('Unknown'); |
| 175 | + }); |
| 176 | + }); |
83 | 177 | }); |
0 commit comments