|
| 1 | +# Integration Tests for Sentry Browser SDK |
| 2 | + |
| 3 | +Integration tests for Sentry's Browser SDK use [Playwright](https://playwright.dev/) internally. These tests are run on latest stable versions of Chromium, Firefox and Webkit. |
| 4 | + |
| 5 | +## Structure |
| 6 | + |
| 7 | +The tests are grouped by their scope such as `breadcrumbs` or `onunhandledrejection`. In every group of tests, there are multiple folders containing test cases with their optional supporting assets. |
| 8 | + |
| 9 | +Each case group has a default HTML skeleton named `template.hbs`, and also a default initialization script named `init.js `, which contains the `Sentry.init()` call. These defaults are used as fallbacks when a specific `template.hbs` or `init.js` is not defined in a case folder. |
| 10 | + |
| 11 | +`subject.js` contains the logic that sets up the environment to be tested. It also can be defined locally and as a group fallback. Unlike `template.hbs` and `init.js`, it's not required to be defined for a group, as there may be cases that does not require a subject, instead the logic is injected using `injectScriptAndGetEvents` from `utils/helpers.ts`. |
| 12 | + |
| 13 | +`test.ts` is required for each test case, which contains the assertions (and if required the script injection logic). For every case, any set of `init.js`, `template.hbs` and `subject.js` can be defined locally, and each one of them will have precedence over the default definitions of the test group. |
| 14 | + |
| 15 | +``` |
| 16 | +suites/ |
| 17 | +|---- breadcrumbs/ |
| 18 | + |---- template.hbs [fallback template for breadcrumb tests] |
| 19 | + |---- init.js [fallback init for breadcrumb tests] |
| 20 | + |---- subject.js [optional fallback subject for breadcrumb tests] |
| 21 | + |---- click_event_tree/ |
| 22 | + |---- template.hbs [optional case specific template] |
| 23 | + |---- init.js [optional case specific init] |
| 24 | + |---- subject.js [optional case specific subject] |
| 25 | + |---- test.ts [assertions] |
| 26 | +``` |
| 27 | + |
| 28 | +## Writing Tests |
| 29 | + |
| 30 | +### Helpers |
| 31 | + |
| 32 | +`utils/helpers.ts` contains helpers that could be used in assertions (`test.ts`). These helpers define a convenient and reliable API to interact with Playwright's native API. It's highly recommended to define all common patterns of Playwright usage in helpers. |
| 33 | + |
| 34 | +### Fixtures |
| 35 | + |
| 36 | +[Fixtures](https://playwright.dev/docs/api/class-fixtures) allows us to define the globals and test-specific information in assertion groups (`test.ts` files). In it's current state, `fixtures.ts` contains an extension over the pure version of `test()` function of Playwright. All the tests should import `sentryTest` function from `utils/fixtures.ts` instead of `@playwright/test` to be able to access the extra fixtures. |
| 37 | + |
| 38 | +## Running Tests Locally |
| 39 | + |
| 40 | +Tests can be run locally using the latest version of Chromium with: |
| 41 | + |
| 42 | +`yarn test` |
| 43 | + |
| 44 | +To run tests with a different browser such as `firefox` or `webkit`: |
| 45 | + |
| 46 | +`yarn test --browser='firefox'` |
| 47 | +`yarn test --browser='webkit'` |
| 48 | + |
| 49 | +Or to run on all three browsers: |
| 50 | + |
| 51 | +`yarn test --browser='all'` |
| 52 | + |
| 53 | +To filter tests by their title: |
| 54 | + |
| 55 | +`yarn test -g "XMLHttpRequest without any handlers set"` |
| 56 | + |
| 57 | +You can refer to [Playwright documentation](https://playwright.dev/docs/test-cli) for other CLI options. |
| 58 | + |
| 59 | +### Troubleshooting |
| 60 | + |
| 61 | +Apart from [Playwright-specific issues](https://playwright.dev/docs/troubleshooting), below are common issues that might occur while writing tests for Sentry Browser SDK. |
| 62 | + |
| 63 | +- #### Flaky Tests |
| 64 | + If a test fails randomly, giving a `Page Closed`, `Target Closed` or a similar error, most of the times, the reason is a race condition between the page action defined in the `subject` and the listeners of the Sentry event / request. It's recommended to firstly check `utils/helpers.ts` whether if that async logic can be replaced by one of the helpers. If not, whether the awaited (or non-awaited on purpose in some cases) Playwright methods can be orchestrated by [`Promise.all`](http://mdn.io/promise.all). Manually-defined waiting logic such as timeouts are not recommended, and should not be required in most of the cases. |
| 65 | + |
| 66 | +- #### Build Errors |
| 67 | + Before running, a page for each test case is built under the case folder inside `dist`. If a page build is failed, it's recommended to check: |
| 68 | + |
| 69 | + - If both default `template.hbs` and `init.js` are defined for the test group. |
| 70 | + - If a `subject.js` is defined for the test case. |
| 71 | + - If either of `init.js` or `subject.js` contain non-browser code. |
| 72 | + - If the webpack configuration is valid. |
0 commit comments