@public-ui/visual-tests 3.0.0-rc.0 → 3.0.0-rc.10

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/README.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # KoliBri - Visual Tests
2
2
 
3
+ [![npm](https://img.shields.io/npm/v/@public-ui/visual-tests)](https://www.npmjs.com/package/@public-ui/components)
4
+ [![license](https://img.shields.io/npm/l/@public-ui/visual-tests)](https://github.com/public-ui/kolibri/blob/main/LICENSE)
5
+ [![downloads](https://img.shields.io/npm/dt/@public-ui/visual-tests)](https://www.npmjs.com/package/@public-ui/visual-tests)
6
+ [![issues](https://img.shields.io/github/issues/public-ui/kolibri)](https://github.com/public-ui/kolibri/issues)
7
+ [![pull requests](https://img.shields.io/github/issues-pr/public-ui/kolibri)](https://github.com/public-ui/kolibri/pulls)
8
+ [![size](https://img.shields.io/bundlephobia/min/@public-ui/visual-tests)](https://bundlephobia.com/result?p=@public-ui/visual-tests)
9
+ ![contributors](https://img.shields.io/github/contributors/public-ui/kolibri)
10
+
3
11
  ## Motivation
4
12
 
5
13
  The `KoliBri` Visual Tests provide a way to add visual regression testing to **theme** modules.
@@ -7,11 +15,23 @@ It takes screenshots of every component defined in the [React Sample App](https:
7
15
 
8
16
  ## Installation
9
17
 
18
+ It is recommended to configure NPM via `.npmrc`:
19
+
20
+ ```bash
21
+ # - npm
22
+ engine-strict=true
23
+ save-exact=true
24
+
25
+ # - pnpm
26
+ shamefully-hoist=true # this is required for the visual tests to work
27
+ workspace-concurrency=1
28
+ ```
29
+
10
30
  You can install the `KoliBri` Visual Tests with `npm`, `pnpm` or `yarn`:
11
31
 
12
32
  ```bash
13
33
  npm i -D @public-ui/visual-tests
14
- pnpm i -D @public-ui/visual-tests
34
+ pnpm i -D @public-ui/visual-tests # recommended
15
35
  yarn add -D @public-ui/visual-tests
16
36
  ```
17
37
 
@@ -21,17 +41,21 @@ Add the following npm scripts to the theme's `package.json`:
21
41
 
22
42
  ```json
23
43
  {
24
- "scripts": {
25
- "test": "THEME_MODULE=src/index THEME_EXPORT=THEME_NAME kolibri-visual-test",
26
- "test-update": "THEME_MODULE=src/index THEME_EXPORT=THEME_NAME kolibri-visual-test --update-snapshots",
27
- }
44
+ "scripts": {
45
+ "test": "THEME_MODULE=src/index THEME_EXPORT=THEME_NAME kolibri-visual-test",
46
+ "test-update": "THEME_MODULE=src/index THEME_EXPORT=THEME_NAME kolibri-visual-test --update-snapshots"
47
+ }
28
48
  }
29
49
  ```
30
50
 
31
- * `THEME_MODULE` defines the relative path to the TypeScript module containing the the theme definitions. Without file extension.
32
- * `THEME_EXPERT` defines the name of the export within the the module. (e.g., `export const THEME_NAME = {/**/};`) Defaults to `default`.
51
+ ### Environment variables
52
+
53
+ - `THEME_MODULE`: Define the relative path to the TypeScript module containing the theme definitions. Without file extension.
54
+ - `THEME_EXPERT`: Define the name of the export within the module. (e.g., `export const THEME_NAME = {/**/};`) Defaults to `default`.
55
+ - `KOLIBRI_VISUAL_TESTS_TIMEOUT`: Define the Playwright [test timeout](https://playwright.dev/docs/test-timeouts).
56
+ - `KOLIBRI_VISUAL_TESTS_EXPECT_TIMEOUT`: Define the Playwright [expect timeout](https://playwright.dev/docs/test-timeouts).
33
57
 
34
58
  Run the tests with `npm test`. The first time, this will create a new folder `snapshots` which is supposed to be committed to the repository.
35
59
  In the following runs, new screenshots will be compared to this reference.
36
60
 
37
- To update the reference screenshots call `npm run test-update`.
61
+ To update the reference screenshots call `npm run test-update`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@public-ui/visual-tests",
3
- "version": "3.0.0-rc.0",
3
+ "version": "3.0.0-rc.10",
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.",
@@ -22,19 +25,19 @@
22
25
  "kolibri-visual-test": "src/index.js"
23
26
  },
24
27
  "dependencies": {
25
- "@playwright/test": "1.49.0",
26
- "axe-playwright": "2.0.3",
27
- "portfinder": "1.0.32",
28
+ "@playwright/test": "1.49.1",
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.0"
32
+ "@public-ui/sample-react": "3.0.0-rc.10"
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.0",
37
+ "@babel/preset-env": "7.26.9",
35
38
  "eslint": "8.57.1",
36
- "knip": "5.37.1",
37
- "prettier": "3.3.3"
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,18 +29,22 @@ 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
 
44
+ expect: {
45
+ timeout: EXPECT_TIMEOUT,
46
+ },
47
+
39
48
  /* Configure projects for major browsers */
40
49
  projects: [
41
50
  // {
@@ -55,8 +64,9 @@ export default defineConfig({
55
64
  /* Run your local dev server before starting the tests */
56
65
  webServer: {
57
66
  command: `npx serve -p ${PORT}`,
58
- cwd: path.resolve(process.env.KOLIBRI_VISUAL_TESTS_BUILD_PATH),
59
- url: URL,
67
+ cwd: path.resolve(BUILD_PATH),
68
+ url: BASE_URL,
60
69
  reuseExistingServer: false,
61
70
  },
71
+ snapshotPathTemplate: `{snapshotDir}/theme-${THEME}/{arg}-{projectName}-{platform}{ext}`,
62
72
  });
package/src/index.js CHANGED
@@ -2,6 +2,7 @@ import child_process from 'node:child_process';
2
2
  import path from 'node:path';
3
3
  import { fileURLToPath, pathToFileURL } from 'url';
4
4
  import * as crypto from 'crypto';
5
+ import { readFile } from 'fs/promises';
5
6
  import * as fs from 'fs';
6
7
  import portfinder from 'portfinder';
7
8
  import * as process from 'process';
@@ -18,15 +19,8 @@ if (!tempDir) {
18
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
19
20
  const visualsTestModulePath = fileURLToPath(new URL('..', import.meta.url));
20
21
  const binaryPath = fileURLToPath(new URL('../node_modules/.bin', import.meta.url));
21
-
22
- let sampleReactPath = '../node_modules/@public-ui/sample-react';
23
- let backSteps = ``;
24
- let workingDir = null;
25
- do {
26
- const url = new URL(backSteps + sampleReactPath, import.meta.url);
27
- workingDir = fileURLToPath(url);
28
- backSteps += `../`;
29
- } 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));
30
24
 
31
25
  if (!fs.existsSync(workingDir)) {
32
26
  throw new Error('Could not find React Sample App package. Please install it with "npm install @public-ui/sample-react".');
@@ -35,12 +29,11 @@ if (!fs.existsSync(workingDir)) {
35
29
  const buildPath = path.join(tempDir, `kolibri-visual-testing-build-${crypto.randomUUID()}`);
36
30
  const rawPackageJsonPath = new URL(path.join(workingDir, 'package.json'), import.meta.url).href;
37
31
  const packageJsonPath = process.platform === 'win32' ? pathToFileURL(rawPackageJsonPath) : rawPackageJsonPath;
38
- const packageJson = await import(packageJsonPath, {
39
- assert: { type: 'json' },
40
- });
32
+ const packageJsonContent = await readFile(new URL(packageJsonPath, import.meta.url), 'utf8');
33
+ const packageJson = JSON.parse(packageJsonContent);
41
34
 
42
35
  console.log(`
43
- Building React Sample App (v${packageJson?.default?.version ?? '#.#.#'}) …`);
36
+ Building React Sample App (v${packageJson?.version ?? '#.#.#'}) …`);
44
37
 
45
38
  child_process.spawnSync('pnpm', ['run', 'build', '--', `--output-path="${buildPath}"`], {
46
39
  cwd: workingDir,
@@ -36,21 +36,31 @@ test.use({
36
36
  },
37
37
  });
38
38
 
39
- const blocklist = [];
40
-
41
39
  ROUTES.forEach((options, route) => {
42
- if (blocklist.includes(route)) {
40
+ // Skip unnecessary axe tests
41
+ if (options?.axe?.skip === true) {
43
42
  return;
44
43
  }
45
44
  test(`snapshot for ${route}`, async ({ page }, testInfo) => {
46
45
  const outputPath = rename(testInfo.outputDir);
47
- await page.goto(`/#${route}?hideMenus`, { waitUntil: 'networkidle' });
48
- if (options?.viewportSize) {
49
- await page.setViewportSize(options.viewportSize);
46
+ const hideMenusParam = `${route.includes('?') ? '&' : '?'}hideMenus`;
47
+ await page.goto(`/#${route}${hideMenusParam}`);
48
+ await page.waitForLoadState('networkidle');
49
+ await page.addStyleTag({
50
+ content: `
51
+ * {
52
+ transition: none !important;
53
+ animation: none !important;
54
+ }
55
+ `,
56
+ });
57
+ if (options?.snapshot?.viewportSize) {
58
+ await page.setViewportSize(options?.snapshot?.viewportSize);
50
59
  }
51
- if (options?.waitForTimeout) {
52
- await page.waitForTimeout(options.waitForTimeout);
60
+ if (options?.snapshot?.waitForTimeout) {
61
+ await page.waitForTimeout(options?.snapshot?.waitForTimeout);
53
62
  }
63
+
54
64
  await injectAxe(page);
55
65
  await checkA11y(
56
66
  page,
@@ -67,7 +77,7 @@ ROUTES.forEach((options, route) => {
67
77
  html: true,
68
78
  },
69
79
  },
70
- options?.axe?.skipFailures ?? true,
80
+ options?.axe?.skipFailures ?? false,
71
81
  'html',
72
82
  {
73
83
  outputDirPath: outputPath.replace(/\/[^/]+$/, ''),