|
| 1 | +import type { Envelope, Event } from '@sentry/core'; |
| 2 | +import { expect, it } from 'vitest'; |
| 3 | +import { createRunner } from '../../runner'; |
| 4 | + |
| 5 | +it('cacheClient: false - two consecutive invocations get different isolation scopes', async ({ signal }) => { |
| 6 | + const runner = createRunner(__dirname).ignore('transaction').start(signal); |
| 7 | + |
| 8 | + await runner.makeRequestAndWaitForEnvelope('get', '/scope?seed=1', (envelope: Envelope) => { |
| 9 | + const event = envelope[1]?.[0]?.[1] as Event; |
| 10 | + expect(event.exception?.values?.[0]?.value).toBe('Scope seed'); |
| 11 | + // Guards the probe assertions below against passing vacuously: the seeding invocation really |
| 12 | + // did write to its isolation scope. |
| 13 | + expect(event.tags).toEqual(expect.objectContaining({ seeded_tag: 'from-seeding-invocation' })); |
| 14 | + expect(event.user).toEqual({ id: 'user-from-seeding-invocation' }); |
| 15 | + }); |
| 16 | + |
| 17 | + await runner.makeRequestAndWaitForEnvelope('get', '/scope?seed=0', (envelope: Envelope) => { |
| 18 | + const event = envelope[1]?.[0]?.[1] as Event; |
| 19 | + expect(event.exception?.values?.[0]?.value).toBe('Scope probe'); |
| 20 | + expect(event.tags?.seeded_tag).toBeUndefined(); |
| 21 | + expect(event.user).toBeUndefined(); |
| 22 | + }); |
| 23 | +}); |
| 24 | + |
| 25 | +it('a nested direct call within one invocation shares the same isolation scope', async ({ signal }) => { |
| 26 | + const runner = createRunner(__dirname).ignore('transaction').start(signal); |
| 27 | + |
| 28 | + await runner.makeRequestAndWaitForEnvelope('get', '/nested', (envelope: Envelope) => { |
| 29 | + const event = envelope[1]?.[0]?.[1] as Event; |
| 30 | + expect(event.exception?.values?.[0]?.value).toBe('Nested outer'); |
| 31 | + // The event must carry data written on both sides of the nested call: `outer_tag` from |
| 32 | + // before it, `inner_tag` and the user from inside it — anything less means the nested |
| 33 | + // call ran in its own scope. |
| 34 | + expect(event.tags).toEqual( |
| 35 | + expect.objectContaining({ |
| 36 | + outer_tag: 'from-outer-method', |
| 37 | + inner_tag: 'from-inner-method', |
| 38 | + }), |
| 39 | + ); |
| 40 | + expect(event.user).toEqual({ id: 'user-from-inner-method' }); |
| 41 | + }); |
| 42 | + |
| 43 | + // Whatever the nested invocation wrote must not survive into the next invocation. |
| 44 | + await runner.makeRequestAndWaitForEnvelope('get', '/scope?seed=0', (envelope: Envelope) => { |
| 45 | + const event = envelope[1]?.[0]?.[1] as Event; |
| 46 | + expect(event.exception?.values?.[0]?.value).toBe('Scope probe'); |
| 47 | + expect(event.tags?.outer_tag).toBeUndefined(); |
| 48 | + expect(event.tags?.inner_tag).toBeUndefined(); |
| 49 | + expect(event.user).toBeUndefined(); |
| 50 | + }); |
| 51 | +}); |
| 52 | + |
| 53 | +it('a nested call into another instrumented handler shares the same isolation scope', async ({ signal }) => { |
| 54 | + const runner = createRunner(__dirname).ignore('transaction').start(signal); |
| 55 | + |
| 56 | + await runner.makeRequestAndWaitForEnvelope('get', '/reentrant', (envelope: Envelope) => { |
| 57 | + const event = envelope[1]?.[0]?.[1] as Event; |
| 58 | + expect(event.exception?.values?.[0]?.value).toBe('Reentrant inner'); |
| 59 | + // `fetch` is itself instrumented and opens an isolation scope. Reached from inside the RPC |
| 60 | + // invocation it must not fork again, or it would not see what the RPC method set. |
| 61 | + expect(event.tags).toEqual( |
| 62 | + expect.objectContaining({ |
| 63 | + reentrant_outer_tag: 'from-rpc-method', |
| 64 | + fetch_tag: 'from-nested-fetch', |
| 65 | + }), |
| 66 | + ); |
| 67 | + expect(event.user).toEqual({ id: 'user-from-rpc-method' }); |
| 68 | + }); |
| 69 | + |
| 70 | + await runner.makeRequestAndWaitForEnvelope('get', '/scope?seed=0', (envelope: Envelope) => { |
| 71 | + const event = envelope[1]?.[0]?.[1] as Event; |
| 72 | + expect(event.exception?.values?.[0]?.value).toBe('Scope probe'); |
| 73 | + expect(event.tags?.reentrant_outer_tag).toBeUndefined(); |
| 74 | + expect(event.tags?.fetch_tag).toBeUndefined(); |
| 75 | + expect(event.user).toBeUndefined(); |
| 76 | + }); |
| 77 | +}); |
0 commit comments