Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

test(nextjs): Add route handler tests for turbopack #17515

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

Open
chargome wants to merge 4 commits into develop
base: develop
Choose a base branch
Loading
from cg-add-route-handler-tests
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export async function GET(request: Request) {
throw new Error('Dynamic route handler error');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { NextResponse } from 'next/server';

export async function GET() {
return NextResponse.json({ name: 'Beep' });
}

export async function POST() {
return NextResponse.json({ name: 'Boop' }, { status: 400 });
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { NextResponse } from 'next/server';

export async function GET(request: Request) {
return NextResponse.json({ name: 'Static' });
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { expect, test } from '@playwright/test';
import { waitForError, waitForTransaction } from '@sentry-internal/test-utils';

test('Should create a transaction for dynamic route handlers', async ({ request }) => {
const routehandlerTransactionPromise = waitForTransaction('nextjs-turbo', async transactionEvent => {
return transactionEvent?.transaction === 'GET /route-handlers/[param]';
});

const response = await request.get('/route-handlers/foo');
expect(await response.json()).toStrictEqual({ name: 'Beep' });

const routehandlerTransaction = await routehandlerTransactionPromise;

expect(routehandlerTransaction.contexts?.trace?.status).toBe('ok');
expect(routehandlerTransaction.contexts?.trace?.op).toBe('http.server');
});

test('Should create a transaction for static route handlers', async ({ request }) => {
const routehandlerTransactionPromise = waitForTransaction('nextjs-turbo', async transactionEvent => {
return transactionEvent?.transaction === 'GET /route-handlers/static';
});

const response = await request.get('/route-handlers/static');
expect(await response.json()).toStrictEqual({ name: 'Static' });

const routehandlerTransaction = await routehandlerTransactionPromise;

expect(routehandlerTransaction.contexts?.trace?.status).toBe('ok');
expect(routehandlerTransaction.contexts?.trace?.op).toBe('http.server');
});

test('Should create a transaction for route handlers and correctly set span status depending on http status', async ({
request,
}) => {
const routehandlerTransactionPromise = waitForTransaction('nextjs-turbo', async transactionEvent => {
return transactionEvent?.transaction === 'POST /route-handlers/[param]';
});

const response = await request.post('/route-handlers/bar');
expect(await response.json()).toStrictEqual({ name: 'Boop' });

const routehandlerTransaction = await routehandlerTransactionPromise;

expect(routehandlerTransaction.contexts?.trace?.status).toBe('invalid_argument');
expect(routehandlerTransaction.contexts?.trace?.op).toBe('http.server');
});

test('Should record exceptions and transactions for faulty route handlers', async ({ request }) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can make this test.fail so it will expect to fail and we will be "notified" when this is fixed! (+ add a comment for this)

chargome reacted with eyes emoji
Copy link
Member Author

@chargome chargome Sep 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test.fail seems to not work with test timeouts – I added a workaround so the test fails once the issue is resolved upstream

Copy link
Member Author

@chargome chargome Sep 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, this is already fixed in canary. leaving as is

const errorEventPromise = waitForError('nextjs-turbo', errorEvent => {
return errorEvent?.exception?.values?.[0]?.value === 'Dynamic route handler error';
});

const routehandlerTransactionPromise = waitForTransaction('nextjs-turbo', async transactionEvent => {
return transactionEvent?.transaction === 'GET /route-handlers/[param]/error';
});

await request.get('/route-handlers/boop/error').catch(() => {});

const routehandlerTransaction = await routehandlerTransactionPromise;
const routehandlerError = await errorEventPromise;

expect(routehandlerTransaction.contexts?.trace?.status).toBe('internal_error');
expect(routehandlerTransaction.contexts?.trace?.op).toBe('http.server');
expect(routehandlerTransaction.contexts?.trace?.origin).toContain('auto');

expect(routehandlerError.exception?.values?.[0].value).toBe('Dynamic route handler error');

expect(routehandlerError.request?.method).toBe('GET');
expect(routehandlerError.request?.url).toContain('/route-handlers/boop/error');

expect(routehandlerError.transaction).toBe('GET /route-handlers/[param]/error');
});
Loading

AltStyle によって変換されたページ (->オリジナル) /