|
1 | | -import type { Event } from '@sentry/types'; |
| 1 | +import type { Event, Session } from '@sentry/types'; |
2 | 2 | import { defineIntegration } from '@sentry/core'; |
3 | 3 | import { Toucan } from 'toucan-js'; |
4 | 4 | import { |
@@ -1106,6 +1106,103 @@ describe('Toucan', () => { |
1106 | 1106 | }); |
1107 | 1107 | }); |
1108 | 1108 |
|
| 1109 | + describe('cloning', () => { |
| 1110 | + test('clone', async () => { |
| 1111 | + const toucan = new Toucan({ dsn: VALID_DSN, context }); |
| 1112 | + |
| 1113 | + toucan.addBreadcrumb({ message: 'test' }); |
| 1114 | + toucan.setTag('foo', 'bar'); |
| 1115 | + toucan.setExtra('foo', 'bar'); |
| 1116 | + toucan.setContext('foo', { foo: 'bar' }); |
| 1117 | + toucan.setUser({ email: '[email protected]' }); |
| 1118 | + toucan.setLevel('debug'); |
| 1119 | + toucan.setSession({ ipAddress: '1.1.1.1' } as Session); |
| 1120 | + toucan.setTransactionName('foo'); |
| 1121 | + toucan.setFingerprint(['foo']); |
| 1122 | + toucan.addEventProcessor((event) => { |
| 1123 | + // Verifying 'extra' added by event processor allows us to verify that event processors get cloned as well |
| 1124 | + if (event.extra) event.extra['bar'] = 'baz'; |
| 1125 | + return event; |
| 1126 | + }); |
| 1127 | + toucan.setRequestSession({ status: 'ok' }); |
| 1128 | + toucan.setSDKProcessingMetadata({ foo: 'bar' }); |
| 1129 | + toucan.setPropagationContext({ spanId: 'foo', traceId: 'bar' }); |
| 1130 | + toucan.setLastEventId('foo'); |
| 1131 | + |
| 1132 | + const toucanClone1 = toucan.clone(); |
| 1133 | + const toucanClone2 = toucan.clone(); |
| 1134 | + const toucanClone3 = toucanClone2.clone(); |
| 1135 | + |
| 1136 | + // Verify we always create new client instance during Toucan.clone() |
| 1137 | + expect(toucan.getClient()).not.toBe(toucanClone1.getClient()); |
| 1138 | + expect(toucan.getClient()).not.toBe(toucanClone2.getClient()); |
| 1139 | + expect(toucan.getClient()).not.toBe(toucanClone3.getClient()); |
| 1140 | + expect(toucanClone1.getClient()).not.toBe(toucanClone2.getClient()); |
| 1141 | + expect(toucanClone1.getClient()).not.toBe(toucanClone3.getClient()); |
| 1142 | + expect(toucanClone2.getClient()).not.toBe(toucanClone3.getClient()); |
| 1143 | + |
| 1144 | + // Capture an exception on original and cloned instances and verify the metadata in payloads are the same |
| 1145 | + toucan.captureException(new Error('error!')); |
| 1146 | + toucanClone1.captureException(new Error('error!')); |
| 1147 | + toucanClone2.captureException(new Error('error!')); |
| 1148 | + toucanClone3.captureException(new Error('error!')); |
| 1149 | + |
| 1150 | + const waitUntilResults = await getMiniflareWaitUntil(context); |
| 1151 | + |
| 1152 | + expect(waitUntilResults.length).toBe(4); |
| 1153 | + expect(requests.length).toBe(4); |
| 1154 | + |
| 1155 | + const assertEnvelopePayloadsEqual = ( |
| 1156 | + p1: Record<string, any>, |
| 1157 | + p2: Record<string, any>, |
| 1158 | + ) => { |
| 1159 | + const assertPropertyExistsAndEqual = (propertyName: string) => { |
| 1160 | + expect(propertyName in p1).toBe(true); |
| 1161 | + expect(p1[propertyName]).toBeTruthy(); |
| 1162 | + |
| 1163 | + expect(propertyName in p2).toBe(true); |
| 1164 | + expect(p2[propertyName]).toBeTruthy(); |
| 1165 | + |
| 1166 | + expect(p1[propertyName]).toStrictEqual(p2[propertyName]); |
| 1167 | + }; |
| 1168 | + |
| 1169 | + assertPropertyExistsAndEqual('breadcrumbs'); |
| 1170 | + assertPropertyExistsAndEqual('tags'); |
| 1171 | + assertPropertyExistsAndEqual('extra'); |
| 1172 | + assertPropertyExistsAndEqual('contexts'); |
| 1173 | + assertPropertyExistsAndEqual('user'); |
| 1174 | + assertPropertyExistsAndEqual('level'); |
| 1175 | + assertPropertyExistsAndEqual('transaction'); |
| 1176 | + assertPropertyExistsAndEqual('fingerprint'); |
| 1177 | + }; |
| 1178 | + |
| 1179 | + assertEnvelopePayloadsEqual( |
| 1180 | + await requests[0].envelopePayload(), |
| 1181 | + await requests[1].envelopePayload(), |
| 1182 | + ); |
| 1183 | + assertEnvelopePayloadsEqual( |
| 1184 | + await requests[0].envelopePayload(), |
| 1185 | + await requests[2].envelopePayload(), |
| 1186 | + ); |
| 1187 | + assertEnvelopePayloadsEqual( |
| 1188 | + await requests[0].envelopePayload(), |
| 1189 | + await requests[3].envelopePayload(), |
| 1190 | + ); |
| 1191 | + assertEnvelopePayloadsEqual( |
| 1192 | + await requests[1].envelopePayload(), |
| 1193 | + await requests[2].envelopePayload(), |
| 1194 | + ); |
| 1195 | + assertEnvelopePayloadsEqual( |
| 1196 | + await requests[1].envelopePayload(), |
| 1197 | + await requests[3].envelopePayload(), |
| 1198 | + ); |
| 1199 | + assertEnvelopePayloadsEqual( |
| 1200 | + await requests[2].envelopePayload(), |
| 1201 | + await requests[3].envelopePayload(), |
| 1202 | + ); |
| 1203 | + }); |
| 1204 | + }); |
| 1205 | + |
1109 | 1206 | describe('scope', () => { |
1110 | 1207 | test('withScope', async () => { |
1111 | 1208 | const toucan = new Toucan({ |
|
0 commit comments