Skip to content

test(browser): Add e2e tests for the @sentry/browser package #13125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 13, 2024

Conversation

Zen-cronic
Copy link
Contributor

Addresses #12662

Added e2e tests for @sentry/browser in dev-packages/e2e-tests/test-applications named default-browser.

  • If you've added code that should be tested, please add tests.
  • Ensure your code lints and the test suite passes (yarn lint) & (yarn test).

@Zen-cronic
Copy link
Contributor Author

The tests for transaction events will be added next.

@Zen-cronic Zen-cronic marked this pull request as draft July 31, 2024 01:04
@Zen-cronic Zen-cronic marked this pull request as ready for review July 31, 2024 01:08
@Zen-cronic Zen-cronic marked this pull request as draft July 31, 2024 01:08
Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Zen-cronic thanks a lot for opening this PR!

Let us know when this is ready to review or if you have questions along the way. I gave your current draft a quick review and have two suggestions:

  1. to add this test to CI, we need to add an entry for the test app in the .github/build.yml matrix
  2. Ideally, we not only test errors but also spans, specifically pageload and navigation spans. You already added browserTracingIntegration to your Sentry.init call, so we should get these events as well. I see you're already perfectly using our usual test pattern so you can take some other performance test files as example.

Great work!

@Lms24 Lms24 self-assigned this Aug 1, 2024
@Lms24
Copy link
Member

Lms24 commented Aug 1, 2024

just FYI: I assigned myself to this PR for internal tracking purposes since I'm gonna review it. So no worries, I'm not taking over or anything. Please continue as before :)

@Zen-cronic
Copy link
Contributor Author

  1. to add this test to CI, we need to add an entry for the test app in the .github/build.yml matrix
  2. Ideally, we not only test errors but also spans, specifically pageload and navigation spans. You already added browserTracingIntegration to your Sentry.init call, so we should get these events as well. I see you're already perfectly using our usual test pattern so you can take some other performance test files as example.

Thanks for the heads up!

I ran into this issue though:

  • Testing errors with waitForError works just fine; the error event type is emitted and the test succeeds.

  • However, using waitForTransaction to check for spans and transactions seem to hang. I debugged it and compare the results to other successful test suites (like react-router-6), and it seems that the transaction or span event types are not emitted.

There could be something lacking in my test setup, but waitForError works with the current setup.

test('Should set correct transaction', async ({ page }) => {

 // `pageload` transaction
  const transactionPromise = waitForTransaction('default-browser', async transactionEvent => {
    return !!transactionEvent?.transaction && transactionEvent.contexts?.trace?.op === 'pageload';
  });

  await page.goto(`/`);

  // `waitForTransaction` hangs here because `transaction` (or `span`) event is not emitted,
  // so the code past this line is never executed.
  const pageLoadTransaction = await transactionPromise;

  // ...assertions
});

@Lms24
Copy link
Member

Lms24 commented Aug 2, 2024

@Zen-cronic You can run the pnpm test:assert --ui to run the test in PlayWright's UI mode. This will let you check if reqeusts from the SDK were sent like in browser dev tools. Also, if you enable debug: true in your Sentry.init call, you'll get debug logs that might hint if a span is dropped for some reason.

Also it's helpful to log whatever comes into your waitForTransaction callback, the check looks fine to me on first glance but maybe something is missing

@@ -0,0 +1,12 @@
import * as Sentry from '@sentry/react';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be @sentry/browser instead.

@Lms24
Copy link
Member

Lms24 commented Aug 6, 2024

@Zen-cronic anything I can help you with or are you fine for the moment? Don't mean to rush you btw, so just let me know :)

@Zen-cronic
Copy link
Contributor Author

Hey @Lms24

thanks for looking out! I got stuck after some more debugging and testing. Here's what I've tried and observed:

  1. With the debug option, span and transaction related events are not logged when using waitForTransaction. However, in other successful tests (react-19, react-17, react-router-6, etc.), those events are emitted correctly.

  2. I tried logging the event object from beforeSendSpan and beforeSendTransaction options of Sentry.init(), but those functions are not invoked at all. Which verifies that span and transaction are not emitted.

  3. Testing with the --ui flag also doesn't help, but further confirms that span and transaction are not emitted. Because the ui tests still hang when the code encounter the call to waitForTransaction.

  4. The e2e tests for webpack-4 and webpack-5 are missing tests for spans and transactions (only errors). And when I tried testing with waitForTransaction (without changing any setup) the same behaviour is observed as in the default-browser package.

^The last point seems really important, because the same "bug" is found in an already existing package.

Some possible root causes:

  • webpack. Because the other tests that doesn't use webpack but use waitForTransaction work. I could try a minimal vite setup.
  • The waitForTransaction function. But after debugging, I don't believe it's the culprit: although it hangs, that's because the span or transaction is not being emitted.

