Skip to content

Commit ee89329

Browse files
author
Luca Forstner
committed
test(e2e): Add test for Next.js middleware
1 parent a4c858f commit ee89329

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { NextResponse } from 'next/server';
2+
import type { NextRequest } from 'next/server';
3+
4+
export function middleware() {
5+
return NextResponse.next();
6+
}
7+
8+
// See "Matching Paths" below to learn more
9+
export const config = {
10+
matcher: ['/api/endpoint-behind-middleware'],
11+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { NextApiRequest, NextApiResponse } from 'next';
2+
3+
type Data = {
4+
name: string;
5+
};
6+
7+
export default function handler(req: NextApiRequest, res: NextApiResponse<Data>) {
8+
res.status(200).json({ name: 'John Doe' });
9+
}

packages/e2e-tests/test-applications/nextjs-app-dir/tests/transactions.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,18 @@ if (process.env.TEST_ENV === 'production') {
112112
expect((await serverComponentTransactionPromise).contexts?.trace?.status).toBe('not_found');
113113
});
114114
}
115+
116+
test('Should create a transaction for middleware', async ({ request }) => {
117+
const middlewareTransactionPromise = waitForTransaction('nextjs-13-app-dir', async transactionEvent => {
118+
return transactionEvent?.transaction === 'middleware';
119+
});
120+
121+
const response = await request.get('/api/endpoint-behind-middleware');
122+
expect(await response.json()).toStrictEqual({ name: 'John Doe' });
123+
124+
const middlewareTransaction = await middlewareTransactionPromise;
125+
126+
expect(middlewareTransaction.contexts?.trace?.status).toBe('ok');
127+
expect(middlewareTransaction.contexts?.trace?.op).toBe('middleware.nextjs');
128+
expect(middlewareTransaction.contexts?.runtime?.name).toBe('edge');
129+
});

0 commit comments

Comments
 (0)