Skip to content

Commit

Permalink
test(browser/v7): Add test for captureConsole (#11830)
Browse files Browse the repository at this point in the history
Adding tests for
#11829 on v7.
  • Loading branch information
mydea authored Apr 30, 2024
1 parent 4340845 commit 217e4c1
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as Sentry from '@sentry/browser';
import { captureConsoleIntegration } from '@sentry/integrations';

window.Sentry = Sentry;

Sentry.init({
dsn: 'https://[email protected]/1337',
integrations: [captureConsoleIntegration()],
autoSessionTracking: false,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
console.log('console log');
console.warn('console warn');
console.error('console error');
console.info('console info');
console.trace('console trace');

console.error(new Error('console error with error object'));
console.trace(new Error('console trace with error object'));
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { expect } from '@playwright/test';
import type { Event } from '@sentry/types';

import { sentryTest } from '../../../utils/fixtures';
import { getMultipleSentryEnvelopeRequests } from '../../../utils/helpers';

sentryTest('it captures console messages correctly', async ({ getLocalTestUrl, page }) => {
const url = await getLocalTestUrl({ testDir: __dirname });

await page.route('https://dsn.ingest.sentry.io/**/*', route => {
return route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ id: 'test-id' }),
});
});

const [, events] = await Promise.all([page.goto(url), getMultipleSentryEnvelopeRequests<Event>(page, 7)]);

expect(events).toHaveLength(7);

const logEvent = events.find(event => event.message === 'console log');
const warnEvent = events.find(event => event.message === 'console warn');
const infoEvent = events.find(event => event.message === 'console info');
const errorEvent = events.find(event => event.message === 'console error');
const traceEvent = events.find(event => event.message === 'console trace');
const errorWithErrorEvent = events.find(event => !!event.exception);
const traceWithErrorEvent = events.find(event => event.message === 'Error: console trace with error object');

expect(logEvent).toEqual(
expect.objectContaining({
level: 'log',
logger: 'console',
extra: {
arguments: ['console log'],
},
}),
);
expect(logEvent?.exception).toBeUndefined();
expect(warnEvent).toEqual(
expect.objectContaining({
level: 'warning',
logger: 'console',
extra: {
arguments: ['console warn'],
},
}),
);
expect(warnEvent?.exception).toBeUndefined();
expect(infoEvent).toEqual(
expect.objectContaining({
level: 'info',
logger: 'console',
extra: {
arguments: ['console info'],
},
}),
);
expect(infoEvent?.exception).toBeUndefined();
expect(errorEvent).toEqual(
expect.objectContaining({
level: 'error',
logger: 'console',
extra: {
arguments: ['console error'],
},
}),
);
expect(errorEvent?.exception).toBeUndefined();
expect(traceEvent).toEqual(
expect.objectContaining({
level: 'log',
logger: 'console',
extra: {
arguments: ['console trace'],
},
}),
);
expect(traceEvent?.exception).toBeUndefined();
expect(errorWithErrorEvent).toEqual(
expect.objectContaining({
level: 'error',
logger: 'console',
extra: {
arguments: [
{
message: 'console error with error object',
name: 'Error',
stack: expect.any(String),
},
],
},
exception: expect.any(Object),
}),
);
expect(errorWithErrorEvent?.exception?.values?.[0].value).toBe('console error with error object');
expect(traceWithErrorEvent).toEqual(
expect.objectContaining({
level: 'log',
logger: 'console',
extra: {
arguments: [
{
message: 'console trace with error object',
name: 'Error',
stack: expect.any(String),
},
],
},
}),
);
expect(traceWithErrorEvent?.exception).toBeUndefined();
});

0 comments on commit 217e4c1

Please sign in to comment.