|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | +import { waitForError, waitForTransaction } from '@sentry-internal/test-utils'; |
| 3 | + |
| 4 | +test('Sends exception to Sentry', async ({ baseURL }) => { |
| 5 | + const errorEventPromise = waitForError('nestjs-basic-with-graphql', event => { |
| 6 | + return !event.type && event.exception?.values?.[0]?.value === 'This is an exception with id 123'; |
| 7 | + }); |
| 8 | + |
| 9 | + const response = await fetch(`${baseURL}/test-exception/123`); |
| 10 | + expect(response.status).toBe(500); |
| 11 | + |
| 12 | + const errorEvent = await errorEventPromise; |
| 13 | + |
| 14 | + expect(errorEvent.exception?.values).toHaveLength(1); |
| 15 | + expect(errorEvent.exception?.values?.[0]?.value).toBe('This is an exception with id 123'); |
| 16 | + |
| 17 | + expect(errorEvent.request).toEqual({ |
| 18 | + method: 'GET', |
| 19 | + cookies: {}, |
| 20 | + headers: expect.any(Object), |
| 21 | + url: 'http://localhost:3030/test-exception/123', |
| 22 | + }); |
| 23 | + |
| 24 | + expect(errorEvent.transaction).toEqual('GET /test-exception/:id'); |
| 25 | + |
| 26 | + expect(errorEvent.contexts?.trace).toEqual({ |
| 27 | + trace_id: expect.any(String), |
| 28 | + span_id: expect.any(String), |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +test('Does not send HttpExceptions to Sentry', async ({ baseURL }) => { |
| 33 | + let errorEventOccurred = false; |
| 34 | + |
| 35 | + waitForError('nestjs-basic-with-graphql', event => { |
| 36 | + if (!event.type && event.exception?.values?.[0]?.value === 'This is an expected 400 exception with id 123') { |
| 37 | + errorEventOccurred = true; |
| 38 | + } |
| 39 | + |
| 40 | + return event?.transaction === 'GET /test-expected-400-exception/:id'; |
| 41 | + }); |
| 42 | + |
| 43 | + waitForError('nestjs-basic-with-graphql', event => { |
| 44 | + if (!event.type && event.exception?.values?.[0]?.value === 'This is an expected 500 exception with id 123') { |
| 45 | + errorEventOccurred = true; |
| 46 | + } |
| 47 | + |
| 48 | + return event?.transaction === 'GET /test-expected-500-exception/:id'; |
| 49 | + }); |
| 50 | + |
| 51 | + const transactionEventPromise400 = waitForTransaction('nestjs-basic-with-graphql', transactionEvent => { |
| 52 | + return transactionEvent?.transaction === 'GET /test-expected-400-exception/:id'; |
| 53 | + }); |
| 54 | + |
| 55 | + const transactionEventPromise500 = waitForTransaction('nestjs-basic-with-graphql', transactionEvent => { |
| 56 | + return transactionEvent?.transaction === 'GET /test-expected-500-exception/:id'; |
| 57 | + }); |
| 58 | + |
| 59 | + const response400 = await fetch(`${baseURL}/test-expected-400-exception/123`); |
| 60 | + expect(response400.status).toBe(400); |
| 61 | + |
| 62 | + const response500 = await fetch(`${baseURL}/test-expected-500-exception/123`); |
| 63 | + expect(response500.status).toBe(500); |
| 64 | + |
| 65 | + await transactionEventPromise400; |
| 66 | + await transactionEventPromise500; |
| 67 | + |
| 68 | + (await fetch(`${baseURL}/flush`)).text(); |
| 69 | + |
| 70 | + expect(errorEventOccurred).toBe(false); |
| 71 | +}); |
| 72 | + |
| 73 | +test('Sends graphql exception to Sentry', async ({ baseURL }) => { |
| 74 | + const errorEventPromise = waitForError('nestjs-basic-with-graphql', event => { |
| 75 | + return !event.type && event.exception?.values?.[0]?.value === 'This is an exception!'; |
| 76 | + }); |
| 77 | + |
| 78 | + const response = await fetch(`${baseURL}/graphql`, { |
| 79 | + method: 'POST', |
| 80 | + headers: { |
| 81 | + 'Content-Type': 'application/json', |
| 82 | + }, |
| 83 | + body: JSON.stringify({ |
| 84 | + query: `query { error }`, |
| 85 | + }), |
| 86 | + }); |
| 87 | + |
| 88 | + const json_response = await response.json(); |
| 89 | + const errorEvent = await errorEventPromise; |
| 90 | + |
| 91 | + expect(json_response?.errors[0]).toEqual({ |
| 92 | + message: 'This is an exception!', |
| 93 | + locations: expect.any(Array), |
| 94 | + path: ['error'], |
| 95 | + extensions: { |
| 96 | + code: 'INTERNAL_SERVER_ERROR', |
| 97 | + stacktrace: expect.any(Array), |
| 98 | + }, |
| 99 | + }); |
| 100 | + |
| 101 | + expect(errorEvent.exception?.values).toHaveLength(1); |
| 102 | + expect(errorEvent.exception?.values?.[0]?.value).toBe('This is an exception!'); |
| 103 | + |
| 104 | + expect(errorEvent.request).toEqual({ |
| 105 | + method: 'POST', |
| 106 | + cookies: {}, |
| 107 | + data: '{"query":"query { error }"}', |
| 108 | + headers: expect.any(Object), |
| 109 | + url: 'http://localhost:3030/graphql', |
| 110 | + }); |
| 111 | + |
| 112 | + expect(errorEvent.transaction).toEqual('POST /graphql'); |
| 113 | + |
| 114 | + expect(errorEvent.contexts?.trace).toEqual({ |
| 115 | + trace_id: expect.any(String), |
| 116 | + span_id: expect.any(String), |
| 117 | + }); |
| 118 | +}); |
0 commit comments