@public-ui/visual-tests 3.0.0-rc.6 → 3.0.0-rc.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@public-ui/visual-tests",
3
- "version": "3.0.0-rc.6",
3
+ "version": "3.0.0-rc.8",
4
4
  "license": "EUPL-1.2",
5
5
  "homepage": "https://public-ui.github.io",
6
6
  "repository": {
@@ -15,6 +15,9 @@
15
15
  "name": "Informationstechnikzentrum Bund",
16
16
  "email": "kolibri@itzbund.de"
17
17
  },
18
+ "engines": {
19
+ "pnpm": "^10"
20
+ },
18
21
  "type": "module",
19
22
  "sideEffects": false,
20
23
  "description": "Provides utility to run visual regression tests for themes.",
@@ -23,18 +26,18 @@
23
26
  },
24
27
  "dependencies": {
25
28
  "@playwright/test": "1.49.1",
26
- "axe-playwright": "2.0.3",
27
- "portfinder": "1.0.32",
29
+ "axe-playwright": "2.1.0",
30
+ "portfinder": "1.0.35",
28
31
  "serve": "14.2.4",
29
- "@public-ui/sample-react": "3.0.0-rc.6"
32
+ "@public-ui/sample-react": "3.0.0-rc.8"
30
33
  },
31
34
  "devDependencies": {
32
- "@babel/eslint-parser": "7.25.9",
35
+ "@babel/eslint-parser": "7.26.10",
33
36
  "@babel/plugin-syntax-import-attributes": "7.26.0",
34
- "@babel/preset-env": "7.26.7",
37
+ "@babel/preset-env": "7.26.9",
35
38
  "eslint": "8.57.1",
36
- "knip": "5.40.0",
37
- "prettier": "3.4.2"
39
+ "knip": "5.46.0",
40
+ "prettier": "3.5.3"
38
41
  },
39
42
  "files": [
40
43
  "playwright.config.js",
@@ -43,7 +46,8 @@
43
46
  ],
44
47
  "scripts": {
45
48
  "format": "prettier --check src",
46
- "lint": "eslint src",
49
+ "lint": "pnpm lint:eslint",
50
+ "lint:eslint": "eslint src",
47
51
  "unused": "knip",
48
52
  "postinstall": "pnpm exec playwright install"
49
53
  }
@@ -2,19 +2,24 @@ import { defineConfig, devices } from '@playwright/test';
2
2
  import * as path from 'path';
3
3
  import * as process from 'process';
4
4
 
5
- const PORT = Number(process.env.KOLIBRI_VISUAL_TEST_PORT);
6
- const URL = `http://localhost:${PORT}`;
5
+ // Validate and set ENVs
6
+ const PORT = parseInt(process.env.KOLIBRI_VISUAL_TEST_PORT || '', 10);
7
+ const BASE_URL = `http://localhost:${PORT}`;
7
8
 
8
- console.log('Serving React Sample app:', URL);
9
+ const CWD = process.env.KOLIBRI_CWD ?? '';
10
+ const TIMEOUT = parseInt(process.env.KOLIBRI_VISUAL_TESTS_TIMEOUT || '15000', 10);
11
+ const EXPECT_TIMEOUT = parseInt(process.env.KOLIBRI_VISUAL_TESTS_EXPECT_TIMEOUT || '5000', 10);
12
+ const BUILD_PATH = process.env.KOLIBRI_VISUAL_TESTS_BUILD_PATH ?? '';
13
+ const THEME = (process.env.THEME_EXPORT || 'default').toLocaleLowerCase();
9
14
 
10
15
  /**
11
16
  * See https://playwright.dev/docs/test-configuration.
12
17
  */
