-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(nextjs): Add NextJS server-side E2E tests. (#6829)
- Loading branch information
1 parent
bee8d52
commit d1ddeff
Showing
9 changed files
with
131 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/e2e-tests/test-applications/create-next-app/pages/api/error.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import * as Sentry from '@sentry/nextjs'; | ||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
const exceptionId = Sentry.captureException(new Error('This is an error')); | ||
|
||
await Sentry.flush(2000); | ||
|
||
res.status(200).json({ exceptionId }); | ||
} |
10 changes: 0 additions & 10 deletions
10
packages/e2e-tests/test-applications/create-next-app/pages/api/hello.ts
This file was deleted.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
packages/e2e-tests/test-applications/create-next-app/pages/api/success.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
import * as Sentry from '@sentry/nextjs'; | ||
|
||
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, | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
packages/e2e-tests/test-applications/create-next-app/tests/behaviour-server.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { test, expect } from '@playwright/test'; | ||
import axios, { AxiosError } from 'axios'; | ||
|
||
const authToken = process.env.E2E_TEST_AUTH_TOKEN; | ||
const sentryTestOrgSlug = process.env.E2E_TEST_SENTRY_ORG_SLUG; | ||
const sentryTestProject = process.env.E2E_TEST_SENTRY_TEST_PROJECT; | ||
const EVENT_POLLING_TIMEOUT = 30_000; | ||
|
||
test('Sends a server-side exception to Sentry', async ({ baseURL }) => { | ||
const { data } = await axios.get(`${baseURL}/api/error`); | ||
const { exceptionId } = data; | ||
|
||
const url = `https://sentry.io/api/0/projects/${sentryTestOrgSlug}/${sentryTestProject}/events/${exceptionId}/`; | ||
|
||
console.log(`Polling for error eventId: ${exceptionId}`); | ||
|
||
await expect | ||
.poll( | ||
async () => { | ||
try { | ||
const response = await axios.get(url, { headers: { Authorization: `Bearer ${authToken}` } }); | ||
|
||
return response.status; | ||
} catch (e) { | ||
if (e instanceof AxiosError && e.response) { | ||
if (e.response.status !== 404) { | ||
throw e; | ||
} else { | ||
return e.response.status; | ||
} | ||
} else { | ||
throw e; | ||
} | ||
} | ||
}, | ||
{ timeout: EVENT_POLLING_TIMEOUT }, | ||
) | ||
.toBe(200); | ||
}); | ||
|
||
test('Sends server-side transactions to Sentry', async ({ baseURL }) => { | ||
const { data } = await axios.get(`${baseURL}/api/success`); | ||
const { transactionIds } = data; | ||
|
||
console.log(`Polling for transaction eventIds: ${JSON.stringify(transactionIds)}`); | ||
|
||
await Promise.all( | ||
transactionIds.map(async (transactionId: string) => { | ||
const url = `https://sentry.io/api/0/projects/${sentryTestOrgSlug}/${sentryTestProject}/events/${transactionId}/`; | ||
|
||
await expect | ||
.poll( | ||
async () => { | ||
try { | ||
const response = await axios.get(url, { headers: { Authorization: `Bearer ${authToken}` } }); | ||
|
||
return response.status; | ||
} catch (e) { | ||
if (e instanceof AxiosError && e.response) { | ||
if (e.response.status !== 404) { | ||
throw e; | ||
} else { | ||
return e.response.status; | ||
} | ||
} else { | ||
throw e; | ||
} | ||
} | ||
}, | ||
{ timeout: EVENT_POLLING_TIMEOUT }, | ||
) | ||
.toBe(200); | ||
}), | ||
); | ||
}); |