Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
combine frontend testing documentation pages (#4546)
Princewill Onyenanu1c05ef067ff1
diff --git a/.github/workflows/ci_cd.yml b/.github/workflows/ci_cd.ymlindex 82f860096..1ee78bf9a 100644--- a/.github/workflows/ci_cd.yml+++ b/.github/workflows/ci_cd.yml@@ -825,7 +825,9 @@ jobs:It looks like some of the Playwright tests failed. You can download the trace output and image diffs for the failed tests under the "Artifacts" section in the above page.- You can read the [docs on how to use this artifact](https://docs.openverse.org/frontend/reference/playwright_tests.html#debugging).+ You can read the [docs on how to use this artifact](https://docs.openverse.org/frontend/reference/testing_guidelines.html#debugging).++ Additionally, if you made changes to the frontend UI, you may need to [update relevant playwright test snapshots](https://docs.openverse.org/frontend/guides/test.html#updating-snapshots).If the test is flaky, follow the [flaky test triage procedure](https://docs.openverse.org/general/test.html#flaky-tests).$EOFdiff --git a/documentation/conf.py b/documentation/conf.pyindex 86b20db59..7fee48201 100644--- a/documentation/conf.py+++ b/documentation/conf.py@@ -103,6 +103,8 @@ redirects = {"meta/contribution/maintainer_tasks": "/general/contribution/maintainer_tasks.html","meta/contribution/codespell": "/general/contribution/codespell.html","meta/contribution/good_first_and_help_wanted_issues": "/general/contribution/good_first_and_help_wanted_issues.html",+ "frontend/reference/playwright_tests.html": "/frontend/reference/testing_guidelines.html#playwright-tests",+ "frontend/reference/storybook_tests.html": "/frontend/reference/testing_guidelines.html#storybook-tests",}myst_enable_extensions = ["linkify"]diff --git a/documentation/frontend/guides/test.md b/documentation/frontend/guides/test.mdindex 63a12589f..02fdb14cf 100644--- a/documentation/frontend/guides/test.md+++ b/documentation/frontend/guides/test.md@@ -3,12 +3,9 @@Once you've made some changes to the codebase, it is important to run tests.Openverse uses unit tests, Playwright tests for end-to-end, and visualregression testing of the app and Storybook components. This guide will help you-run the tests. To learn more about how to test PRs, read the-[testing guidelines](/frontend/reference/testing_guidelines.md). To learn more-about the Playwright tests, read-[this guide](/frontend/reference/playwright_tests.md). To learn more about the-Storybook visual regression tests, read-[this guide](/frontend/reference/storybook_tests.md).+run the tests. To learn more about how to test PRs, Playwright tests and+Storybook visual regression tests read the+[testing guidelines](/frontend/reference/testing_guidelines.md).## Steps@@ -44,6 +41,19 @@ Storybook visual regression tests, read./ov just frontend/run test:storybook```+ ````{note}+ This will run the Storybook visual regression tests inside a docker container. Should you wish to run the+ tests locally, you can use the command below. However, please note that unless+ you are running the same operating system (down to the distro, in some cases)+ there are likely to be visual rendering differences that will cause snapshot+ tests to fail on false-positives.++ ```bash+ ./ov just frontend/run test:storybook:local+ ```++ ````+## Updating snapshotsIf you've made changes to the frontend that require updating snapshots, you candiff --git a/documentation/frontend/reference/index.md b/documentation/frontend/reference/index.mdindex 5eafbc596..24468db15 100644--- a/documentation/frontend/reference/index.md+++ b/documentation/frontend/reference/index.md@@ -6,8 +6,6 @@i18nfeature_flagstesting_guidelines-playwright_tests-storybook_testsmiscellaneoushealthchecknginxdiff --git a/documentation/frontend/reference/playwright_tests.md b/documentation/frontend/reference/playwright_tests.mddeleted file mode 100644index 7b5722302..000000000--- a/documentation/frontend/reference/playwright_tests.md+++ /dev/null@@ -1,233 +0,0 @@-# Playwright tests--## Visual regression and end-to-end tests--We run the Playwright test suite on each PR to test that the front end works-correctly and there are no visual regressions.--The **end-to-end** tests make sure that functionality works as expected and are-located in `test/playwright/e2e`. They test, for instance, that we can open the-filters by clicking on the Filters button in the header, or select the filters-and execute the relevant search by clicking on the filter checkboxes.--There are also **visual regression** tests that make sure that the pages display-correct components, and components are rendered correctly. The components can be-tested in isolation to make sure that the states are correctly rendered. For-more on which states should be tested, see-[`Testing guidelines.md`](./testing_guidelines.md). These tests should use-Storybook to render the component in isolation, and be placed in-`frontend/test/storybook/visual-regression`.--The component tests that test that the component state is correctly rendered-based on the page interaction should be placed in-`frontend/test/visual-regression/components`. For example, the header elements-are tested this way because their appearance depends on the page scroll-position.--## Dockerization--Our Playwright test suite runs inside a docker container in order to prevent-cross-platform browser differences from creating flaky test behavior. We run-both end-to-end and visual-regression tests in the same container to save from-having to run the Nuxt production build twice.--Having Docker and Compose V2 is a pre-requisite to running the Playwright tests-locally. Please follow-[the relevant instructions for your operating system for how to install docker and docker-compose](https://docs.docker.com/get-docker/).-If you're on Windows 10 Home Edition, please note that you'll need to-[install and run docker inside WSL2](https://www.freecodecamp.org/news/how-to-run-docker-on-windows-10-home-edition/).-We strongly recommend (and only plan to support) Windows users run everything-inside of WSL.--If it's not possible for you to run docker locally, don't fret! Our CI will run-it on every pull request and another contributor who is able to run the tests-locally can help you develop new or update existing tests for your changes.--The Playwright docker container runs everything needed for the end-to-end and-visual-regression tests, including the Nuxt server and a-[Talkback proxy](https://github.com/ijpiantanida/talkback) for the API. It will-also generate and match against the existing visual-regression snapshots in the-codebase.--## Running the tests--To run the end-to-end tests, after having installed docker, run the following:--```bash-./ov just frontend/run test:playwright-```--You may pass arguments to playwright directly, for example `-u` to update-snapshots or a filter.--```bash-./ov just frontend/run test:playwright visual-regression -u-```--The above will run only test files with `visual-regression` in the path and will-update any snapshot tests due to the `-u` flag.--## Visual Regression tests--When writing visual regression tests, it is good practice to write tests for-each relevant breakpoint. There is a series of helpers in-[`frontend/test/utils/breakpoints.ts`](https://github.com/WordPress/openverse/blob/main/frontend/test/playwright/utils/breakpoints.ts)-for this. The most common usage for this module is to run a test or set of tests-for every breakpoint our app knows about or a subset of them. Occasionally you-will need to separate tests by breakpoint or by range for which there are also-helpers.--Each of the following methods expects a describe block callback function-accepting some useful helpers.--- `breakpoints.describeEvery`: Generates a describe block for every breakpoint- our app is concerned with.-- `breakpoints.describeEachDesktop`: Generates a describe block for every- breakpoint that roughly correlates with desktop and tablet width devices.-- `breakpoints.describeEachMobile`: Generates a describe block for every- breakpoint that roughly correlates with smartphone sized mobile devices.-- `breakpoints.describeEachBreakpoint`: Accepts a list of breakpoints to- generate describe blocks for, with similar syntax to `jest`'s `test.each`- helper. Used like- `breakpoints.describeEachBreakpoint(['xs', '2xl'])(<block>)`. `describeEvery`- and the two `describeEach*` methods are aliases for pre-configured- `describeEachBreakpoint`. It is unlikely that you will need to use this helper- directly.--Additionally, there are describe blocks for individual breakpoints, each-following the pattern `` `describe${Capitalize<Breakpoint>}` ``, for example-`breakpoints.describeXs`.--The describe blocks are passed the following helpers:--- `breakpoint`: The name of the breakpoint for the current describe block.-- `getConfigValues`: Returns useful configuration values if running a snapshot- test manually.-- `expectSnapshot`: A function accepting an identifier and a screenshot-able- object. This will generate the screenshot and match it against a snapshot- matching the name passed. Remember to `await` this function or else the- `expect` will not be fired within the context of the test (you'll get a nasty- error in this case).--Optionally, you may pass a configuration object as the first argument to the-`breakpoints.describe*` functions. If you do this, then the describe block is-the second argument.--The configuration object currently supports the following options:--- `uaMocking`: This `boolean` option defaults to `true`. When enabled, it will- use a mock mobile browser user agent string for narrow viewports. Setting it- to `false` for viewport widths above `md` (inclusive) is a no-op.--Please see the-[`homepage.spec.ts` visual-regression tests](https://github.com/WordPress/openverse/blob/main/frontend/test/playwright/visual-regression/pages/homepage.spec.ts)-as an example of how to use these helpers.--### What to test for visual regression--Visual regression tests can match entire pages or on individual elements in-specific states. They're useful, for example, to test the various focus, hover,-and active states of interactive elements. Use-[`page.locator`](https://playwright.dev/docs/api/class-page#page-locator) to-select specific elements to pass to `expectSnapshot`.--## API proxy--The Playwright test container also runs an API proxy using-[Talkback](https://github.com/ijpiantanida/talkback) to prevent network requests-from hitting the live API during end-to-end and visual-regression tests. This-significantly speeds up the tests and makes the data being tested against-consistent across test runs and eliminates variations that the API may include-after data refreshes happen.--The configuration for the Talkback proxy is in the-[`proxy.js`](https://github.com/WordPress/openverse/blob/main/frontend/test/proxy.js)-module and is run using `pnpm talkback`. It is rare and unlikely that you will-need to run it directly as running it is handled by the Playwright `webServer`-configuration.--If you've added new tests or updated existing ones, you may get errors about API-responses not being found. To remedy this, you'll need to update the tapes:--```bash-./ov just frontend/run test:playwright:update-tapes-```--If for some reason you find yourself needing to completely recreate the tapes,-you may do so using the `test:playwright:recreate-tapes` script. Please use this-sparingly as it creates massive diffs in PRs (tens of thousands of lines across-over literally hundreds of JSON files). Note that you may be rate-limited by the-upstream production API if you do this. There is no official workaround for this-at the moment.--## Debugging--Additional debugging may be accomplished in two ways. You may inspect the trace-output of failed tests by finding the `trace.zip` for the relevant test in the-`test-results` folder. Traces are only saved for failed tests. You can use the-[Playwright Trace Viewer](https://playwright.dev/docs/trace-viewer) to inspect-these (or open the zip yourself and poke around the files on your own).--For Playwright tests failing in CI, a GitHub comment will appear with a link to-download an artifact of the `test-results` folder.--Visual regression tests that fail to match the existing snapshots will also have-`expected`, `actual` and `diff` files generated that are helpful for-understanding why intermittent failures are happening. These are generated-automatically by Playwright and will be placed in the `test-results` folder-under the fully qualified name of the test that failed (with every parent-describe block included).--Additionally, you can run the tests in debug mode. This will run the tests with-a headed browser as opposed to a headless (invisible) one and allow you to watch-the test happen in real time. It's not possible for a headed browser to run-inside the docker container, however, so be aware that when debugging the-environment will be slightly different. For example, if you're on any OS other-than Linux, the browser you're running will have small differences in how it-renders the page compared to the docker container.--To run the debug tests:--```bash-./ov just frontend/run test:playwright:debug-```--Note that this still runs the talkback proxy and the Nuxt server for you. If-you'd like to avoid this, simply run the Nuxt server before you run the-`test:playwright:debug` script and Playwright will automatically prefer your-previously running Nuxt server.--<aside>-For some reason the following two tests are consistently flaky when updating tapes but appear to be stable when running the e2e tapes with pre-existing tapes.--```-search-types.spec.js:102:3 › Can open All content page client-side-search-types.spec.js:102:3 › Can open Images page client-side-```--Don't be alarmed if you notice this.--</aside>--## Generating new tests--When writing end-to-end tests, it can be helpful to use Playwright-[codegen](https://playwright.dev/docs/codegen#running-codegen) to generate the-tests by performing actions in the browser:--```bash-./ov just frontend/run test:playwright:gen-```--This will open the app in a new browser window, and record any actions you take-in a format that can be used in end-to-end tests.--Note that this does _not_ run the server for you; you must run the Nuxt server-using `pnpm start` or `pnpm dev` separately before running the codegen script.--To generate tests for a non-default breakpoint, set the viewport size using the-`--viewport-size` flag. For example, to test the `xs` breakpoint, run:--```bash-./ov just frontend/run test:playwright:gen --viewport-size=340,600"-```diff --git a/documentation/frontend/reference/storybook_tests.md b/documentation/frontend/reference/storybook_tests.mddeleted file mode 100644index 58e6856ba..000000000--- a/documentation/frontend/reference/storybook_tests.md+++ /dev/null@@ -1,65 +0,0 @@-# Storybook tests--We run Playwright tests for our Nuxt application. We also run them for-Storybook, where components are tested in isolation and (ideally) in a-comprehensive way that is not always possible inside an actual application.--Generally, it is preferable to write regular Jest tests with-`@testing-library/vue`, however, some interactions require a real web browser-context (like loading audio files, complex popover interactions, testing drag-and drop interactions, etc). For these, Playwright is the perfect tool.--We also use Playwright to write visual regression tests for components in-isolation so that we don't have to duplicate things like focus state testing-into our application-level tests. Sometimes, components cannot be tested in the-full app because they are shifted 1-2 pixels between the tests and therefore the-identical images do not match due to shifting.--You will find the functional component tests in `/test/storybook/functional`,-and visual regression tests that with snapshots in-`/test/storybook/visual-regression`--Our Nuxt playwright tests are described by the-[Playwright testing guide](./playwright_tests.md). Please see the section on-"Dockerization" as it also applies to the Storybook tests.--## Running the tests--To run the tests:--```bash-./ov just frontend/run test:storybook-```--This will run the tests inside a docker container. Should you wish to run the-tests locally, you can use the command below. However, please note that unless-you are running the same operating system (down to the distro, in some cases)-there are likely to be visual rendering differences that will cause snapshot-tests to fail on false-positives.--```bash-./ov just frontend/run test:storybook:local-```--## Writing tests--It is preferable to write tests using the iframed version of the component to-avoid Storybook UI updates from effective snapshots. To access this, take the-`path` query parameter for any story and apply it to the `/iframe.html` route.-See-[`v-checkbox.spec.ts` for an example](https://github.com/WordPress/openverse/blob/main/frontend/test/storybook/visual-regression/v-checkbox.spec.ts)--### Codegen--You can use Playwright's `codegen` tool to generate tests. For this you will-need to run Storybook locally on your own using-`./ov just frontend/run storybook`. Then run:--```bash-./ov just frontend/run test:storybook:gen-```--Please note that the codegen script automatically runs against the non-iframed-version of storybook, so while you can record interactions and use the resulting-script, you will likely need to adapt it for running inside the iframed version-of the page in the actual test.diff --git a/documentation/frontend/reference/testing_guidelines.md b/documentation/frontend/reference/testing_guidelines.mdindex 90d249587..2ef23eada 100644--- a/documentation/frontend/reference/testing_guidelines.md+++ b/documentation/frontend/reference/testing_guidelines.md@@ -208,8 +208,246 @@ re-written using testing library.### Playwright tests+<!-- ## Visual regression and end-to-end tests -->++We run the Playwright test suite on each PR to test that the front end works+correctly and there are no visual regressions.++The **end-to-end** tests make sure that functionality works as expected and are+located in `test/playwright/e2e`. They test, for instance, that we can open the+filters by clicking on the Filters button in the header, or select the filters+and execute the relevant search by clicking on the filter checkboxes.++The component tests that test that the component state is correctly rendered+based on the page interaction should be placed in+`frontend/test/visual-regression/components`. For example, the header elements+are tested this way because their appearance depends on the page scroll+position.++### Storybook tests++There are also **visual regression** tests that make sure that the pages display+correct components, and that the components are rendered correctly. The+components can be tested in isolation to make sure that the states are correctly+rendered and so that we don't have to duplicate things like focus state testing+into our application-level tests. These tests should use Storybook to render the+component in isolation, and be placed in+`frontend/test/storybook/visual-regression`.++You will find the functional component tests in `/test/storybook/functional`,+and visual regression tests with snapshots in+`/test/storybook/visual-regression`++### Visual Regression tests++When writing visual regression tests, it is good practice to write tests for+each relevant breakpoint. There is a series of helpers in+[`frontend/test/utils/breakpoints.ts`](https://github.com/WordPress/openverse/blob/main/frontend/test/playwright/utils/breakpoints.ts)+for this. The most common usage for this module is to run a test or set of tests+for every breakpoint our app knows about or a subset of them. Occasionally you+will need to separate tests by breakpoint or by range for which there are also+helpers.++Each of the following methods expects a describe block callback function+accepting some useful helpers.++- `breakpoints.describeEvery`: Generates a describe block for every breakpoint+ our app is concerned with.+- `breakpoints.describeEachDesktop`: Generates a describe block for every+ breakpoint that roughly correlates with desktop and tablet width devices.+- `breakpoints.describeEachMobile`: Generates a describe block for every+ breakpoint that roughly correlates with smartphone sized mobile devices.+- `breakpoints.describeEachBreakpoint`: Accepts a list of breakpoints to+ generate describe blocks for, with similar syntax to `jest`'s `test.each`+ helper. Used like+ `breakpoints.describeEachBreakpoint(['xs', '2xl'])(<block>)`. `describeEvery`+ and the two `describeEach*` methods are aliases for pre-configured+ `describeEachBreakpoint`. It is unlikely that you will need to use this helper+ directly.++Additionally, there are describe blocks for individual breakpoints, each+following the pattern `` `describe${Capitalize<Breakpoint>}` ``, for example+`breakpoints.describeXs`.++The describe blocks are passed the following helpers:++- `breakpoint`: The name of the breakpoint for the current describe block.+- `getConfigValues`: Returns useful configuration values if running a snapshot+ test manually.+- `expectSnapshot`: A function accepting an identifier and a screenshot-able+ object. This will generate the screenshot and match it against a snapshot+ matching the name passed. Remember to `await` this function or else the+ `expect` will not be fired within the context of the test (you'll get a nasty+ error in this case).++Optionally, you may pass a configuration object as the first argument to the+`breakpoints.describe*` functions. If you do this, then the describe block is+the second argument.++The configuration object currently supports the following options:++- `uaMocking`: This `boolean` option defaults to `true`. When enabled, it will+ use a mock mobile browser user agent string for narrow viewports. Setting it+ to `false` for viewport widths above `md` (inclusive) is a no-op.+Please see the-[Playwright test guidelines](/frontend/reference/playwright_tests.md) for-instructions on running and maintaining the Playwright test suite, and the-[Storybook test guidelines](/frontend/reference/storybook_tests.md) for-instructions on the Storybook Playwright test suite.+[`homepage.spec.ts` visual-regression tests](https://github.com/WordPress/openverse/blob/main/frontend/test/playwright/visual-regression/pages/homepage.spec.ts)+as an example of how to use these helpers.++### What to test for visual regression++Visual regression tests can match entire pages or on individual elements in+specific states. They're useful, for example, to test the various focus, hover,+and active states of interactive elements. Use+[`page.locator`](https://playwright.dev/docs/api/class-page#page-locator) to+select specific elements to pass to `expectSnapshot`.++### Writing tests++#### Playwright++When writing end-to-end tests, it can be helpful to use Playwright+[codegen](https://playwright.dev/docs/codegen#running-codegen) to generate the+tests by performing actions in the browser:++```bash+./ov just frontend/run test:playwright:gen+```++This will open the app in a new browser window, and record any actions you take+in a format that can be used in end-to-end tests.++Note that this does _not_ run the server for you; you must run the Nuxt server+using `pnpm start` or `pnpm dev` separately before running the codegen script.++To generate tests for a non-default breakpoint, set the viewport size using the+`--viewport-size` flag. For example, to test the `xs` breakpoint, run:++```bash+./ov just frontend/run test:playwright:gen --viewport-size=340,600"+```++#### Storybook++It is preferable to write tests using the iframed version of the component to+avoid Storybook UI updates from effective snapshots. To access this, take the+`path` query parameter for any story and apply it to the `/iframe.html` route.+See+[`v-checkbox.spec.ts` for an example](https://github.com/WordPress/openverse/blob/main/frontend/test/storybook/visual-regression/v-checkbox.spec.ts)++You can use Playwright's+[codegen](https://playwright.dev/docs/codegen#running-codegen) tool to generate+tests. For this you will need to run Storybook locally on your own using+`./ov just frontend/run storybook`. Then run:++```bash+./ov just frontend/run test:storybook:gen+```++Please note that the codegen script automatically runs against the non-iframed+version of storybook, so while you can record interactions and use the resulting+script, you will likely need to adapt it for running inside the iframed version+of the page in the actual test.++### Debugging++Additional debugging may be accomplished in two ways. You may inspect the trace+output of failed tests by finding the `trace.zip` for the relevant test in the+`test-results` folder. Traces are only saved for failed tests. You can use the+[Playwright Trace Viewer](https://playwright.dev/docs/trace-viewer) to inspect+these (or open the zip yourself and poke around the files on your own).++For Playwright tests failing in CI, a GitHub comment will appear with a link to+download an artifact of the `test-results` folder.++Visual regression tests that fail to match the existing snapshots will also have+`expected`, `actual` and `diff` files generated that are helpful for+understanding why intermittent failures are happening. These are generated+automatically by Playwright and will be placed in the `test-results` folder+under the fully qualified name of the test that failed (with every parent+describe block included).++Additionally, you can run the tests in debug mode. This will run the tests with+a headed browser as opposed to a headless (invisible) one and allow you to watch+the test happen in real time. It's not possible for a headed browser to run+inside the docker container, however, so be aware that when debugging the+environment will be slightly different. For example, if you're on any OS other+than Linux, the browser you're running will have small differences in how it+renders the page compared to the docker container.++To run the debug tests:++```bash+./ov just frontend/run test:playwright:debug+```++Note that this still runs the talkback proxy and the Nuxt server for you. If+you'd like to avoid this, simply run the Nuxt server before you run the+`test:playwright:debug` script and Playwright will automatically prefer your+previously running Nuxt server.++<aside>+For some reason the following two tests are consistently flaky when updating tapes but appear to be stable when running the e2e tapes with pre-existing tapes.++```+search-types.spec.js:102:3 › Can open All content page client-side+search-types.spec.js:102:3 › Can open Images page client-side+```++Don't be alarmed if you notice this.++</aside>++### Dockerization++Our Playwright test suite runs inside a docker container in order to prevent+cross-platform browser differences from creating flaky test behavior. We run+both end-to-end and visual-regression tests in the same container to save from+having to run the Nuxt production build twice.++Having Docker and Compose V2 is a pre-requisite to running the Playwright tests+locally. Please follow+[the relevant instructions for your operating system for how to install docker and docker-compose](https://docs.docker.com/get-docker/).+If you're on Windows 10 Home Edition, please note that you'll need to+[install and run docker inside WSL2](https://www.freecodecamp.org/news/how-to-run-docker-on-windows-10-home-edition/).+We strongly recommend (and only plan to support) Windows users run everything+inside of WSL.++If it's not possible for you to run docker locally, don't fret! Our CI will run+it on every pull request and another contributor who is able to run the tests+locally can help you develop new or update existing tests for your changes.++The Playwright docker container runs everything needed for the end-to-end and+visual-regression tests, including the Nuxt server and a+[Talkback proxy](https://github.com/ijpiantanida/talkback) for the API. It will+also generate and match against the existing visual-regression snapshots in the+codebase.++### API proxy++The Playwright test container also runs an API proxy using+[Talkback](https://github.com/ijpiantanida/talkback) to prevent network requests+from hitting the live API during end-to-end and visual-regression tests. This+significantly speeds up the tests and makes the data being tested against+consistent across test runs and eliminates variations that the API may include+after data refreshes happen.++The configuration for the Talkback proxy is in the+[`proxy.js`](https://github.com/WordPress/openverse/blob/main/frontend/test/proxy.js)+module and is run using `pnpm talkback`. It is rare and unlikely that you will+need to run it directly as running it is handled by the Playwright `webServer`+configuration.++If you've added new tests or updated existing ones, you may get errors about API+responses not being found. To remedy this, you'll need to update the tapes:++```bash+./ov just frontend/run test:playwright:update-tapes+```++If for some reason you find yourself needing to completely recreate the tapes,+you may do so using the `test:playwright:recreate-tapes` script. Please use this+sparingly as it creates massive diffs in PRs (tens of thousands of lines across+over literally hundreds of JSON files). Note that you may be rate-limited by the+upstream production API if you do this. There is no official workaround for this+at the moment.