Skip to content

test(nextjs): Add test for redirects from server actions #14451

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

Merged
merged 1 commit into from
Nov 25, 2024
Merged
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,7 @@
export default function RedirectDestinationPage() {
return (
<div>
<h1>Redirect Destination</h1>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { redirect } from 'next/navigation';

async function redirectAction() {
'use server';

redirect('/redirect/destination');
}

export default function RedirectOriginPage() {
return (
<>
{/* @ts-ignore */}
<form action={redirectAction}>
<button type="submit">Redirect me</button>
</form>
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { expect, test } from '@playwright/test';
import { waitForError, waitForTransaction } from '@sentry-internal/test-utils';

test('Should handle server action redirect without capturing errors', async ({ page }) => {
// Wait for the initial page load transaction
const pageLoadTransactionPromise = waitForTransaction('nextjs-15', async transactionEvent => {
return transactionEvent?.transaction === '/redirect/origin';
});

// Navigate to the origin page
await page.goto('/redirect/origin');

const pageLoadTransaction = await pageLoadTransactionPromise;
expect(pageLoadTransaction).toBeDefined();

// Wait for the redirect transaction
const redirectTransactionPromise = waitForTransaction('nextjs-15', async transactionEvent => {
return transactionEvent?.transaction === 'GET /redirect/destination';
});

// No error should be captured
const redirectErrorPromise = waitForError('nextjs-15', async errorEvent => {
return !!errorEvent;
});

// Click the redirect button
await page.click('button[type="submit"]');

await redirectTransactionPromise;

// Verify we got redirected to the destination page
await expect(page).toHaveURL('/redirect/destination');

// Wait for potential errors with a 2 second timeout
const errorTimeout = new Promise((_, reject) =>
setTimeout(() => reject(new Error('No error captured (timeout)')), 2000),
);

// We expect this to timeout since no error should be captured during the redirect
try {
await Promise.race([redirectErrorPromise, errorTimeout]);
throw new Error('Expected no error to be captured, but an error was found');
} catch (e) {
// If we get a timeout error (as expected), no error was captured
expect((e as Error).message).toBe('No error captured (timeout)');
}
});
Loading