Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Lms24 committed Sep 18, 2024
1 parent d558c1b commit 921d7f0
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { loggingTransport } = require('@sentry-internal/node-integration-tests');
const Sentry = require('@sentry/node');

Sentry.init({
dsn: 'https://[email protected]/1337',
transport: loggingTransport,
});

// express must be required after Sentry is initialized
const express = require('express');
const { startExpressServerAndSendPortToRunner } = require('@sentry-internal/node-integration-tests');

const app = express();

app.get('/test', (_req, res) => {
Sentry.captureException(new Error('This is a test error'));
Sentry.getClient().on('beforeEnvelope', envelope => {
const event = envelope[1][0][1];
if (event.exception.values[0].value === 'This is a test error') {
res.send({
traceData: Sentry.getTraceData(),
traceMetaTags: Sentry.getTraceMetaTags(),
errorTraceContext: event.contexts.trace,
});
}
});
});

Sentry.setupExpressErrorHandler(app);

startExpressServerAndSendPortToRunner(app);
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';

describe('errors in Tracing without Performance mode', () => {
afterAll(() => {
cleanupChildProcesses();
});

test('error has the same traceId as obtained via getTraceData()/getTraceMetaTags()', async () => {
const runner = createRunner(__dirname, 'server.js').start();

const response = await runner.makeRequest('get', '/test');

console.log('response: ', response);

const { traceData, traceMetaTags, errorTraceContext } = response as {
traceData: Record<string, string>;
traceMetaTags: string;
errorTraceContext: {
trace_id: string;
span_id: string;
};
};

const traceId = errorTraceContext?.trace_id;
const spanId = errorTraceContext?.span_id;

expect(traceId).toMatch(/^[a-f0-9]{32}$/);
expect(spanId).toMatch(/^[a-f0-9]{16}$/);

expect(errorTraceContext).toEqual({
trace_id: traceId,
span_id: spanId,
});

expect(traceData).toEqual({
'sentry-trace': `${traceId}-${spanId}`,
baggage: expect.stringContaining(`sentry-trace_id=${traceId}`),
});

expect(traceMetaTags).toContain(`content="${traceId}-${spanId}"/>\n`);
expect(traceMetaTags).toContain(`sentry-trace_id=${traceId}`);
});
});

0 comments on commit 921d7f0

Please sign in to comment.