Let me know what you think

@Lms24
Copy link
Member

Lms24 commented Aug 6, 2024

Thanks for the update! I set a reminder to take a look tomorrow. Perhaps there really is a bug that we need to fix.

@Zen-cronic
Copy link
Contributor Author

Sounds great!

@Lms24 Lms24 force-pushed the e2e/default-browser branch from 12ec8b5 to b6dcb74 Compare August 7, 2024 08:22
Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Zen-cronic I took a look at the test app and found the reason why transactions were not sent (see comments). I also took the liberty to change some other small things and rebase the PR to the current develop head.

Now before we merge this, there's one thing I think we should still test, which is that we send a navigation span/transaction as well. For this to test, we need to perform a soft navigation in the browser which we can easiest do with one of these actions:

  • clicking on an element that redirects you to an anchor link (e.g. /#somewhere)
  • calling something like window.history.pushState({}, '', '/test')

Would you be interested in adding this test? If not just let me know then I'll do it.
Again, great work!

integrations: [Sentry.browserTracingIntegration()],
tracesSampleRate: 1.0,
release: 'e2e-test',
environment: 'qa',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just FYI, you couldn't have known this: setting the environment to 'qa' is something we do that, if we actually want to send events from this app to Sentry, we can make sure it arrives and isn't dynamically sampled/dropped by the Sentry backend.

Sentry.init({
dsn: process.env.E2E_TEST_DSN,
integrations: [Sentry.browserTracingIntegration()],
tracesSampleRate: 1.0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For spans and transactions to be emitted, you also need to set tracesSampleRate to >0. In case of e2e tests, for consistent results, we set it to 1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This really was the missing link! Only now do i notice that all the e2e suites that test for spans and transactions have this option enabled to 1.0.

@@ -0,0 +1,14 @@
import * as Sentry from '@sentry/browser';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to @sentry/browser as you already identified :)

// so the code past this line is never executed.
const pageLoadTransaction = await transactionPromise;

expect(pageLoadTransaction).toEqual({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I'd assert on quite a few fields here since we rarely do it this detailed and I'm curious if we get inconsistencies/flakes in this simple app already. We might need to adjust some checks if they get flaky but at least locally, this 100% consistent.

@@ -859,6 +859,7 @@ jobs:
'create-remix-app-express',
'create-remix-app-express-legacy',
'create-remix-app-express-vite-dev',
'default-browser',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the test app to our CI e2e testing matrix

@Zen-cronic
Copy link
Contributor Author

Now before we merge this, there's one thing I think we should still test, which is that we send a navigation span/transaction as well. For this to test, we need to perform a soft navigation in the browser which we can easiest do with one of these actions:

  • clicking on an element that redirects you to an anchor link (e.g. /#somewhere)
  • calling something like window.history.pushState({}, '', '/test')

Would you be interested in adding this test? If not just let me know then I'll do it. Again, great work!

I'd definitely like to take it up. Thanks for all the detailed review!

@Zen-cronic Zen-cronic marked this pull request as ready for review August 7, 2024 15:56
@Zen-cronic
Copy link
Contributor Author

hey @Lms24, the pr is ready whenever you're available. I believe there's no lint issues bcuz running yarn lint in the root of e2e-tests emit no warnings whatsoever on my side.

Let me know if there's anything more needed in this pr.

@Zen-cronic Zen-cronic changed the title test(e2e): Add e2e tests for the @sentry/browser package test(browser): Add e2e tests for the @sentry/browser package Aug 8, 2024
Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks a lot! The navigation test looks good to me. Let's just move it into performance.test.ts and remove the transaction.test.ts file in favour of it. Then I'll merge the PR. Thanks!

@Zen-cronic
Copy link
Contributor Author

Hey, thanks a lot! The navigation test looks good to me. Let's just move it into performance.test.ts and remove the transaction.test.ts file in favour of it. Then I'll merge the PR. Thanks!

Alrighty, all fixed now.

Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @Zen-cronic! We'll make sure to give you a shoutout in the next release :)

Will merge this soon!

@Zen-cronic
Copy link
Contributor Author

No worries @Lms24 and thanks for your guidance. Glad to have helped!

- Update the simple web app to include soft navigation action
-  Include tests for `pageload` and `navigation` transactions/spans
@Lms24 Lms24 force-pushed the e2e/default-browser branch from d2e459d to ebf9dd9 Compare August 13, 2024 07:18
@Lms24 Lms24 enabled auto-merge (squash) August 13, 2024 07:19
@Lms24 Lms24 merged commit 6aeaf42 into getsentry:develop Aug 13, 2024
121 checks passed
Lms24 added a commit that referenced this pull request Aug 13, 2024
This PR adds the external contributor to the CHANGELOG.md file, so that
they are credited for their contribution. See #13125

---

Co-authored-by: Lukas Stracke <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants