-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
test(react-router): Test v8 middleware #17783
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { createContext } from 'react-router'; | ||
|
||
export type User = { | ||
id: string; | ||
name: string; | ||
}; | ||
|
||
export const userContext = createContext<User | null>(null); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { Route } from './+types/with-middleware'; | ||
import type { User } from '../../context'; | ||
import { userContext } from '../../context'; | ||
import * as Sentry from '@sentry/react-router'; | ||
|
||
async function getUser() { | ||
await new Promise(resolve => setTimeout(resolve, 500)); | ||
return { | ||
id: '1', | ||
name: 'Carlos Gomez', | ||
}; | ||
} | ||
|
||
const authMiddleware: Route.MiddlewareFunction = async ({ request, context }, next) => { | ||
Sentry.startSpan({ name: 'authMiddleware', op: 'middleware.auth' }, async () => { | ||
const user: User = await getUser(); | ||
context.set(userContext, user); | ||
await next(); | ||
}); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Middleware Timing Issue with Sentry SpansThe |
||
|
||
export const middleware: Route.MiddlewareFunction[] = [authMiddleware]; | ||
|
||
export const loader = async ({ context }: Route.LoaderArgs) => { | ||
const user = context.get(userContext); | ||
return { user }; | ||
}; | ||
|
||
export default function WithMiddlewarePage({ loaderData }: Route.ComponentProps) { | ||
const { user } = loaderData; | ||
|
||
return ( | ||
<div> | ||
<h1>With Middleware Page</h1> | ||
<p>User: {user?.name}</p> | ||
</div> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { expect, test } from '@playwright/test'; | ||
import { waitForTransaction } from '@sentry-internal/test-utils'; | ||
import { APP_NAME } from '../constants'; | ||
|
||
test.describe('server - middleware', () => { | ||
test('should send middleware transaction on pageload', async ({ page }) => { | ||
const serverTxPromise = waitForTransaction(APP_NAME, async transactionEvent => { | ||
return transactionEvent.transaction === 'GET /performance/with-middleware'; | ||
}); | ||
|
||
const pageloadTxPromise = waitForTransaction(APP_NAME, async transactionEvent => { | ||
return transactionEvent.transaction === '/performance/with-middleware'; | ||
}); | ||
|
||
const customMiddlewareTxPromise = waitForTransaction(APP_NAME, async transactionEvent => { | ||
return transactionEvent.transaction === 'authMiddleware'; | ||
}); | ||
|
||
await page.goto(`/performance/with-middleware`); | ||
|
||
const serverTx = await serverTxPromise; | ||
const pageloadTx = await pageloadTxPromise; | ||
const customMiddlewareTx = await customMiddlewareTxPromise; | ||
|
||
const traceIds = { | ||
server: serverTx?.contexts?.trace?.trace_id, | ||
pageload: pageloadTx?.contexts?.trace?.trace_id, | ||
customMiddleware: customMiddlewareTx?.contexts?.trace?.trace_id, | ||
}; | ||
|
||
expect(pageloadTx).toBeDefined(); | ||
expect(customMiddlewareTx).toBeDefined(); | ||
expect(pageloadTx).toBeDefined(); | ||
cursor[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
chargome marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
// Assert that all transactions belong to the same trace | ||
expect(traceIds.server).toBe(traceIds.pageload); | ||
expect(traceIds.server).toBe(traceIds.customMiddleware); | ||
}); | ||
}); |
Uh oh!
There was an error while loading. Please reload this page.