Skip to content
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

test: Improve E2E node tests #12258

Merged
merged 1 commit into from
May 28, 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
Expand Up @@ -7,7 +7,8 @@ const sentryTestProject = process.env.E2E_TEST_SENTRY_TEST_PROJECT;
const EVENT_POLLING_TIMEOUT = 90_000;

test('Sends a server-side exception to Sentry', async ({ baseURL }) => {
const { data } = await axios.get(`${baseURL}/api/error`);
const response = await fetch(`${baseURL}/api/error`);
const data = await response.json();
const { exceptionId } = data;

const url = `https://sentry.io/api/0/projects/${sentryTestOrgSlug}/${sentryTestProject}/events/${exceptionId}/`;
Expand Down Expand Up @@ -39,7 +40,8 @@ test('Sends a server-side exception to Sentry', async ({ baseURL }) => {
});

test('Sends server-side transactions to Sentry', async ({ baseURL }) => {
const { data } = await axios.get(`${baseURL}/api/success`);
const response = await fetch(`${baseURL}/api/success`);
const data = await response.json();
const { transactionIds } = data;

console.log(`Polling for transaction eventIds: ${JSON.stringify(transactionIds)}`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,12 @@
import { expect, test } from '@playwright/test';
import { waitForError } from '@sentry-internal/event-proxy-server';
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 = 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 }) => {
const errorEventPromise = waitForError('node-connect', event => {
return !event.type && event.exception?.values?.[0]?.value === 'This is an exception';
});

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

const errorEvent = await errorEventPromise;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { expect, test } from '@playwright/test';
import { waitForTransaction } from '@sentry-internal/event-proxy-server';
import axios, { AxiosError } from 'axios';

const authToken = process.env.E2E_TEST_AUTH_TOKEN;
const sentryTestOrgSlug = process.env.E2E_TEST_SENTRY_ORG_SLUG;
Expand All @@ -15,7 +14,7 @@ test('Sends an API route transaction', async ({ baseURL }) => {
);
});

await axios.get(`${baseURL}/test-transaction`);
await fetch(`${baseURL}/test-transaction`);

const transactionEvent = await pageloadTransactionEventPromise;
const transactionEventId = transactionEvent.event_id;
Expand All @@ -35,7 +34,7 @@ test('Sends an API route transaction', async ({ baseURL }) => {
'http.method': 'GET',
'http.scheme': 'http',
'http.target': '/test-transaction',
'http.user_agent': 'axios/1.6.7',
'http.user_agent': 'node',
'http.flavor': '1.1',
'net.transport': 'ip_tcp',
'net.host.ip': expect.any(String),
Expand Down Expand Up @@ -97,32 +96,4 @@ test('Sends an API route transaction', async ({ baseURL }) => {
},
}),
);

await expect
.poll(
async () => {
try {
const response = await axios.get(
`https://sentry.io/api/0/projects/${sentryTestOrgSlug}/${sentryTestProject}/events/${transactionEventId}/`,
{ 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);
});
Original file line number Diff line number Diff line change
@@ -1,54 +1,12 @@
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 }) => {
const errorEventPromise = waitForError('node-express', 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
}
await fetch(`${baseURL}/test-exception/123`);

const errorEvent = await errorEventPromise;

Expand All @@ -71,55 +29,14 @@ test('Sends correct error event', async ({ baseURL }) => {
});

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', errorEvent => {
return !!errorEvent?.exception?.values?.[0]?.value?.includes('Uncaught Local Variable Error');
});

await axios.get(`${baseURL}/test-local-variables-uncaught`).catch(() => {
// noop
const errorEventPromise = waitForError('node-express', event => {
return event.transaction === 'GET /test-local-variables-caught';
});

const routehandlerError = await errorEventPromise;
await fetch(`${baseURL}/test-local-variables-caught`);

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

expect(frames[frames.length - 1].vars?.randomVariableToRecord).toBeDefined();
const frames = errorEvent.exception?.values?.[0].stacktrace?.frames;
expect(frames?.[frames.length - 1].vars?.randomVariableToRecord).toBeDefined();
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { expect, test } from '@playwright/test';
import { waitForTransaction } from '@sentry-internal/event-proxy-server';
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 = 90_000;

test('Sends an API route transaction', async ({ baseURL }) => {
const pageloadTransactionEventPromise = waitForTransaction('node-express', transactionEvent => {
Expand All @@ -15,7 +9,7 @@ test('Sends an API route transaction', async ({ baseURL }) => {
);
});

await axios.get(`${baseURL}/test-transaction`);
await fetch(`${baseURL}/test-transaction`);

const transactionEvent = await pageloadTransactionEventPromise;
const transactionEventId = transactionEvent.event_id;
Expand All @@ -35,7 +29,7 @@ test('Sends an API route transaction', async ({ baseURL }) => {
'http.method': 'GET',
'http.scheme': 'http',
'http.target': '/test-transaction',
'http.user_agent': 'axios/1.6.7',
'http.user_agent': 'node',
'http.flavor': '1.1',
'net.transport': 'ip_tcp',
'net.host.ip': expect.any(String),
Expand Down Expand Up @@ -124,32 +118,4 @@ test('Sends an API route transaction', async ({ baseURL }) => {
timestamp: expect.any(Number),
trace_id: expect.any(String),
});

await expect
.poll(
async () => {
try {
const response = await axios.get(
`https://sentry.io/api/0/projects/${sentryTestOrgSlug}/${sentryTestProject}/events/${transactionEventId}/`,
{ 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);
});
13 changes: 13 additions & 0 deletions dev-packages/e2e-tests/test-applications/node-fastify/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import type * as S from '@sentry/node';
const Sentry = require('@sentry/node') as typeof S;

// We wrap console.warn to find out if a warning is incorrectly logged
console.warn = new Proxy(console.warn, {
apply: function (target, thisArg, argumentsList) {
const msg = argumentsList[0];
if (typeof msg === 'string' && msg.startsWith('[Sentry]')) {
console.error(`Sentry warning was triggered: ${msg}`);
process.exit(1);
}

return target.apply(thisArg, argumentsList);
},
});

Sentry.init({
environment: 'qa', // dynamic sampling bias to keep transactions
dsn: process.env.E2E_TEST_DSN,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,12 @@
import { expect, test } from '@playwright/test';
import { waitForError } from '@sentry-internal/event-proxy-server';
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 = 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 }) => {
const errorEventPromise = waitForError('node-fastify', 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
}
await fetch(`${baseURL}/test-exception/123`);

const errorEvent = await errorEventPromise;

Expand Down
Loading
Loading