|
| 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 | + }; |
| 34 | + return returnedTransaction; |
| 35 | + }); |
| 36 | + |
| 37 | + const mockedRoutingSpan = { |
| 38 | + finish: () => {}, |
| 39 | + }; |
| 40 | + |
| 41 | + const routingSpanFinishSpy = vi.spyOn(mockedRoutingSpan, 'finish'); |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + navigatingStore = writable(); |
| 45 | + vi.clearAllMocks(); |
| 46 | + }); |
| 47 | + |
| 48 | + it("starts a pageload transaction when it's called with default params", () => { |
| 49 | + svelteKitRoutingInstrumentation(mockedStartTransaction); |
| 50 | + |
| 51 | + expect(mockedStartTransaction).toHaveBeenCalledTimes(1); |
| 52 | + expect(mockedStartTransaction).toHaveBeenCalledWith({ |
| 53 | + name: 'pageload', |
| 54 | + op: 'pageload', |
| 55 | + description: '/', |
| 56 | + }); |
| 57 | + |
| 58 | + // We emit an update to the `page` store to simulate the SvelteKit router lifecycle |
| 59 | + // @ts-ignore This is fine because we testUtils/stores.ts defines `page` as a writable store |
| 60 | + page.set({ route: { id: 'testRoute' } }); |
| 61 | + |
| 62 | + // This should update the transaction name with the parameterized route: |
| 63 | + expect(returnedTransaction?.setName).toHaveBeenCalledTimes(1); |
| 64 | + expect(returnedTransaction?.setName).toHaveBeenCalledWith('testRoute', 'route'); |
| 65 | + }); |
| 66 | + |
| 67 | + it("doesn't start a pageload transaction if `startTransactionOnPageLoad` is false", () => { |
| 68 | + svelteKitRoutingInstrumentation(mockedStartTransaction, false); |
| 69 | + expect(mockedStartTransaction).toHaveBeenCalledTimes(0); |
| 70 | + }); |
| 71 | + |
| 72 | + it("doesn't starts a navigation transaction when `startTransactionOnLocationChange` is false", () => { |
| 73 | + svelteKitRoutingInstrumentation(mockedStartTransaction, false, false); |
| 74 | + |
| 75 | + // We emit an update to the `navigating` store to simulate the SvelteKit navigation lifecycle |
| 76 | + // @ts-ignore This is fine because we testUtils/stores.ts defines `navigating` as a writable store |
| 77 | + navigating.set( |
| 78 | + { from: { route: { id: 'testNavigationOrigin' } } }, |
| 79 | + { to: { route: { id: 'testNavigationDestination' } } }, |
| 80 | + ); |
| 81 | + |
| 82 | + // This should update the transaction name with the parameterized route: |
| 83 | + expect(mockedStartTransaction).toHaveBeenCalledTimes(0); |
| 84 | + }); |
| 85 | + |
| 86 | + it('starts a navigation transaction when `startTransactionOnLocationChange` is true', () => { |
| 87 | + svelteKitRoutingInstrumentation(mockedStartTransaction, false, true); |
| 88 | + |
| 89 | + // We emit an update to the `navigating` store to simulate the SvelteKit navigation lifecycle |
| 90 | + // @ts-ignore This is fine because we testUtils/stores.ts defines `navigating` as a writable store |
| 91 | + navigating.set({ |
| 92 | + from: { route: { id: 'testNavigationOrigin' } }, |
| 93 | + to: { route: { id: 'testNavigationDestination' } }, |
| 94 | + }); |
| 95 | + |
| 96 | + // This should update the transaction name with the parameterized route: |
| 97 | + expect(mockedStartTransaction).toHaveBeenCalledTimes(1); |
| 98 | + expect(mockedStartTransaction).toHaveBeenCalledWith({ |
| 99 | + name: 'testNavigationDestination', |
| 100 | + op: 'navigation', |
| 101 | + metadata: { |
| 102 | + source: 'route', |
| 103 | + }, |
| 104 | + }); |
| 105 | + |
| 106 | + expect(returnedTransaction?.startChild).toHaveBeenCalledWith({ |
| 107 | + op: 'ui.sveltekit.routing', |
| 108 | + description: 'SvelteKit Route Change', |
| 109 | + tags: { |
| 110 | + 'routing.instrumentation': '@sentry/sveltekit', |
| 111 | + from: 'testNavigationOrigin', |
| 112 | + to: 'testNavigationDestination', |
| 113 | + }, |
| 114 | + }); |
| 115 | + |
| 116 | + // We emit `null` here to simulate the end of the navigation lifecycle |
| 117 | + // @ts-ignore this is fine |
| 118 | + navigating.set(null); |
| 119 | + |
| 120 | + expect(routingSpanFinishSpy).toHaveBeenCalledTimes(1); |
| 121 | + }); |
| 122 | +}); |
0 commit comments