Skip to content

test(e2e): Fix node E2E test app #11682

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

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import * as Sentry from '@sentry/node';
import { TRPCError, initTRPC } from '@trpc/server';
import * as trpcExpress from '@trpc/server/adapters/express';
import express from 'express';
import { z } from 'zod';

declare global {
namespace globalThis {
Expand All @@ -19,6 +15,11 @@ Sentry.init({
tracesSampleRate: 1,
});

import { TRPCError, initTRPC } from '@trpc/server';
import * as trpcExpress from '@trpc/server/adapters/express';
import express from 'express';
import { z } from 'zod';

const app = express();
const port = 3030;

Expand Down Expand Up @@ -52,6 +53,10 @@ app.get('/test-error', async function (req, res) {
res.send({ exceptionId });
});

app.get('/test-exception/:id', function (req, _res) {
throw new Error(`This is an exception with id ${req.params.id}`);
});

app.get('/test-local-variables-uncaught', function (req, res) {
const randomVariableToRecord = Math.random();
throw new Error(`Uncaught Local Variable Error - ${JSON.stringify({ randomVariableToRecord })}`);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { expect, test } from '@playwright/test';
import { waitForError } from '@sentry-internal/event-proxy-server';
import axios, { AxiosError, AxiosResponse } 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 = 90_000;

test('Sends exception to Sentry', async ({ baseURL }) => {
const { data } = await axios.get(`${baseURL}/test-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 correct error event', async ({ baseURL }) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test is new

const errorEventPromise = waitForError('node-express-app', event => {
return !event.type && event.exception?.values?.[0]?.value === 'This is an exception with id 123';
});

try {
await axios.get(`${baseURL}/test-exception/123`);
} catch {
// this results in an error, but we don't care - we want to check the error event
}

const errorEvent = await errorEventPromise;

expect(errorEvent.exception?.values).toHaveLength(1);
expect(errorEvent.exception?.values?.[0]?.value).toBe('This is an exception with id 123');

expect(errorEvent.request).toEqual({
method: 'GET',
cookies: {},
headers: expect.any(Object),
url: 'http://localhost:3030/test-exception/123',
});

expect(errorEvent.transaction).toEqual('GET /test-exception/:id');

expect(errorEvent.contexts?.trace).toEqual({
trace_id: expect.any(String),
span_id: expect.any(String),
});
});

test('Should record caught exceptions with local variable', async ({ baseURL }) => {
const { data } = await axios.get(`${baseURL}/test-local-variables-caught`);
const { exceptionId } = data;

const url = `https://sentry.io/api/0/projects/${sentryTestOrgSlug}/${sentryTestProject}/events/${exceptionId}/json/`;

console.log(`Polling for error eventId: ${exceptionId}`);

let response: AxiosResponse;

await expect
.poll(
async () => {
try {
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);

const frames = response!.data.exception.values[0].stacktrace.frames;

expect(frames[frames.length - 1].vars?.randomVariableToRecord).toBeDefined();
});

test('Should record uncaught exceptions with local variable', async ({ baseURL }) => {
const errorEventPromise = waitForError('node-express-app', errorEvent => {
return !!errorEvent?.exception?.values?.[0]?.value?.includes('Uncaught Local Variable Error');
});

await axios.get(`${baseURL}/test-local-variables-uncaught`).catch(() => {
// noop
});

const routehandlerError = await errorEventPromise;

const frames = routehandlerError!.exception!.values![0]!.stacktrace!.frames!;

expect(frames[frames.length - 1].vars?.randomVariableToRecord).toBeDefined();
});

This file was deleted.

Loading