|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | +import { waitForError, waitForTransaction } from '@sentry-internal/test-utils'; |
| 3 | + |
| 4 | +test('Sends exceptions to Sentry on error in @Cron decorated method', async ({ baseURL }) => { |
| 5 | + const errorEventPromise = waitForError('nestjs-basic', event => { |
| 6 | + return ( |
| 7 | + !event.type && |
| 8 | + event.exception?.values?.[0]?.value === 'Test error from schedule cron' && |
| 9 | + event.exception?.values?.[0]?.mechanism?.type === 'auto.function.nestjs.cron' |
| 10 | + ); |
| 11 | + }); |
| 12 | + |
| 13 | + const errorEvent = await errorEventPromise; |
| 14 | + |
| 15 | + expect(errorEvent.exception?.values).toHaveLength(1); |
| 16 | + expect(errorEvent.exception?.values?.[0]?.mechanism).toEqual({ |
| 17 | + handled: false, |
| 18 | + type: 'auto.function.nestjs.cron', |
| 19 | + }); |
| 20 | + |
| 21 | + // kill cron so tests don't get stuck |
| 22 | + await fetch(`${baseURL}/kill-test-schedule-cron/test-schedule-cron-error`); |
| 23 | +}); |
| 24 | + |
| 25 | +test('Sends exceptions to Sentry on error in @Interval decorated method', async ({ baseURL }) => { |
| 26 | + const errorEventPromise = waitForError('nestjs-basic', event => { |
| 27 | + return ( |
| 28 | + !event.type && |
| 29 | + event.exception?.values?.[0]?.value === 'Test error from schedule interval' && |
| 30 | + event.exception?.values?.[0]?.mechanism?.type === 'auto.function.nestjs.interval' |
| 31 | + ); |
| 32 | + }); |
| 33 | + |
| 34 | + const errorEvent = await errorEventPromise; |
| 35 | + |
| 36 | + expect(errorEvent.exception?.values).toHaveLength(1); |
| 37 | + expect(errorEvent.exception?.values?.[0]?.mechanism).toEqual({ |
| 38 | + handled: false, |
| 39 | + type: 'auto.function.nestjs.interval', |
| 40 | + }); |
| 41 | + |
| 42 | + // kill interval so tests don't get stuck |
| 43 | + await fetch(`${baseURL}/kill-test-schedule-interval/test-schedule-interval-error`); |
| 44 | +}); |
| 45 | + |
| 46 | +test('Sends exceptions to Sentry on error in @Timeout decorated method', async ({ baseURL }) => { |
| 47 | + const errorEventPromise = waitForError('nestjs-basic', event => { |
| 48 | + return ( |
| 49 | + !event.type && |
| 50 | + event.exception?.values?.[0]?.value === 'Test error from schedule timeout' && |
| 51 | + event.exception?.values?.[0]?.mechanism?.type === 'auto.function.nestjs.timeout' |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + // Trigger the @Timeout-decorated method via HTTP endpoint since @Timeout |
| 56 | + // fires once and timing is unreliable across test runs. |
| 57 | + await fetch(`${baseURL}/trigger-schedule-timeout-error`).catch(() => { |
| 58 | + // Expected to fail since the handler throws |
| 59 | + }); |
| 60 | + |
| 61 | + const errorEvent = await errorEventPromise; |
| 62 | + |
| 63 | + expect(errorEvent.exception?.values).toHaveLength(1); |
| 64 | + expect(errorEvent.exception?.values?.[0]?.mechanism).toEqual({ |
| 65 | + handled: false, |
| 66 | + type: 'auto.function.nestjs.timeout', |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +test('Scheduled task breadcrumbs do not leak into subsequent HTTP requests', async ({ baseURL }) => { |
| 71 | + // The app runs @Interval('test-schedule-isolation', 2000) which adds a breadcrumb. |
| 72 | + // Without isolation scope forking, this breadcrumb leaks into the default isolation scope |
| 73 | + // and gets cloned into subsequent HTTP requests. |
| 74 | + |
| 75 | + // Wait for at least one interval tick to fire |
| 76 | + await new Promise(resolve => setTimeout(resolve, 3000)); |
| 77 | + |
| 78 | + const transactionPromise = waitForTransaction('nestjs-basic', transactionEvent => { |
| 79 | + return transactionEvent.transaction === 'GET /test-schedule-isolation'; |
| 80 | + }); |
| 81 | + |
| 82 | + await fetch(`${baseURL}/test-schedule-isolation`); |
| 83 | + |
| 84 | + const transaction = await transactionPromise; |
| 85 | + |
| 86 | + const leakedBreadcrumb = (transaction.breadcrumbs || []).find( |
| 87 | + (b: any) => b.message === 'leaked-breadcrumb-from-schedule', |
| 88 | + ); |
| 89 | + expect(leakedBreadcrumb).toBeUndefined(); |
| 90 | + |
| 91 | + // kill interval so tests don't get stuck |
| 92 | + await fetch(`${baseURL}/kill-test-schedule-interval/test-schedule-isolation`); |
| 93 | +}); |
0 commit comments