From 41fc96c964cd48ffe3a1c6eccd879ddf7a7bc434 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 19 Jun 2023 10:52:03 +0000 Subject: [PATCH 1/2] test(e2e): Add tests for Next.js edge routes --- .../nextjs-app-dir/pages/api/edge-endpoint.ts | 18 +++++- .../pages/api/error-edge-endpoint.ts | 5 ++ .../nextjs-app-dir/tests/edge-route.test.ts | 56 +++++++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 packages/e2e-tests/test-applications/nextjs-app-dir/pages/api/error-edge-endpoint.ts create mode 100644 packages/e2e-tests/test-applications/nextjs-app-dir/tests/edge-route.test.ts diff --git a/packages/e2e-tests/test-applications/nextjs-app-dir/pages/api/edge-endpoint.ts b/packages/e2e-tests/test-applications/nextjs-app-dir/pages/api/edge-endpoint.ts index d8af89f2e9d5..b2b2dfdf4fc3 100644 --- a/packages/e2e-tests/test-applications/nextjs-app-dir/pages/api/edge-endpoint.ts +++ b/packages/e2e-tests/test-applications/nextjs-app-dir/pages/api/edge-endpoint.ts @@ -1,3 +1,17 @@ -export const config = { runtime: 'edge' }; +export const config = { + runtime: 'edge', +}; -export default () => new Response('Hello world!'); +export default async function handler() { + return new Response( + JSON.stringify({ + name: 'Jim Halpert', + }), + { + status: 200, + headers: { + 'content-type': 'application/json', + }, + }, + ); +} diff --git a/packages/e2e-tests/test-applications/nextjs-app-dir/pages/api/error-edge-endpoint.ts b/packages/e2e-tests/test-applications/nextjs-app-dir/pages/api/error-edge-endpoint.ts new file mode 100644 index 000000000000..043112494c23 --- /dev/null +++ b/packages/e2e-tests/test-applications/nextjs-app-dir/pages/api/error-edge-endpoint.ts @@ -0,0 +1,5 @@ +export const config = { runtime: 'edge' }; + +export default () => { + throw new Error('Edge Route Error'); +}; diff --git a/packages/e2e-tests/test-applications/nextjs-app-dir/tests/edge-route.test.ts b/packages/e2e-tests/test-applications/nextjs-app-dir/tests/edge-route.test.ts new file mode 100644 index 000000000000..d5eaf3eb9aa6 --- /dev/null +++ b/packages/e2e-tests/test-applications/nextjs-app-dir/tests/edge-route.test.ts @@ -0,0 +1,56 @@ +import { test, expect } from '@playwright/test'; +import { waitForTransaction, waitForError } from '../../../test-utils/event-proxy-server'; + +test('Should create a transaction for edge routes', async ({ request }) => { + // test.skip(process.env.TEST_ENV === 'development', "Doesn't work in dev mode."); + + const edgerouteTransactionPromise = waitForTransaction('nextjs-13-app-dir', async transactionEvent => { + return ( + transactionEvent?.transaction === 'GET /api/edge-endpoint' && transactionEvent?.contexts?.trace?.status === 'ok' + ); + }); + + const response = await request.get('/api/edge-endpoint'); + expect(await response.json()).toStrictEqual({ name: 'Jim Halpert' }); + + const edgerouteTransaction = await edgerouteTransactionPromise; + + expect(edgerouteTransaction.contexts?.trace?.status).toBe('ok'); + expect(edgerouteTransaction.contexts?.trace?.op).toBe('http.server'); + expect(edgerouteTransaction.contexts?.runtime?.name).toBe('edge'); +}); + +test('Should create a transaction with error status for faulty edge routes', async ({ request }) => { + // test.skip(process.env.TEST_ENV === 'development', "Doesn't work in dev mode."); + + const edgerouteTransactionPromise = waitForTransaction('nextjs-13-app-dir', async transactionEvent => { + return ( + transactionEvent?.transaction === 'GET /api/error-edge-endpoint' && + transactionEvent?.contexts?.trace?.status === 'internal_error' + ); + }); + + request.get('/api/error-edge-endpoint').catch(() => { + // Noop + }); + + const edgerouteTransaction = await edgerouteTransactionPromise; + + expect(edgerouteTransaction.contexts?.trace?.status).toBe('internal_error'); + expect(edgerouteTransaction.contexts?.trace?.op).toBe('http.server'); + expect(edgerouteTransaction.contexts?.runtime?.name).toBe('edge'); +}); + +test('Should record exceptions for faulty edge routes', async ({ request }) => { + // test.skip(process.env.TEST_ENV === 'development', "Doesn't work in dev mode."); + + const errorEventPromise = waitForError('nextjs-13-app-dir', errorEvent => { + return errorEvent?.exception?.values?.[0]?.value === 'Edge Route Error'; + }); + + request.get('/api/error-edge-endpoint').catch(() => { + // Noop + }); + + expect(await errorEventPromise).toBeDefined(); +}); From c5528027ae61a47e683fcacc322e8516fd99f8dd Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 19 Jun 2023 11:06:35 +0000 Subject: [PATCH 2/2] Skip dev mode --- .../nextjs-app-dir/tests/edge-route.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/e2e-tests/test-applications/nextjs-app-dir/tests/edge-route.test.ts b/packages/e2e-tests/test-applications/nextjs-app-dir/tests/edge-route.test.ts index d5eaf3eb9aa6..8f73764a919f 100644 --- a/packages/e2e-tests/test-applications/nextjs-app-dir/tests/edge-route.test.ts +++ b/packages/e2e-tests/test-applications/nextjs-app-dir/tests/edge-route.test.ts @@ -2,7 +2,7 @@ import { test, expect } from '@playwright/test'; import { waitForTransaction, waitForError } from '../../../test-utils/event-proxy-server'; test('Should create a transaction for edge routes', async ({ request }) => { - // test.skip(process.env.TEST_ENV === 'development', "Doesn't work in dev mode."); + test.skip(process.env.TEST_ENV === 'development', "Doesn't work in dev mode."); const edgerouteTransactionPromise = waitForTransaction('nextjs-13-app-dir', async transactionEvent => { return ( @@ -21,7 +21,7 @@ test('Should create a transaction for edge routes', async ({ request }) => { }); test('Should create a transaction with error status for faulty edge routes', async ({ request }) => { - // test.skip(process.env.TEST_ENV === 'development', "Doesn't work in dev mode."); + test.skip(process.env.TEST_ENV === 'development', "Doesn't work in dev mode."); const edgerouteTransactionPromise = waitForTransaction('nextjs-13-app-dir', async transactionEvent => { return ( @@ -42,7 +42,7 @@ test('Should create a transaction with error status for faulty edge routes', asy }); test('Should record exceptions for faulty edge routes', async ({ request }) => { - // test.skip(process.env.TEST_ENV === 'development', "Doesn't work in dev mode."); + test.skip(process.env.TEST_ENV === 'development', "Doesn't work in dev mode."); const errorEventPromise = waitForError('nextjs-13-app-dir', errorEvent => { return errorEvent?.exception?.values?.[0]?.value === 'Edge Route Error';