-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tests for Next.js middleware and edge routes #6906
Comments
From @onurtemizkan on discord:
|
Damn. Nice job figuring that out. Would have never guessed... |
In summary what I have done was: For e2e tests:
import type { NextApiRequest, NextApiResponse } from 'next';
import * as Sentry from '@sentry/nextjs';
// ### EDGE CONFIG ###
let config = {};
if (process.env.USE_EDGE) {
config = { runtime: 'edge' };
}
export { config };
// ### END ###
export default function handler(req: NextApiRequest, res: NextApiResponse) {
const transaction = Sentry.startTransaction({ name: 'test-transaction', op: 'e2e-test' });
Sentry.getCurrentHub().configureScope(scope => scope.setSpan(transaction));
const span = transaction.startChild();
span.finish();
transaction.finish();
Sentry.flush().then(() => {
res.status(200).json({
transactionIds: global.transactionIds,
});
});
}
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function middleware(req: NextRequest) {
return NextResponse.next();
}
export const config = {
matcher: '/api/(.*)',
};
- And a sentry.edge.config.ts like:
// This file configures the initialization of Sentry on the browser.
// The config you add here will be used whenever a page is visited.
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
import * as Sentry from '@sentry/nextjs';
Sentry.init({
dsn: process.env.NEXT_PUBLIC_E2E_TEST_DSN,
// Adjust this value in production, or use tracesSampler for greater control
tracesSampleRate: 1.0,
// ...
// Note: if you want to override the automatic release value, do not set a
// `release` value here - use the environment variable `SENTRY_RELEASE`, so
// that it will also get attached to your source maps
});
For integration tests:I have ported the sample test application of @lforst. (Not quite ported, more like integrated into the current integration test app) Then got stuck because what I mentioned before (separate globals, no nice way of returning transaction / event data from the API calls). So I was planning to implement an run an Express server working as a transaction/event store service for tests. |
This issue has gone three weeks without activity. In another week, I will close it. But! If you comment or otherwise update it, I will reset the clock, and if you label it "A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀 |
We should add tests for Next.js middleware and edge routes. E2E tests would be fitting since we need to actually verify the build process.
The text was updated successfully, but these errors were encountered: