|
| 1 | +import { expect } from '@playwright/test'; |
| 2 | +import type { Event } from '@sentry/types'; |
| 3 | + |
| 4 | +import { sentryTest } from '../../../utils/fixtures'; |
| 5 | +import { getMultipleSentryEnvelopeRequests } from '../../../utils/helpers'; |
| 6 | + |
| 7 | +sentryTest('it captures console messages correctly', async ({ getLocalTestUrl, page }) => { |
| 8 | + const url = await getLocalTestUrl({ testDir: __dirname }); |
| 9 | + |
| 10 | + await page.route('https://dsn.ingest.sentry.io/**/*', route => { |
| 11 | + return route.fulfill({ |
| 12 | + status: 200, |
| 13 | + contentType: 'application/json', |
| 14 | + body: JSON.stringify({ id: 'test-id' }), |
| 15 | + }); |
| 16 | + }); |
| 17 | + |
| 18 | + const [, events] = await Promise.all([page.goto(url), getMultipleSentryEnvelopeRequests<Event>(page, 7)]); |
| 19 | + |
| 20 | + expect(events).toHaveLength(7); |
| 21 | + |
| 22 | + const logEvent = events.find(event => event.message === 'console log'); |
| 23 | + const warnEvent = events.find(event => event.message === 'console warn'); |
| 24 | + const infoEvent = events.find(event => event.message === 'console info'); |
| 25 | + const errorEvent = events.find(event => event.message === 'console error'); |
| 26 | + const traceEvent = events.find(event => event.message === 'console trace'); |
| 27 | + const errorWithErrorEvent = events.find(event => !!event.exception); |
| 28 | + const traceWithErrorEvent = events.find(event => event.message === 'Error: console trace with error object'); |
| 29 | + |
| 30 | + expect(logEvent).toEqual( |
| 31 | + expect.objectContaining({ |
| 32 | + level: 'log', |
| 33 | + logger: 'console', |
| 34 | + extra: { |
| 35 | + arguments: ['console log'], |
| 36 | + }, |
| 37 | + }), |
| 38 | + ); |
| 39 | + expect(logEvent?.exception).toBeUndefined(); |
| 40 | + expect(warnEvent).toEqual( |
| 41 | + expect.objectContaining({ |
| 42 | + level: 'warning', |
| 43 | + logger: 'console', |
| 44 | + extra: { |
| 45 | + arguments: ['console warn'], |
| 46 | + }, |
| 47 | + }), |
| 48 | + ); |
| 49 | + expect(warnEvent?.exception).toBeUndefined(); |
| 50 | + expect(infoEvent).toEqual( |
| 51 | + expect.objectContaining({ |
| 52 | + level: 'info', |
| 53 | + logger: 'console', |
| 54 | + extra: { |
| 55 | + arguments: ['console info'], |
| 56 | + }, |
| 57 | + }), |
| 58 | + ); |
| 59 | + expect(infoEvent?.exception).toBeUndefined(); |
| 60 | + expect(errorEvent).toEqual( |
| 61 | + expect.objectContaining({ |
| 62 | + level: 'error', |
| 63 | + logger: 'console', |
| 64 | + extra: { |
| 65 | + arguments: ['console error'], |
| 66 | + }, |
| 67 | + }), |
| 68 | + ); |
| 69 | + expect(errorEvent?.exception).toBeUndefined(); |
| 70 | + expect(traceEvent).toEqual( |
| 71 | + expect.objectContaining({ |
| 72 | + level: 'log', |
| 73 | + logger: 'console', |
| 74 | + extra: { |
| 75 | + arguments: ['console trace'], |
| 76 | + }, |
| 77 | + }), |
| 78 | + ); |
| 79 | + expect(traceEvent?.exception).toBeUndefined(); |
| 80 | + expect(errorWithErrorEvent).toEqual( |
| 81 | + expect.objectContaining({ |
| 82 | + level: 'error', |
| 83 | + logger: 'console', |
| 84 | + extra: { |
| 85 | + arguments: [ |
| 86 | + { |
| 87 | + message: 'console error with error object', |
| 88 | + name: 'Error', |
| 89 | + stack: expect.any(String), |
| 90 | + }, |
| 91 | + ], |
| 92 | + }, |
| 93 | + exception: expect.any(Object), |
| 94 | + }), |
| 95 | + ); |
| 96 | + expect(errorWithErrorEvent?.exception?.values?.[0].value).toBe('console error with error object'); |
| 97 | + expect(traceWithErrorEvent).toEqual( |
| 98 | + expect.objectContaining({ |
| 99 | + level: 'log', |
| 100 | + logger: 'console', |
| 101 | + extra: { |
| 102 | + arguments: [ |
| 103 | + { |
| 104 | + message: 'console trace with error object', |
| 105 | + name: 'Error', |
| 106 | + stack: expect.any(String), |
| 107 | + }, |
| 108 | + ], |
| 109 | + }, |
| 110 | + }), |
| 111 | + ); |
| 112 | + expect(traceWithErrorEvent?.exception).toBeUndefined(); |
| 113 | +}); |
0 commit comments