-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(browser): Port unhandledrejection tests to playwright (#11758)
ref #11084 This test ports `packages/browser/test/integration/suites/onunhandledrejection.js` playwright. Because of the same limitations as outlined with the on error tests #11666, I had to use calls to `window.onunhandledrejection` to simulate these tests instead of just using `Promise.reject` to test the handler. #11678 tracks being able to fix this so we can avoid directly calling `window.onunhandledrejection` to test. As `onunhandledrejection.js` was the last suite to use the old integration tests, I fully removed that code and the corresponding GH action workflow. I also removed the monorepo deps on `karma`, `chai` and `sinon`. Extremely satisfying.
- Loading branch information
1 parent
de98428
commit 3eea537
Showing
50 changed files
with
458 additions
and
4,892 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...tion-tests/suites/public-api/instrumentation/onUnhandledRejection/custom-event/subject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
function run() { | ||
// this isn't how it happens in real life, in that the promise and reason | ||
// values come from an actual PromiseRejectionEvent, but it's enough to test | ||
// how the SDK handles the structure | ||
window.dispatchEvent( | ||
new CustomEvent('unhandledrejection', { | ||
detail: { | ||
promise: new Promise(function () {}), | ||
// we're testing with an error here but it could be anything - really | ||
// all we're testing is that it gets dug out correctly | ||
reason: new Error('promiseError'), | ||
}, | ||
}), | ||
); | ||
} | ||
|
||
run(); |
32 changes: 32 additions & 0 deletions
32
...gration-tests/suites/public-api/instrumentation/onUnhandledRejection/custom-event/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { expect } from '@playwright/test'; | ||
import type { Event } from '@sentry/types'; | ||
|
||
import { sentryTest } from '../../../../../utils/fixtures'; | ||
import { getFirstSentryEnvelopeRequest } from '../../../../../utils/helpers'; | ||
|
||
// something, somewhere, (likely a browser extension) effectively casts PromiseRejectionEvents | ||
// to CustomEvents, moving the `promise` and `reason` attributes of the PRE into | ||
// the CustomEvent's `detail` attribute, since they're not part of CustomEvent's spec | ||
// see https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent and | ||
// https://github.com/getsentry/sentry-javascript/issues/2380 | ||
sentryTest( | ||
'should capture PromiseRejectionEvent cast to CustomEvent with type unhandledrejection', | ||
async ({ getLocalTestPath, page }) => { | ||
const url = await getLocalTestPath({ testDir: __dirname }); | ||
|
||
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url); | ||
|
||
expect(eventData.exception?.values).toHaveLength(1); | ||
expect(eventData.exception?.values?.[0]).toMatchObject({ | ||
type: 'Error', | ||
value: 'promiseError', | ||
mechanism: { | ||
type: 'onunhandledrejection', | ||
handled: false, | ||
}, | ||
stacktrace: { | ||
frames: expect.any(Array), | ||
}, | ||
}); | ||
}, | ||
); |
5 changes: 5 additions & 0 deletions
5
...integration-tests/suites/public-api/instrumentation/onUnhandledRejection/event/subject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function run() { | ||
window.dispatchEvent(new Event('unhandledrejection')); | ||
} | ||
|
||
run(); |
23 changes: 23 additions & 0 deletions
23
...er-integration-tests/suites/public-api/instrumentation/onUnhandledRejection/event/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { expect } from '@playwright/test'; | ||
import type { Event } from '@sentry/types'; | ||
|
||
import { sentryTest } from '../../../../../utils/fixtures'; | ||
import { getFirstSentryEnvelopeRequest } from '../../../../../utils/helpers'; | ||
|
||
// there's no evidence that this actually happens, but it could, and our code correctly | ||
// handles it, so might as well prevent future regression on that score | ||
sentryTest('should capture a random Event with type unhandledrejection', async ({ getLocalTestPath, page }) => { | ||
const url = await getLocalTestPath({ testDir: __dirname }); | ||
|
||
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url); | ||
|
||
expect(eventData.exception?.values).toHaveLength(1); | ||
expect(eventData.exception?.values?.[0]).toMatchObject({ | ||
type: 'Event', | ||
value: 'Event `Event` (type=unhandledrejection) captured as promise rejection', | ||
mechanism: { | ||
type: 'onunhandledrejection', | ||
handled: false, | ||
}, | ||
}); | ||
}); |
7 changes: 7 additions & 0 deletions
7
.../browser-integration-tests/suites/public-api/instrumentation/onUnhandledRejection/init.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
window.Sentry = Sentry; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
}); |
9 changes: 9 additions & 0 deletions
9
...ion-tests/suites/public-api/instrumentation/onUnhandledRejection/thrown-errors/subject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function run() { | ||
const reason = new Error('promiseError'); | ||
const promise = Promise.reject(reason); | ||
const event = new PromiseRejectionEvent('unhandledrejection', { promise, reason }); | ||
// simulate window.onunhandledrejection without generating a Script error | ||
window.onunhandledrejection(event); | ||
} | ||
|
||
run(); |
24 changes: 24 additions & 0 deletions
24
...ration-tests/suites/public-api/instrumentation/onUnhandledRejection/thrown-errors/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { expect } from '@playwright/test'; | ||
import type { Event } from '@sentry/types'; | ||
|
||
import { sentryTest } from '../../../../../utils/fixtures'; | ||
import { getFirstSentryEnvelopeRequest } from '../../../../../utils/helpers'; | ||
|
||
sentryTest('should catch thrown errors', async ({ getLocalTestPath, page }) => { | ||
const url = await getLocalTestPath({ testDir: __dirname }); | ||
|
||
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url); | ||
|
||
expect(eventData.exception?.values).toHaveLength(1); | ||
expect(eventData.exception?.values?.[0]).toMatchObject({ | ||
type: 'Error', | ||
value: 'promiseError', | ||
mechanism: { | ||
type: 'onunhandledrejection', | ||
handled: false, | ||
}, | ||
stacktrace: { | ||
frames: expect.any(Array), | ||
}, | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
...ation-tests/suites/public-api/instrumentation/onUnhandledRejection/thrown-null/subject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function run() { | ||
const reason = null; | ||
const promise = Promise.reject(reason); | ||
const event = new PromiseRejectionEvent('unhandledrejection', { promise, reason }); | ||
// simulate window.onunhandledrejection without generating a Script error | ||
window.onunhandledrejection(event); | ||
} | ||
|
||
run(); |
21 changes: 21 additions & 0 deletions
21
...egration-tests/suites/public-api/instrumentation/onUnhandledRejection/thrown-null/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { expect } from '@playwright/test'; | ||
import type { Event } from '@sentry/types'; | ||
|
||
import { sentryTest } from '../../../../../utils/fixtures'; | ||
import { getFirstSentryEnvelopeRequest } from '../../../../../utils/helpers'; | ||
|
||
sentryTest('should catch thrown strings', async ({ getLocalTestPath, page }) => { | ||
const url = await getLocalTestPath({ testDir: __dirname }); | ||
|
||
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url); | ||
|
||
expect(eventData.exception?.values).toHaveLength(1); | ||
expect(eventData.exception?.values?.[0]).toMatchObject({ | ||
type: 'UnhandledRejection', | ||
value: 'Non-Error promise rejection captured with value: null', | ||
mechanism: { | ||
type: 'onunhandledrejection', | ||
handled: false, | ||
}, | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
...ion-tests/suites/public-api/instrumentation/onUnhandledRejection/thrown-number/subject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function run() { | ||
const reason = 123; | ||
const promise = Promise.reject(reason); | ||
const event = new PromiseRejectionEvent('unhandledrejection', { promise, reason }); | ||
// simulate window.onunhandledrejection without generating a Script error | ||
window.onunhandledrejection(event); | ||
} | ||
|
||
run(); |
21 changes: 21 additions & 0 deletions
21
...ration-tests/suites/public-api/instrumentation/onUnhandledRejection/thrown-number/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { expect } from '@playwright/test'; | ||
import type { Event } from '@sentry/types'; | ||
|
||
import { sentryTest } from '../../../../../utils/fixtures'; | ||
import { getFirstSentryEnvelopeRequest } from '../../../../../utils/helpers'; | ||
|
||
sentryTest('should catch thrown strings', async ({ getLocalTestPath, page }) => { | ||
const url = await getLocalTestPath({ testDir: __dirname }); | ||
|
||
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url); | ||
|
||
expect(eventData.exception?.values).toHaveLength(1); | ||
expect(eventData.exception?.values?.[0]).toMatchObject({ | ||
type: 'UnhandledRejection', | ||
value: 'Non-Error promise rejection captured with value: 123', | ||
mechanism: { | ||
type: 'onunhandledrejection', | ||
handled: false, | ||
}, | ||
}); | ||
}); |
15 changes: 15 additions & 0 deletions
15
...s/suites/public-api/instrumentation/onUnhandledRejection/thrown-object-complex/subject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function run() { | ||
const reason = { | ||
a: '1'.repeat('100'), | ||
b: '2'.repeat('100'), | ||
c: '3'.repeat('100'), | ||
}; | ||
reason.d = reason.a; | ||
reason.e = reason; | ||
const promise = Promise.reject(reason); | ||
const event = new PromiseRejectionEvent('unhandledrejection', { promise, reason }); | ||
// simulate window.onunhandledrejection without generating a Script error | ||
window.onunhandledrejection(event); | ||
} | ||
|
||
run(); |
21 changes: 21 additions & 0 deletions
21
...ests/suites/public-api/instrumentation/onUnhandledRejection/thrown-object-complex/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { expect } from '@playwright/test'; | ||
import type { Event } from '@sentry/types'; | ||
|
||
import { sentryTest } from '../../../../../utils/fixtures'; | ||
import { getFirstSentryEnvelopeRequest } from '../../../../../utils/helpers'; | ||
|
||
sentryTest('should capture unhandledrejection with a complex object', async ({ getLocalTestPath, page }) => { | ||
const url = await getLocalTestPath({ testDir: __dirname }); | ||
|
||
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url); | ||
|
||
expect(eventData.exception?.values).toHaveLength(1); | ||
expect(eventData.exception?.values?.[0]).toMatchObject({ | ||
type: 'UnhandledRejection', | ||
value: 'Object captured as promise rejection with keys: a, b, c, d, e', | ||
mechanism: { | ||
type: 'onunhandledrejection', | ||
handled: false, | ||
}, | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
...on-tests/suites/public-api/instrumentation/onUnhandledRejection/thrown-objects/subject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function run() { | ||
const reason = { a: 'b', b: 'c', c: 'd' }; | ||
const promise = Promise.reject(reason); | ||
const event = new PromiseRejectionEvent('unhandledrejection', { promise, reason }); | ||
// simulate window.onunhandledrejection without generating a Script error | ||
window.onunhandledrejection(event); | ||
} | ||
|
||
run(); |
21 changes: 21 additions & 0 deletions
21
...ation-tests/suites/public-api/instrumentation/onUnhandledRejection/thrown-objects/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { expect } from '@playwright/test'; | ||
import type { Event } from '@sentry/types'; | ||
|
||
import { sentryTest } from '../../../../../utils/fixtures'; | ||
import { getFirstSentryEnvelopeRequest } from '../../../../../utils/helpers'; | ||
|
||
sentryTest('should capture unhandledrejection with an object', async ({ getLocalTestPath, page }) => { | ||
const url = await getLocalTestPath({ testDir: __dirname }); | ||
|
||
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url); | ||
|
||
expect(eventData.exception?.values).toHaveLength(1); | ||
expect(eventData.exception?.values?.[0]).toMatchObject({ | ||
type: 'UnhandledRejection', | ||
value: 'Object captured as promise rejection with keys: a, b, c', | ||
mechanism: { | ||
type: 'onunhandledrejection', | ||
handled: false, | ||
}, | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
...sts/suites/public-api/instrumentation/onUnhandledRejection/thrown-string-large/subject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function run() { | ||
const reason = 'stringError'.repeat(100); | ||
const promise = Promise.reject(reason); | ||
const event = new PromiseRejectionEvent('unhandledrejection', { promise, reason }); | ||
// simulate window.onunhandledrejection without generating a Script error | ||
window.onunhandledrejection(event); | ||
} | ||
|
||
run(); |
22 changes: 22 additions & 0 deletions
22
...-tests/suites/public-api/instrumentation/onUnhandledRejection/thrown-string-large/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { expect } from '@playwright/test'; | ||
import type { Event } from '@sentry/types'; | ||
|
||
import { sentryTest } from '../../../../../utils/fixtures'; | ||
import { getFirstSentryEnvelopeRequest } from '../../../../../utils/helpers'; | ||
|
||
sentryTest('should capture unhandledrejection with a large string', async ({ getLocalTestPath, page }) => { | ||
const url = await getLocalTestPath({ testDir: __dirname }); | ||
|
||
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url); | ||
|
||
expect(eventData.exception?.values).toHaveLength(1); | ||
expect(eventData.exception?.values?.[0]).toMatchObject({ | ||
type: 'UnhandledRejection', | ||
value: | ||
'Non-Error promise rejection captured with value: stringErrorstringErrorstringErrorstringErrorstringErrorstringErrorstringErrorstringErrorstringErrorstringErrorstringErrorstringErrorstringErrorstringErrorstringErrorstringErrorstringErrorstringErrorstr...', | ||
mechanism: { | ||
type: 'onunhandledrejection', | ||
handled: false, | ||
}, | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
...on-tests/suites/public-api/instrumentation/onUnhandledRejection/thrown-strings/subject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function run() { | ||
const reason = 'stringError'; | ||
const promise = Promise.reject(reason); | ||
const event = new PromiseRejectionEvent('unhandledrejection', { promise, reason }); | ||
// simulate window.onunhandledrejection without generating a Script error | ||
window.onunhandledrejection(event); | ||
} | ||
|
||
run(); |
21 changes: 21 additions & 0 deletions
21
...ation-tests/suites/public-api/instrumentation/onUnhandledRejection/thrown-strings/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { expect } from '@playwright/test'; | ||
import type { Event } from '@sentry/types'; | ||
|
||
import { sentryTest } from '../../../../../utils/fixtures'; | ||
import { getFirstSentryEnvelopeRequest } from '../../../../../utils/helpers'; | ||
|
||
sentryTest('should catch thrown strings', async ({ getLocalTestPath, page }) => { | ||
const url = await getLocalTestPath({ testDir: __dirname }); | ||
|
||
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url); | ||
|
||
expect(eventData.exception?.values).toHaveLength(1); | ||
expect(eventData.exception?.values?.[0]).toMatchObject({ | ||
type: 'UnhandledRejection', | ||
value: 'Non-Error promise rejection captured with value: stringError', | ||
mechanism: { | ||
type: 'onunhandledrejection', | ||
handled: false, | ||
}, | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
...-tests/suites/public-api/instrumentation/onUnhandledRejection/thrown-undefined/subject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function run() { | ||
const reason = undefined; | ||
const promise = Promise.reject(reason); | ||
const event = new PromiseRejectionEvent('unhandledrejection', { promise, reason }); | ||
// simulate window.onunhandledrejection without generating a Script error | ||
window.onunhandledrejection(event); | ||
} | ||
|
||
run(); |
Oops, something went wrong.