|
| 1 | +/* eslint-disable @typescript-eslint/unbound-method */ |
| 2 | +import type { Transaction } from '@sentry/types'; |
| 3 | +import { writable } from 'svelte/store'; |
| 4 | +import type { SpyInstance } from 'vitest'; |
| 5 | +import { vi } from 'vitest'; |
| 6 | + |
| 7 | +import { navigating, page } from '$app/stores'; |
| 8 | + |
| 9 | +import { svelteKitRoutingInstrumentation } from '../../src/client/router'; |
| 10 | + |
| 11 | +// we have to overwrite the global mock from `vitest.setup.ts` here to reset the |
| 12 | +// `navigating` store for each test. |
| 13 | +vi.mock('$app/stores', async () => { |
| 14 | + return { |
| 15 | + get navigating() { |
| 16 | + return navigatingStore; |
| 17 | + }, |
| 18 | + page: writable(), |
| 19 | + }; |
| 20 | +}); |
| 21 | + |
| 22 | +let navigatingStore = writable(); |
| 23 | + |
| 24 | +describe('sveltekitRoutingInstrumentation', () => { |
| 25 | + let returnedTransaction: (Transaction & { returnedTransaction: SpyInstance }) | undefined; |
| 26 | + const mockedStartTransaction = vi.fn().mockImplementation(txnCtx => { |
| 27 | + returnedTransaction = { |
| 28 | + ...txnCtx, |
| 29 | + setName: vi.fn(), |
| 30 | + startChild: vi.fn().mockImplementation(ctx => { |
| 31 | + return { ...mockedRoutingSpan, ...ctx }; |
| 32 | + }), |
| 33 | + setTag: vi.fn(), |
| 34 | + }; |
| 35 | + return returnedTransaction; |
| 36 | + }); |
| 37 | + |
| 38 | + const mockedRoutingSpan = { |
| 39 | + finish: () => {}, |
| 40 | + }; |
| 41 | + |
| 42 | + const routingSpanFinishSpy = vi.spyOn(mockedRoutingSpan, 'finish'); |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + navigatingStore = writable(); |
| 46 | + vi.clearAllMocks(); |
| 47 | + }); |
| 48 | + |
| 49 | + it("starts a pageload transaction when it's called with default params", () => { |
| 50 | + svelteKitRoutingInstrumentation(mockedStartTransaction); |
| 51 | + |
| 52 | + expect(mockedStartTransaction).toHaveBeenCalledTimes(1); |
| 53 | + expect(mockedStartTransaction).toHaveBeenCalledWith({ |
| 54 | + name: '/', |
| 55 | + op: 'pageload', |
| 56 | + description: '/', |
| 57 | + tags: { |
| 58 | + 'routing.instrumentation': '@sentry/sveltekit', |
| 59 | + }, |
| 60 | + }); |
| 61 | + |
| 62 | + // We emit an update to the `page` store to simulate the SvelteKit router lifecycle |
| 63 | + // @ts-ignore This is fine because we testUtils/stores.ts defines `page` as a writable store |
| 64 | + page.set({ route: { id: 'testRoute' } }); |
| 65 | + |
| 66 | + // This should update the transaction name with the parameterized route: |
| 67 | + expect(returnedTransaction?.setName).toHaveBeenCalledTimes(1); |
| 68 | + expect(returnedTransaction?.setName).toHaveBeenCalledWith('testRoute', 'route'); |
| 69 | + }); |
| 70 | + |
| 71 | + it("doesn't start a pageload transaction if `startTransactionOnPageLoad` is false", () => { |
| 72 | + svelteKitRoutingInstrumentation(mockedStartTransaction, false); |
| 73 | + expect(mockedStartTransaction).toHaveBeenCalledTimes(0); |
| 74 | + }); |
| 75 | + |
| 76 | + it("doesn't starts a navigation transaction when `startTransactionOnLocationChange` is false", () => { |
| 77 | + svelteKitRoutingInstrumentation(mockedStartTransaction, false, false); |
| 78 | + |
| 79 | + // We emit an update to the `navigating` store to simulate the SvelteKit navigation lifecycle |
| 80 | + // @ts-ignore This is fine because we testUtils/stores.ts defines `navigating` as a writable store |
| 81 | + navigating.set( |
| 82 | + { from: { route: { id: 'testNavigationOrigin' } } }, |
| 83 | + { to: { route: { id: 'testNavigationDestination' } } }, |
| 84 | + ); |
| 85 | + |
| 86 | + // This should update the transaction name with the parameterized route: |
| 87 | + expect(mockedStartTransaction).toHaveBeenCalledTimes(0); |
| 88 | + }); |
| 89 | + |
| 90 | + it('starts a navigation transaction when `startTransactionOnLocationChange` is true', () => { |
| 91 | + svelteKitRoutingInstrumentation(mockedStartTransaction, false, true); |
| 92 | + |
| 93 | + // We emit an update to the `navigating` store to simulate the SvelteKit navigation lifecycle |
| 94 | + // @ts-ignore This is fine because we testUtils/stores.ts defines `navigating` as a writable store |
| 95 | + navigating.set({ |
| 96 | + from: { route: { id: 'testNavigationOrigin' } }, |
| 97 | + to: { route: { id: 'testNavigationDestination' } }, |
| 98 | + }); |
| 99 | + |
| 100 | + // This should update the transaction name with the parameterized route: |
| 101 | + expect(mockedStartTransaction).toHaveBeenCalledTimes(1); |
| 102 | + expect(mockedStartTransaction).toHaveBeenCalledWith({ |
| 103 | + name: 'testNavigationDestination', |
| 104 | + op: 'navigation', |
| 105 | + metadata: { |
| 106 | + source: 'route', |
| 107 | + }, |
| 108 | + tags: { |
| 109 | + 'routing.instrumentation': '@sentry/sveltekit', |
| 110 | + }, |
| 111 | + }); |
| 112 | + |
| 113 | + expect(returnedTransaction?.startChild).toHaveBeenCalledWith({ |
| 114 | + op: 'ui.sveltekit.routing', |
| 115 | + description: 'SvelteKit Route Change', |
| 116 | + }); |
| 117 | + |
| 118 | + expect(returnedTransaction?.setTag).toHaveBeenCalledWith('from', 'testNavigationOrigin'); |
| 119 | + |
| 120 | + // We emit `null` here to simulate the end of the navigation lifecycle |
| 121 | + // @ts-ignore this is fine |
| 122 | + navigating.set(null); |
| 123 | + |
| 124 | + expect(routingSpanFinishSpy).toHaveBeenCalledTimes(1); |
| 125 | + }); |
| 126 | + |
| 127 | + it("doesn't start a navigation transaction if navigation origin and destination are equal", () => { |
| 128 | + svelteKitRoutingInstrumentation(mockedStartTransaction, false, true); |
| 129 | + |
| 130 | + // We emit an update to the `navigating` store to simulate the SvelteKit navigation lifecycle |
| 131 | + // @ts-ignore This is fine because we testUtils/stores.ts defines `navigating` as a writable store |
| 132 | + navigating.set({ |
| 133 | + from: { route: { id: 'testRoute' } }, |
| 134 | + to: { route: { id: 'testRoute' } }, |
| 135 | + }); |
| 136 | + |
| 137 | + expect(mockedStartTransaction).toHaveBeenCalledTimes(0); |
| 138 | + }); |
| 139 | +}); |
0 commit comments