Skip to content

Commit 1fd377f

Browse files
authored
test(nextjs): Add test for redirects from server actions (#14451)
1 parent 8d1dbb1 commit 1fd377f

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function RedirectDestinationPage() {
2+
return (
3+
<div>
4+
<h1>Redirect Destination</h1>
5+
</div>
6+
);
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { redirect } from 'next/navigation';
2+
3+
async function redirectAction() {
4+
'use server';
5+
6+
redirect('/redirect/destination');
7+
}
8+
9+
export default function RedirectOriginPage() {
10+
return (
11+
<>
12+
{/* @ts-ignore */}
13+
<form action={redirectAction}>
14+
<button type="submit">Redirect me</button>
15+
</form>
16+
</>
17+
);
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { expect, test } from '@playwright/test';
2+
import { waitForError, waitForTransaction } from '@sentry-internal/test-utils';
3+
4+
test('Should handle server action redirect without capturing errors', async ({ page }) => {
5+
// Wait for the initial page load transaction
6+
const pageLoadTransactionPromise = waitForTransaction('nextjs-15', async transactionEvent => {
7+
return transactionEvent?.transaction === '/redirect/origin';
8+
});
9+
10+
// Navigate to the origin page
11+
await page.goto('/redirect/origin');
12+
13+
const pageLoadTransaction = await pageLoadTransactionPromise;
14+
expect(pageLoadTransaction).toBeDefined();
15+
16+
// Wait for the redirect transaction
17+
const redirectTransactionPromise = waitForTransaction('nextjs-15', async transactionEvent => {
18+
return transactionEvent?.transaction === 'GET /redirect/destination';
19+
});
20+
21+
// No error should be captured
22+
const redirectErrorPromise = waitForError('nextjs-15', async errorEvent => {
23+
return !!errorEvent;
24+
});
25+
26+
// Click the redirect button
27+
await page.click('button[type="submit"]');
28+
29+
await redirectTransactionPromise;
30+
31+
// Verify we got redirected to the destination page
32+
await expect(page).toHaveURL('/redirect/destination');
33+
34+
// Wait for potential errors with a 2 second timeout
35+
const errorTimeout = new Promise((_, reject) =>
36+
setTimeout(() => reject(new Error('No error captured (timeout)')), 2000),
37+
);
38+
39+
// We expect this to timeout since no error should be captured during the redirect
40+
try {
41+
await Promise.race([redirectErrorPromise, errorTimeout]);
42+
throw new Error('Expected no error to be captured, but an error was found');
43+
} catch (e) {
44+
// If we get a timeout error (as expected), no error was captured
45+
expect((e as Error).message).toBe('No error captured (timeout)');
46+
}
47+
});

0 commit comments

Comments
 (0)