Skip to content

test(node): Add captureMessage integration tests. #4785

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
Mar 25, 2022
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,6 +1,6 @@
import { assertSentryEvent, getEventRequest, runServer } from '../../../utils';
import { assertSentryEvent, getEventRequest, runServer } from '../../../../utils';

test('should send captureMessage', async () => {
test('should capture a simple message string', async () => {
const url = await runServer(__dirname);
const requestBody = await getEventRequest(url);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as Sentry from '@sentry/node';

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

Sentry.captureMessage('debug_message', Sentry.Severity.Debug);
Sentry.captureMessage('info_message', Sentry.Severity.Info);
Sentry.captureMessage('warning_message', Sentry.Severity.Warning);
Sentry.captureMessage('error_message', Sentry.Severity.Error);
Sentry.captureMessage('fatal_message', Sentry.Severity.Fatal);
Sentry.captureMessage('critical_message', Sentry.Severity.Critical);
Sentry.captureMessage('log_message', Sentry.Severity.Log);
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { assertSentryEvent, getMultipleEventRequests, runServer } from '../../../../utils';

test('should capture with different severity levels', async () => {
const url = await runServer(__dirname);
const events = await getMultipleEventRequests(url, 7);

assertSentryEvent(events[0], {
message: 'debug_message',
level: 'debug',
});

assertSentryEvent(events[1], {
message: 'info_message',
level: 'info',
});

assertSentryEvent(events[2], {
message: 'warning_message',
level: 'warning',
});

assertSentryEvent(events[3], {
message: 'error_message',
level: 'error',
});

assertSentryEvent(events[4], {
message: 'fatal_message',
level: 'fatal',
});

assertSentryEvent(events[5], {
message: 'critical_message',
level: 'critical',
});

assertSentryEvent(events[6], {
message: 'log_message',
level: 'log',
});
});
28 changes: 18 additions & 10 deletions packages/node-integration-tests/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import nock from 'nock';
import * as path from 'path';
import { getPortPromise } from 'portfinder';

const assertSentryEvent = (actual: Record<string, unknown>, expected: Record<string, unknown>): void => {
export const assertSentryEvent = (actual: Record<string, unknown>, expected: Record<string, unknown>): void => {
expect(actual).toMatchObject({
event_id: expect.any(String),
timestamp: expect.any(Number),
...expected,
});
};

const assertSentryTransaction = (actual: Record<string, unknown>, expected: Record<string, unknown>): void => {
export const assertSentryTransaction = (actual: Record<string, unknown>, expected: Record<string, unknown>): void => {
expect(actual).toMatchObject({
event_id: expect.any(String),
timestamp: expect.any(Number),
Expand All @@ -24,24 +24,34 @@ const assertSentryTransaction = (actual: Record<string, unknown>, expected: Reco
});
};

const parseEnvelope = (body: string): Array<Record<string, unknown>> => {
export const parseEnvelope = (body: string): Array<Record<string, unknown>> => {
return body.split('\n').map(e => JSON.parse(e));
};

const getEventRequest = async (url: string): Promise<Record<string, unknown>> => {
export const getMultipleEventRequests = async (url: string, count: number): Promise<Array<Record<string, unknown>>> => {
const events: Record<string, unknown>[] = [];

return new Promise(resolve => {
nock('https://dsn.ingest.sentry.io')
.post('/api/1337/store/', body => {
resolve(body);
events.push(body);

if (events.length === count) {
resolve(events);
}
return true;
})
.times(7)
.reply(200);

http.get(url);
});
};

const getEnvelopeRequest = async (url: string): Promise<Array<Record<string, unknown>>> => {
export const getEventRequest = async (url: string): Promise<Record<string, unknown>> => {
return (await getMultipleEventRequests(url, 1))[0];
};

export const getEnvelopeRequest = async (url: string): Promise<Array<Record<string, unknown>>> => {
return new Promise(resolve => {
nock('https://dsn.ingest.sentry.io')
.post('/api/1337/envelope/', body => {
Expand All @@ -55,7 +65,7 @@ const getEnvelopeRequest = async (url: string): Promise<Array<Record<string, unk
});
};

async function runServer(testDir: string, serverPath?: string, scenarioPath?: string): Promise<string> {
export async function runServer(testDir: string, serverPath?: string, scenarioPath?: string): Promise<string> {
const port = await getPortPromise();
const url = `http://localhost:${port}/test`;
const defaultServerPath = path.resolve(process.cwd(), 'utils', 'defaults', 'server');
Expand All @@ -77,5 +87,3 @@ async function runServer(testDir: string, serverPath?: string, scenarioPath?: st

return url;
}

export { assertSentryEvent, assertSentryTransaction, parseEnvelope, getEventRequest, getEnvelopeRequest, runServer };