|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | +import { waitForTransaction } from '@sentry-internal/test-utils'; |
| 3 | +import { APP_NAME } from './constants'; |
| 4 | + |
| 5 | +test.describe('basePath with sub-app routes', () => { |
| 6 | + test('traces GET on a sub-app mounted via .basePath().route()', async ({ baseURL }) => { |
| 7 | + const transactionPromise = waitForTransaction(APP_NAME, event => { |
| 8 | + return event.contexts?.trace?.op === 'http.server' && event.transaction === 'GET /test-basepath/v1/users'; |
| 9 | + }); |
| 10 | + |
| 11 | + const response = await fetch(`${baseURL}/test-basepath/v1/users`); |
| 12 | + expect(response.status).toBe(200); |
| 13 | + |
| 14 | + const body = await response.json(); |
| 15 | + expect(body).toEqual({ users: [{ id: 1, name: 'Alice' }] }); |
| 16 | + |
| 17 | + const transaction = await transactionPromise; |
| 18 | + expect(transaction.transaction).toBe('GET /test-basepath/v1/users'); |
| 19 | + expect(transaction.contexts?.trace?.op).toBe('http.server'); |
| 20 | + }); |
| 21 | + |
| 22 | + test('traces parameterized route under .basePath().route()', async ({ baseURL }) => { |
| 23 | + const transactionPromise = waitForTransaction(APP_NAME, event => { |
| 24 | + return event.contexts?.trace?.op === 'http.server' && event.transaction === 'GET /test-basepath/v1/users/:userId'; |
| 25 | + }); |
| 26 | + |
| 27 | + const response = await fetch(`${baseURL}/test-basepath/v1/users/42`); |
| 28 | + expect(response.status).toBe(200); |
| 29 | + |
| 30 | + const body = await response.json(); |
| 31 | + expect(body).toEqual({ userId: '42' }); |
| 32 | + |
| 33 | + const transaction = await transactionPromise; |
| 34 | + expect(transaction.transaction).toBe('GET /test-basepath/v1/users/:userId'); |
| 35 | + expect(transaction.contexts?.trace?.op).toBe('http.server'); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +// TODO: this test is currently skipped because we do not yet support middleware registered on new instances (e.g. here via .basePath(..).use(...)). |
| 40 | +test.skip('.basePath() middleware instrumentation', () => { |
| 41 | + test('creates middleware span for .use() on .basePath() clone', async ({ baseURL }) => { |
| 42 | + const transactionPromise = waitForTransaction(APP_NAME, event => { |
| 43 | + return event.contexts?.trace?.op === 'http.server' && event.transaction === 'GET /test-basepath-mw/hello'; |
| 44 | + }); |
| 45 | + |
| 46 | + const response = await fetch(`${baseURL}/test-basepath-mw/hello`); |
| 47 | + expect(response.status).toBe(200); |
| 48 | + |
| 49 | + const body = await response.json(); |
| 50 | + expect(body).toEqual({ greeting: 'world' }); |
| 51 | + |
| 52 | + const transaction = await transactionPromise; |
| 53 | + expect(transaction.transaction).toBe('GET /test-basepath-mw/hello'); |
| 54 | + |
| 55 | + const spans = transaction.spans || []; |
| 56 | + const middlewareSpan = spans.find( |
| 57 | + (span: { description?: string; op?: string }) => |
| 58 | + span.op === 'middleware.hono' && span.description === 'basepathMiddleware', |
| 59 | + ); |
| 60 | + |
| 61 | + expect(middlewareSpan).toBeDefined(); |
| 62 | + expect(middlewareSpan?.origin).toBe('auto.middleware.hono'); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +test('traces .get() route registered after .basePath()/.route() chains', async ({ baseURL }) => { |
| 67 | + const transactionPromise = waitForTransaction(APP_NAME, event => { |
| 68 | + return event.contexts?.trace?.op === 'http.server' && event.transaction === 'GET /test-late-get'; |
| 69 | + }); |
| 70 | + |
| 71 | + const response = await fetch(`${baseURL}/test-late-get`); |
| 72 | + expect(response.status).toBe(200); |
| 73 | + |
| 74 | + const body = await response.json(); |
| 75 | + expect(body).toEqual({ registered: 'after-chains' }); |
| 76 | + |
| 77 | + const transaction = await transactionPromise; |
| 78 | + expect(transaction.transaction).toBe('GET /test-late-get'); |
| 79 | + expect(transaction.contexts?.trace?.op).toBe('http.server'); |
| 80 | +}); |
0 commit comments