13
18
  export default defineConfig({
14
19
  testDir: './tests',
15
- snapshotDir: path.join(process.env.KOLIBRI_CWD ?? '', 'snapshots'),
20
+ snapshotDir: path.join(CWD, 'snapshots'),
16
21
  // snapshotPathTemplate: '',
17
- outputDir: path.join(process.env.KOLIBRI_CWD ?? '', 'test-results'),
22
+ outputDir: path.join(CWD, 'test-results'),
18
23
  /* Run tests in files in parallel */
19
24
  fullyParallel: true,
20
25
  /* Fail the build on CI if you accidentally left test.only in the source code. */
@@ -24,20 +29,20 @@ export default defineConfig({
24
29
  /* Opt out of parallel tests on CI. */
25
30
  workers: process.env.CI ? 1 : undefined,
26
31
  /* Allow to override the expectation timeout for slow environments */
27
- timeout: process.env.KOLIBRI_VISUAL_TESTS_TIMEOUT ? Number(process.env.KOLIBRI_VISUAL_TESTS_TIMEOUT) : undefined,
32
+ timeout: TIMEOUT,
28
33
  /* Reporter to use. See https://playwright.dev/docs/test-reporters */
29
34
  reporter: 'line',
30
35
  /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
31
36
  use: {
32
37
  /* Base URL to use in actions like `await page.goto('/')`. */
33
- baseURL: URL,
38
+ baseURL: BASE_URL,
34
39
 
35
40
  /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
36
41
  trace: 'on-first-retry',
37
42
  },
38
43
 
39
44
  expect: {
40
- timeout: process.env.KOLIBRI_VISUAL_TESTS_EXPECT_TIMEOUT ? Number(process.env.KOLIBRI_VISUAL_TESTS_EXPECT_TIMEOUT) : undefined,
45
+ timeout: EXPECT_TIMEOUT,
41
46
  },
42
47
 
43
48
  /* Configure projects for major browsers */
@@ -59,8 +64,9 @@ export default defineConfig({
59
64
  /* Run your local dev server before starting the tests */
60
65
  webServer: {
61
66
  command: `npx serve -p ${PORT}`,
62
- cwd: path.resolve(process.env.KOLIBRI_VISUAL_TESTS_BUILD_PATH),
63
- url: URL,
67
+ cwd: path.resolve(BUILD_PATH),
68
+ url: BASE_URL,
64
69
  reuseExistingServer: false,
65
70
  },
71
+ snapshotPathTemplate: `{snapshotDir}/theme-${THEME}/{arg}-{projectName}-{platform}{ext}`,
66
72
  });
package/src/index.js CHANGED
@@ -19,15 +19,8 @@ if (!tempDir) {
19
19
  process.env.THEME_MODULE = path.join(process.cwd(), process.env.THEME_MODULE); // Use current working directory (i.e. the theme folder) to complete module path
20
20
  const visualsTestModulePath = fileURLToPath(new URL('..', import.meta.url));
21
21
  const binaryPath = fileURLToPath(new URL('../node_modules/.bin', import.meta.url));
22
-
23
- let sampleReactPath = '../node_modules/@public-ui/sample-react';
24
- let backSteps = ``;
25
- let workingDir = null;
26
- do {
27
- const url = new URL(backSteps + sampleReactPath, import.meta.url);
28
- workingDir = fileURLToPath(url);
29
- backSteps += `../`;
30
- } while (!fs.existsSync(workingDir) && path.resolve(process.cwd(), backSteps) !== '/');
22
+ const sampleReactPackageJsonPath = import.meta.resolve('@public-ui/sample-react/package.json');
23
+ const workingDir = fileURLToPath(path.dirname(sampleReactPackageJsonPath));
31
24
 
32
25
  if (!fs.existsSync(workingDir)) {
33
26
  throw new Error('Could not find React Sample App package. Please install it with "npm install @public-ui/sample-react".');
@@ -93,6 +93,11 @@ ROUTES.set('button/baselined', {
93
93
  skipFailures: false,
94
94
  },
95
95
  });
96
+ ROUTES.set('button/short-key', {
97
+ axe: {
98
+ skipFailures: false,
99
+ },
100
+ });
96
101
  ROUTES.set('card/basic', {
97
102
  axe: {
98
103
  skipFailures: false,
@@ -1,40 +1,6 @@
1
1
  import { test, expect } from '@playwright/test';
2
2
  import { ROUTES } from './sample-app.routes.js';
3
3
 
4
- // https://github.com/microsoft/playwright/issues/7575#issuecomment-1288164474
5
- export const configureSnapshotPath =
6
- () =>
7
- // eslint-disable-next-line no-empty-pattern
8
- ({}, testInfo) => {
9
- const originalSnapshotPath = testInfo.snapshotPath;
10
- testInfo.snapshotPath = (snapshotName) => {
11
- const result = originalSnapshotPath
12
- .apply(testInfo, [snapshotName])
13
-
14
- // Remove browser name from snapshot name
15
- // .replace('-chromium', '')
16
- // .replace('-firefox', '')
17
-
18
- // Remove os name from snapshot name
19
- // .replace('-darwin', '')
20
- // .replace('-linux', '')
21
- // .replace('-windows', '')
22
-
23
- // Remove test counter from snapshot name
24
- .replace('-1-', '-')
25
-
26
- // Identify 2. test as zoom snapshot
27
- .replace('-2-', '-zoom-')
28
-
29
- // Make different snapshot folder for different themes
30
- .replace('theme-snapshots.spec.js', `theme-${(process.env.THEME_EXPORT || 'default').toLocaleLowerCase()}`)
31
- .replace('-snapshots', '');
32
- return result;
33
- };
34
- };
35
-
36
- test.beforeEach(configureSnapshotPath());
37
-
38
4
  // https://playwright.dev/docs/emulation
39
5
  test.use({
40
6
  colorScheme: 'light',
@@ -47,27 +13,52 @@ test.use({
47
13
  },
48
14
  });
49
15
 
16
+ const DEFAULT_SNAPSHOT_OPTIONS = {
17
+ animations: 'disabled',
18
+ fullPage: true,
19
+ maxDiffPixelRatio: 0,
20
+ scale: 'css', // 'css' or 'device'
21
+ timeout: 10000,
22
+ };
23
+
50
24
  ROUTES.forEach((options, route) => {
51
25
  test(`snapshot for ${route}`, async ({ page }) => {
52
26
  const hideMenusParam = `${route.includes('?') ? '&' : '?'}hideMenus`;
53
- await page.goto(`/#${route}${hideMenusParam}`, { waitUntil: 'networkidle' });
27
+ await page.goto(`/#${route}${hideMenusParam}`);
28
+ await page.waitForLoadState('networkidle');
29
+ await page.addStyleTag({
30
+ content: `
31
+ * {
32
+ transition: none !important;
33
+ animation: none !important;
34
+ }
35
+ `,
36
+ });
54
37
  if (options?.viewportSize) {
55
38
  await page.setViewportSize(options.viewportSize);
56
39
  }
57
40
  if (options?.waitForTimeout) {
58
41
  await page.waitForTimeout(options.waitForTimeout);
59
42
  }
60
- await expect(page).toHaveScreenshot({
61
- fullPage: true,
62
- maxDiffPixelRatio: 0.01,
43
+
44
+ /**
45
+ * We would like to use a readable name for the snapshot file.
46
+ */
47
+ const snapshotName = `snapshot-for-${route.replace(/(\/|\?|&|=)/g, '-')}`;
48
+
49
+ await expect(page).toHaveScreenshot(`${snapshotName}.png`, {
50
+ ...DEFAULT_SNAPSHOT_OPTIONS,
63
51
  ...options,
64
52
  });
65
53
  await page.evaluate(() => {
54
+ // eslint-disable-next-line no-undef
66
55
  document.body.style.zoom = '400%';
56
+ // document.body.style.transform = 'scale(4)';
57
+ // document.body.style.transformOrigin = 'top left';
58
+ // document.body.style.width = '25vw';
67
59
  });
68
- await expect(page).toHaveScreenshot({
69
- fullPage: true,
70
- maxDiffPixelRatio: 0.02,
60
+ await expect(page).toHaveScreenshot(`${snapshotName}-zoom.png`, {
61
+ ...DEFAULT_SNAPSHOT_OPTIONS,
71
62
  ...options,
72
63
  });
73
64
  });