Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Add storybook visual regression tests (#1246)

sarayourfrienddf16c251e8b5
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index cde1ad19f..946fdb388 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -58,8 +58,8 @@ jobs:
- name: Run unit tests
run: pnpm test:unit
- playwright:
- name: Playwright tests
+ nuxt-playwright:
+ name: Nuxt Playwright tests
runs-on: ubuntu-latest
steps:
@@ -67,7 +67,7 @@ jobs:
- uses: ./.github/actions/setup-node-env
- - name: Run playwright tests
+ - name: Run Nuxt Playwright tests
run: pnpm test:playwright
- uses: actions/upload-artifact@v3
@@ -76,6 +76,24 @@ jobs:
name: test-results
path: test-results
+ storybook-playwright:
+ name: Storybook Playwright tests
+ runs-on: ubuntu-latest
+
+ steps:
+ - uses: actions/checkout@v2
+
+ - uses: ./.github/actions/setup-node-env
+
+ - name: Run Storybook Playwright tests
+ run: pnpm test:storybook
+
+ - uses: actions/upload-artifact@v3
+ if: ${{ failure() }}
+ with:
+ name: test-results
+ path: test-results
+
storybook:
name: Check Storybook build
runs-on: ubuntu-latest
diff --git a/bin/playwright.sh b/bin/playwright.sh
index 10d264aef..275e425ef 100755
--- a/bin/playwright.sh
+++ b/bin/playwright.sh
@@ -7,6 +7,7 @@ version() {
export USER_ID=${USER_ID:-$(id -u)}
export PLAYWRIGHT_ARGS=$@
export PLAYWRIGHT_VERSION=$(version)
+export TEST_COMMAND=${TEST_COMMAND:-test:playwright:local}
echo Running Playwright v$PLAYWRIGHT_VERSION as $USER_ID with Playwright arguments $PLAYWRIGHT_ARGS
diff --git a/docker-compose.playwright.yml b/docker-compose.playwright.yml
index 3df157b08..d768d9d4f 100644
--- a/docker-compose.playwright.yml
+++ b/docker-compose.playwright.yml
@@ -10,7 +10,7 @@ services:
- .:/app
user: ${USER_ID}
working_dir: /app
- command: pnpm test:playwright:local -- ${PLAYWRIGHT_ARGS:-}
+ command: pnpm ${TEST_COMMAND} -- ${PLAYWRIGHT_ARGS:-}
environment:
# This makes the webserver that Playwright runs show the build
- DEBUG=pw:webserver
diff --git a/jest.config.js b/jest.config.js
index d8021d423..6e3932214 100644
--- a/jest.config.js
+++ b/jest.config.js
@@ -22,7 +22,7 @@ module.exports = {
'jest-transform-stub',
'^.+\\.svg$': '<rootDir>/test/unit/svg-transform.js',
},
- testPathIgnorePatterns: ['/playwright/'],
+ testPathIgnorePatterns: ['/playwright/', '/storybook/'],
collectCoverage: true,
coverageDirectory: '<rootDir>/test/unit/coverage',
collectCoverageFrom: [
diff --git a/package.json b/package.json
index e17122999..4c4534da8 100644
--- a/package.json
+++ b/package.json
@@ -23,7 +23,7 @@
"generate": "nuxt generate",
"start": "nuxt start",
"prod": "pnpm build:only && pnpm start",
- "storybook": "nuxt storybook",
+ "storybook": "nuxt storybook --port=54000",
"storybook:build": "pnpm storybook -- build",
"storybook:smoke": "pnpm storybook -- --ci --smoke-test",
"talkback": "node ./test/proxy.js",
@@ -37,6 +37,10 @@
"test:playwright:recreate-tapes": "rimraf test/tapes && pnpm test:playwright:update-tapes",
"test:playwright:update-tapes": "UPDATE_TAPES=true pnpm test:playwright",
"test:playwright:gen": "playwright codegen localhost:8443/",
+ "test:storybook": "TEST_COMMAND=test:storybook:local ./bin/playwright.sh",
+ "test:storybook:local": "playwright test -c test/storybook",
+ "test:storybook:debug": "PWDEBUG=1 pnpm test:storybook:local",
+ "test:storybook:gen": "playwright codegen localhost:54000/",
"types": "vue-tsc --noEmit",
"lint": "prettier --check .; eslint --ext .js,.vue,.ts --ignore-path .gitignore --ignore-path .eslintignore .",
"lint:fix": "prettier --write --loglevel=warn .; pnpm lint -- --fix",
diff --git a/test/playwright/README.md b/test/playwright/README.md
index eeac13b5c..70e2c272e 100644
--- a/test/playwright/README.md
+++ b/test/playwright/README.md
@@ -1,5 +1,7 @@
# Playwright tests
+## 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 docker-compose 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.
diff --git a/test/storybook/README.md b/test/storybook/README.md
new file mode 100644
index 000000000..161875448
--- /dev/null
+++ b/test/storybook/README.md
@@ -0,0 +1,37 @@
+# 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 preferrable to write regular Jest tests with `@testing-library/vue`, however, some interactions require a real web browser context (like loading audio files, testing drag and drop interactions, etc). For these, Playwright is the perfect tool. We can also use Playwright to write visual regression tests for components in isolation so that we don't have to dupliate things like focus state testing into our application level tests.
+
+Our Nuxt playwright tests are described by the [README in the `playwright` test directory](../playwright/README.md). Please see the section on "Dockerization" as it also applies to the Storybook tests.
+
+## Running the tests
+
+To run the tests:
+
+```bash
+pnpm test:storybook
+```
+
+This will run the tests inside a docker container. Should you wish to run the tests locally, you can use the following:
+
+```bash
+pnpm test:storybook:local
+```
+
+But 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.
+
+## Writing tests
+
+It is preferrable 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](./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 `pnpm storybook`. Then run:
+
+```bash
+pnpm 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/test/storybook/playwright.config.ts b/test/storybook/playwright.config.ts
new file mode 100644
index 000000000..d6b80e1a7
--- /dev/null
+++ b/test/storybook/playwright.config.ts
@@ -0,0 +1,26 @@
+import path from 'path'
+
+import { addAliases } from 'module-alias'
+
+import type { PlaywrightTestConfig } from '@playwright/test'
+
+addAliases({
+ '~': path.resolve(process.cwd(), 'src'),
+ '~~': process.cwd(),
+})
+
+const config: PlaywrightTestConfig = {
+ webServer: {
+ command: 'pnpm storybook',
+ timeout: 60_000 * 5, // 5 minutes
+ port: 54000,
+ reuseExistingServer: !process.env.CI || process.env.PWDEBUG === '1',
+ },
+ use: {
+ baseURL: 'http://localhost:54000',
+ trace: 'retain-on-failure',
+ },
+ timeout: 60 * 1e3,
+}
+
+export default config
diff --git a/test/storybook/visual-regression/v-checkbox.spec.ts b/test/storybook/visual-regression/v-checkbox.spec.ts
new file mode 100644
index 000000000..d17a1db36
--- /dev/null
+++ b/test/storybook/visual-regression/v-checkbox.spec.ts
@@ -0,0 +1,27 @@
+import { test, expect } from '@playwright/test'
+
+test.describe('v-checkbox', () => {
+ test.describe('default', () => {
+ test.beforeEach(async ({ page }) => {
+ await page.goto('/iframe.html?id=components-vcheckbox--default')
+ })
+
+ test('default', async ({ page }) => {
+ expect(await page.screenshot()).toMatchSnapshot({ name: 'default.png' })
+ })
+
+ test('on', async ({ page }) => {
+ await page.click('input[type="checkbox"]')
+ expect(await page.screenshot()).toMatchSnapshot({ name: 'on.png' })
+ })
+
+ test('off', async ({ page }) => {
+ // toggle on and off again
+ await page.click('input[type="checkbox"]')
+ // `force: true` is required because the `input`'s pointer events are actually intercepted by the visual SVG.
+ // We still want to check that it works though as that does mimic the user behavior of checking directly on the checkbox.
+ await page.click('input[type="checkbox"]', { force: true })
+ expect(await page.screenshot()).toMatchSnapshot({ name: 'off.png' })
+ })
+ })
+})
diff --git a/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/default-linux.png b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/default-linux.png
new file mode 100644
index 000000000..229dfc442
Binary files /dev/null and b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/default-linux.png differ
diff --git a/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/off-linux.png b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/off-linux.png
new file mode 100644
index 000000000..c911b7a00
Binary files /dev/null and b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/off-linux.png differ
diff --git a/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/on-linux.png b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/on-linux.png
new file mode 100644
index 000000000..68cd5c7c6
Binary files /dev/null and b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/on-linux.png differ