|
1 | 1 | import type { Client, Envelope, Event, Span, Transaction } from '@sentry/types';
|
2 | 2 | import { SentryError, SyncPromise, dsnToString, logger } from '@sentry/utils';
|
3 | 3 |
|
4 |
| -import { Hub, Scope, makeSession, setGlobalScope } from '../../src'; |
| 4 | +import { |
| 5 | + Scope, |
| 6 | + addBreadcrumb, |
| 7 | + getCurrentScope, |
| 8 | + getIsolationScope, |
| 9 | + makeSession, |
| 10 | + setCurrentClient, |
| 11 | + setGlobalScope, |
| 12 | +} from '../../src'; |
5 | 13 | import * as integrationModule from '../../src/integration';
|
6 | 14 | import { TestClient, getDefaultTestClientOptions } from '../mocks/client';
|
7 | 15 | import { AdHocIntegration, TestIntegration } from '../mocks/integration';
|
@@ -55,6 +63,8 @@ describe('BaseClient', () => {
|
55 | 63 | TestClient.sendEventCalled = undefined;
|
56 | 64 | TestClient.instance = undefined;
|
57 | 65 | setGlobalScope(undefined);
|
| 66 | + getCurrentScope().clear(); |
| 67 | + getIsolationScope().clear(); |
58 | 68 | });
|
59 | 69 |
|
60 | 70 | afterEach(() => {
|
@@ -114,133 +124,108 @@ describe('BaseClient', () => {
|
114 | 124 |
|
115 | 125 | describe('getBreadcrumbs() / addBreadcrumb()', () => {
|
116 | 126 | test('adds a breadcrumb', () => {
|
117 |
| - expect.assertions(1); |
118 |
| - |
119 | 127 | const options = getDefaultTestClientOptions({});
|
120 | 128 | const client = new TestClient(options);
|
| 129 | + setCurrentClient(client); |
| 130 | + client.init(); |
| 131 | + |
121 | 132 | const scope = new Scope();
|
122 |
| - // eslint-disable-next-line deprecation/deprecation |
123 |
| - const hub = new Hub(client, scope); |
124 | 133 |
|
125 | 134 | scope.addBreadcrumb({ message: 'hello' }, 100);
|
126 |
| - // eslint-disable-next-line deprecation/deprecation |
127 |
| - hub.addBreadcrumb({ message: 'world' }); |
| 135 | + addBreadcrumb({ message: 'world' }); |
128 | 136 |
|
129 |
| - expect((scope as any)._breadcrumbs[1].message).toEqual('world'); |
130 |
| - }); |
| 137 | + const breadcrumbs = scope.getScopeData().breadcrumbs; |
| 138 | + const isolationScopeBreadcrumbs = getIsolationScope().getScopeData().breadcrumbs; |
131 | 139 |
|
132 |
| - test('adds a timestamp to new breadcrumbs', () => { |
133 |
| - expect.assertions(1); |
| 140 | + expect(breadcrumbs).toEqual([{ message: 'hello', timestamp: expect.any(Number) }]); |
| 141 | + expect(isolationScopeBreadcrumbs).toEqual([{ message: 'world', timestamp: expect.any(Number) }]); |
| 142 | + }); |
134 | 143 |
|
| 144 | + test('accepts a timestamp for new breadcrumbs', () => { |
135 | 145 | const options = getDefaultTestClientOptions({});
|
136 | 146 | const client = new TestClient(options);
|
| 147 | + setCurrentClient(client); |
| 148 | + client.init(); |
| 149 | + |
137 | 150 | const scope = new Scope();
|
138 |
| - // eslint-disable-next-line deprecation/deprecation |
139 |
| - const hub = new Hub(client, scope); |
140 | 151 |
|
141 |
| - scope.addBreadcrumb({ message: 'hello' }, 100); |
142 |
| - // eslint-disable-next-line deprecation/deprecation |
143 |
| - hub.addBreadcrumb({ message: 'world' }); |
| 152 | + scope.addBreadcrumb({ message: 'hello', timestamp: 1234 }, 100); |
| 153 | + addBreadcrumb({ message: 'world', timestamp: 12345 }); |
144 | 154 |
|
145 |
| - expect((scope as any)._breadcrumbs[1].timestamp).toBeGreaterThan(1); |
| 155 | + const breadcrumbs = scope.getScopeData().breadcrumbs; |
| 156 | + const isolationScopeBreadcrumbs = getIsolationScope().getScopeData().breadcrumbs; |
| 157 | + |
| 158 | + expect(breadcrumbs).toEqual([{ message: 'hello', timestamp: 1234 }]); |
| 159 | + expect(isolationScopeBreadcrumbs).toEqual([{ message: 'world', timestamp: 12345 }]); |
146 | 160 | });
|
147 | 161 |
|
148 | 162 | test('discards breadcrumbs beyond `maxBreadcrumbs`', () => {
|
149 |
| - expect.assertions(2); |
150 |
| - |
151 | 163 | const options = getDefaultTestClientOptions({ maxBreadcrumbs: 1 });
|
152 | 164 | const client = new TestClient(options);
|
153 |
| - const scope = new Scope(); |
154 |
| - // eslint-disable-next-line deprecation/deprecation |
155 |
| - const hub = new Hub(client, scope); |
156 |
| - |
157 |
| - scope.addBreadcrumb({ message: 'hello' }, 100); |
158 |
| - // eslint-disable-next-line deprecation/deprecation |
159 |
| - hub.addBreadcrumb({ message: 'world' }); |
160 |
| - |
161 |
| - expect((scope as any)._breadcrumbs.length).toEqual(1); |
162 |
| - expect((scope as any)._breadcrumbs[0].message).toEqual('world'); |
163 |
| - }); |
164 |
| - |
165 |
| - test('allows concurrent updates', () => { |
166 |
| - expect.assertions(1); |
| 165 | + setCurrentClient(client); |
| 166 | + client.init(); |
167 | 167 |
|
168 |
| - const options = getDefaultTestClientOptions({}); |
169 |
| - const client = new TestClient(options); |
170 |
| - const scope = new Scope(); |
171 |
| - // eslint-disable-next-line deprecation/deprecation |
172 |
| - const hub = new Hub(client, scope); |
| 168 | + addBreadcrumb({ message: 'hello1' }); |
| 169 | + addBreadcrumb({ message: 'hello2' }); |
| 170 | + addBreadcrumb({ message: 'hello3' }); |
173 | 171 |
|
174 |
| - scope.addBreadcrumb({ message: 'hello' }); |
175 |
| - // eslint-disable-next-line deprecation/deprecation |
176 |
| - hub.addBreadcrumb({ message: 'world' }); |
| 172 | + const isolationScopeBreadcrumbs = getIsolationScope().getScopeData().breadcrumbs; |
177 | 173 |
|
178 |
| - expect((scope as any)._breadcrumbs).toHaveLength(2); |
| 174 | + expect(isolationScopeBreadcrumbs).toEqual([{ message: 'hello3', timestamp: expect.any(Number) }]); |
179 | 175 | });
|
180 | 176 |
|
181 | 177 | test('calls `beforeBreadcrumb` and adds the breadcrumb without any changes', () => {
|
182 |
| - expect.assertions(1); |
183 |
| - |
184 | 178 | const beforeBreadcrumb = jest.fn(breadcrumb => breadcrumb);
|
185 | 179 | const options = getDefaultTestClientOptions({ beforeBreadcrumb });
|
186 | 180 | const client = new TestClient(options);
|
187 |
| - const scope = new Scope(); |
188 |
| - // eslint-disable-next-line deprecation/deprecation |
189 |
| - const hub = new Hub(client, scope); |
| 181 | + setCurrentClient(client); |
| 182 | + client.init(); |
190 | 183 |
|
191 |
| - // eslint-disable-next-line deprecation/deprecation |
192 |
| - hub.addBreadcrumb({ message: 'hello' }); |
| 184 | + addBreadcrumb({ message: 'hello' }); |
193 | 185 |
|
194 |
| - expect((scope as any)._breadcrumbs[0].message).toEqual('hello'); |
| 186 | + const isolationScopeBreadcrumbs = getIsolationScope().getScopeData().breadcrumbs; |
| 187 | + expect(isolationScopeBreadcrumbs).toEqual([{ message: 'hello', timestamp: expect.any(Number) }]); |
195 | 188 | });
|
196 | 189 |
|
197 | 190 | test('calls `beforeBreadcrumb` and uses the new one', () => {
|
198 |
| - expect.assertions(1); |
199 |
| - |
200 | 191 | const beforeBreadcrumb = jest.fn(() => ({ message: 'changed' }));
|
201 | 192 | const options = getDefaultTestClientOptions({ beforeBreadcrumb });
|
202 | 193 | const client = new TestClient(options);
|
203 |
| - const scope = new Scope(); |
204 |
| - // eslint-disable-next-line deprecation/deprecation |
205 |
| - const hub = new Hub(client, scope); |
| 194 | + setCurrentClient(client); |
| 195 | + client.init(); |
206 | 196 |
|
207 |
| - // eslint-disable-next-line deprecation/deprecation |
208 |
| - hub.addBreadcrumb({ message: 'hello' }); |
| 197 | + addBreadcrumb({ message: 'hello' }); |
209 | 198 |
|
210 |
| - expect((scope as any)._breadcrumbs[0].message).toEqual('changed'); |
| 199 | + const isolationScopeBreadcrumbs = getIsolationScope().getScopeData().breadcrumbs; |
| 200 | + expect(isolationScopeBreadcrumbs).toEqual([{ message: 'changed', timestamp: expect.any(Number) }]); |
211 | 201 | });
|
212 | 202 |
|
213 | 203 | test('calls `beforeBreadcrumb` and discards the breadcrumb when returned `null`', () => {
|
214 |
| - expect.assertions(1); |
215 |
| - |
216 | 204 | const beforeBreadcrumb = jest.fn(() => null);
|
217 | 205 | const options = getDefaultTestClientOptions({ beforeBreadcrumb });
|
218 | 206 | const client = new TestClient(options);
|
219 |
| - const scope = new Scope(); |
220 |
| - // eslint-disable-next-line deprecation/deprecation |
221 |
| - const hub = new Hub(client, scope); |
| 207 | + setCurrentClient(client); |
| 208 | + client.init(); |
222 | 209 |
|
223 |
| - // eslint-disable-next-line deprecation/deprecation |
224 |
| - hub.addBreadcrumb({ message: 'hello' }); |
| 210 | + addBreadcrumb({ message: 'hello' }); |
225 | 211 |
|
226 |
| - expect((scope as any)._breadcrumbs.length).toEqual(0); |
| 212 | + const isolationScopeBreadcrumbs = getIsolationScope().getScopeData().breadcrumbs; |
| 213 | + expect(isolationScopeBreadcrumbs).toEqual([]); |
227 | 214 | });
|
228 | 215 |
|
229 | 216 | test('`beforeBreadcrumb` gets an access to a hint as a second argument', () => {
|
230 |
| - expect.assertions(2); |
231 |
| - |
232 | 217 | const beforeBreadcrumb = jest.fn((breadcrumb, hint) => ({ ...breadcrumb, data: hint.data }));
|
233 | 218 | const options = getDefaultTestClientOptions({ beforeBreadcrumb });
|
234 | 219 | const client = new TestClient(options);
|
235 |
| - const scope = new Scope(); |
236 |
| - // eslint-disable-next-line deprecation/deprecation |
237 |
| - const hub = new Hub(client, scope); |
| 220 | + setCurrentClient(client); |
| 221 | + client.init(); |
238 | 222 |
|
239 |
| - // eslint-disable-next-line deprecation/deprecation |
240 |
| - hub.addBreadcrumb({ message: 'hello' }, { data: 'someRandomThing' }); |
| 223 | + addBreadcrumb({ message: 'hello' }, { data: 'someRandomThing' }); |
241 | 224 |
|
242 |
| - expect((scope as any)._breadcrumbs[0].message).toEqual('hello'); |
243 |
| - expect((scope as any)._breadcrumbs[0].data).toEqual('someRandomThing'); |
| 225 | + const isolationScopeBreadcrumbs = getIsolationScope().getScopeData().breadcrumbs; |
| 226 | + expect(isolationScopeBreadcrumbs).toEqual([ |
| 227 | + { message: 'hello', data: 'someRandomThing', timestamp: expect.any(Number) }, |
| 228 | + ]); |
244 | 229 | });
|
245 | 230 | });
|
246 | 231 |
|
@@ -627,13 +612,12 @@ describe('BaseClient', () => {
|
627 | 612 |
|
628 | 613 | const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, maxBreadcrumbs: 1 });
|
629 | 614 | const client = new TestClient(options);
|
| 615 | + setCurrentClient(client); |
| 616 | + client.init(); |
630 | 617 | const scope = new Scope();
|
631 |
| - // eslint-disable-next-line deprecation/deprecation |
632 |
| - const hub = new Hub(client, scope); |
633 |
| - // eslint-disable-next-line deprecation/deprecation |
634 |
| - hub.addBreadcrumb({ message: '1' }); |
635 |
| - // eslint-disable-next-line deprecation/deprecation |
636 |
| - hub.addBreadcrumb({ message: '2' }); |
| 618 | + |
| 619 | + addBreadcrumb({ message: '1' }); |
| 620 | + addBreadcrumb({ message: '2' }); |
637 | 621 |
|
638 | 622 | client.captureEvent({ message: 'message' }, undefined, scope);
|
639 | 623 |
|
|
0 commit comments