Skip to content

Commit

Permalink
test(e2e): Add test for Next.js middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst committed Jun 19, 2023
1 parent a4c858f commit ee89329
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/e2e-tests/test-applications/nextjs-app-dir/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';

export function middleware() {
return NextResponse.next();
}

// See "Matching Paths" below to learn more
export const config = {
matcher: ['/api/endpoint-behind-middleware'],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { NextApiRequest, NextApiResponse } from 'next';

type Data = {
name: string;
};

export default function handler(req: NextApiRequest, res: NextApiResponse<Data>) {
res.status(200).json({ name: 'John Doe' });
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,18 @@ if (process.env.TEST_ENV === 'production') {
expect((await serverComponentTransactionPromise).contexts?.trace?.status).toBe('not_found');
});
}

test('Should create a transaction for middleware', async ({ request }) => {
const middlewareTransactionPromise = waitForTransaction('nextjs-13-app-dir', async transactionEvent => {
return transactionEvent?.transaction === 'middleware';
});

const response = await request.get('/api/endpoint-behind-middleware');
expect(await response.json()).toStrictEqual({ name: 'John Doe' });

const middlewareTransaction = await middlewareTransactionPromise;

expect(middlewareTransaction.contexts?.trace?.status).toBe('ok');
expect(middlewareTransaction.contexts?.trace?.op).toBe('middleware.nextjs');
expect(middlewareTransaction.contexts?.runtime?.name).toBe('edge');
});

0 comments on commit ee89329

Please sign in to comment.