Skip to content

Commit

Permalink
test(e2e): Add tests for Next.js edge routes
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst committed Jun 19, 2023
1 parent 3f7dc52 commit 41fc96c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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',
},
},
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const config = { runtime: 'edge' };

export default () => {
throw new Error('Edge Route Error');
};
Original file line number Diff line number Diff line change
@@ -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();
});

0 comments on commit 41fc96c

Please sign in to comment.