@public-ui/visual-tests 4.0.0-alpha.0 → 4.0.0-alpha.1
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 +36 -8
- package/package.json +20 -14
- package/playwright.config.js +19 -9
- package/src/index.js +16 -15
- package/tests/axe-snapshots.spec.js +21 -10
- package/tests/sample-app.routes.js +1017 -298
- package/tests/theme-snapshots.spec.js +53 -43
package/README.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# KoliBri - Visual Tests
|
|
2
2
|
|
|
3
|
+
Utilities for screenshot based regression testing of KoliBri themes.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@public-ui/components)
|
|
6
|
+
[](https://github.com/public-ui/kolibri/blob/main/LICENSE)
|
|
7
|
+
[](https://www.npmjs.com/package/@public-ui/visual-tests)
|
|
8
|
+
[](https://github.com/public-ui/kolibri/issues)
|
|
9
|
+
[](https://github.com/public-ui/kolibri/pulls)
|
|
10
|
+
[](https://bundlephobia.com/result?p=@public-ui/visual-tests)
|
|
11
|
+

|
|
12
|
+
|
|
3
13
|
## Motivation
|
|
4
14
|
|
|
5
15
|
The `KoliBri` Visual Tests provide a way to add visual regression testing to **theme** modules.
|
|
@@ -7,11 +17,23 @@ It takes screenshots of every component defined in the [React Sample App](https:
|
|
|
7
17
|
|
|
8
18
|
## Installation
|
|
9
19
|
|
|
20
|
+
It is recommended to configure NPM via `.npmrc`:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
# - npm
|
|
24
|
+
engine-strict=true
|
|
25
|
+
save-exact=true
|
|
26
|
+
|
|
27
|
+
# - pnpm
|
|
28
|
+
shamefully-hoist=true # this is required for the visual tests to work
|
|
29
|
+
workspace-concurrency=1
|
|
30
|
+
```
|
|
31
|
+
|
|
10
32
|
You can install the `KoliBri` Visual Tests with `npm`, `pnpm` or `yarn`:
|
|
11
33
|
|
|
12
34
|
```bash
|
|
13
35
|
npm i -D @public-ui/visual-tests
|
|
14
|
-
pnpm i -D @public-ui/visual-tests
|
|
36
|
+
pnpm i -D @public-ui/visual-tests # recommended
|
|
15
37
|
yarn add -D @public-ui/visual-tests
|
|
16
38
|
```
|
|
17
39
|
|
|
@@ -21,17 +43,23 @@ Add the following npm scripts to the theme's `package.json`:
|
|
|
21
43
|
|
|
22
44
|
```json
|
|
23
45
|
{
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
46
|
+
"scripts": {
|
|
47
|
+
"test": "THEME_MODULE=src/index THEME_EXPORT=THEME_NAME kolibri-visual-test",
|
|
48
|
+
"test-update": "THEME_MODULE=src/index THEME_EXPORT=THEME_NAME kolibri-visual-test --update-snapshots=changed"
|
|
49
|
+
}
|
|
28
50
|
}
|
|
29
51
|
```
|
|
30
52
|
|
|
31
|
-
|
|
32
|
-
|
|
53
|
+
### Environment variables
|
|
54
|
+
|
|
55
|
+
- `THEME_MODULE`: Define the relative path to the TypeScript module containing the theme definitions. Without file extension.
|
|
56
|
+
- `THEME_EXPERT`: Define the name of the export within the module. (e.g., `export const THEME_NAME = {/**/};`) Defaults to `default`.
|
|
57
|
+
- `KOLIBRI_VISUAL_TESTS_TIMEOUT`: Define the Playwright [test timeout](https://playwright.dev/docs/test-timeouts).
|
|
58
|
+
- `KOLIBRI_VISUAL_TESTS_EXPECT_TIMEOUT`: Define the Playwright [expect timeout](https://playwright.dev/docs/test-timeouts).
|
|
33
59
|
|
|
34
60
|
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
61
|
In the following runs, new screenshots will be compared to this reference.
|
|
36
62
|
|
|
37
|
-
To update the reference screenshots call `npm run test-update`.
|
|
63
|
+
To update the reference screenshots call `npm run test-update`.
|
|
64
|
+
|
|
65
|
+
For details on theming see the [default theme README](../../themes/default/README.md).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@public-ui/visual-tests",
|
|
3
|
-
"version": "4.0.0-alpha.
|
|
3
|
+
"version": "4.0.0-alpha.1",
|
|
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,22 @@
|
|
|
22
25
|
"kolibri-visual-test": "src/index.js"
|
|
23
26
|
},
|
|
24
27
|
"dependencies": {
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"@public-ui/sample-react": "4.0.0-alpha.0"
|
|
28
|
+
"axe-playwright": "2.2.2",
|
|
29
|
+
"portfinder": "1.0.38",
|
|
30
|
+
"serve": "14.2.5",
|
|
31
|
+
"@public-ui/sample-react": "4.0.0-alpha.1"
|
|
30
32
|
},
|
|
31
33
|
"devDependencies": {
|
|
32
|
-
"@babel/eslint-parser": "7.
|
|
33
|
-
"@babel/plugin-syntax-import-attributes": "7.
|
|
34
|
-
"@babel/preset-env": "7.
|
|
34
|
+
"@babel/eslint-parser": "7.28.4",
|
|
35
|
+
"@babel/plugin-syntax-import-attributes": "7.27.1",
|
|
36
|
+
"@babel/preset-env": "7.28.3",
|
|
37
|
+
"@playwright/test": "1.55.1",
|
|
35
38
|
"eslint": "8.57.1",
|
|
36
|
-
"knip": "5.
|
|
37
|
-
"prettier": "3.
|
|
39
|
+
"knip": "5.64.1",
|
|
40
|
+
"prettier": "3.6.2"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"@playwright/test": "1.55.1"
|
|
38
44
|
},
|
|
39
45
|
"files": [
|
|
40
46
|
"playwright.config.js",
|
|
@@ -43,8 +49,8 @@
|
|
|
43
49
|
],
|
|
44
50
|
"scripts": {
|
|
45
51
|
"format": "prettier --check src",
|
|
46
|
-
"lint": "eslint
|
|
47
|
-
"
|
|
48
|
-
"
|
|
52
|
+
"lint": "pnpm lint:eslint",
|
|
53
|
+
"lint:eslint": "eslint src",
|
|
54
|
+
"unused": "knip"
|
|
49
55
|
}
|
|
50
56
|
}
|
package/playwright.config.js
CHANGED
|
@@ -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
|
-
|
|
6
|
-
const
|
|
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
|
-
|
|
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(
|
|
20
|
+
snapshotDir: path.join(CWD, 'snapshots'),
|
|
16
21
|
// snapshotPathTemplate: '',
|
|
17
|
-
outputDir: path.join(
|
|
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:
|
|
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:
|
|
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(
|
|
59
|
-
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,11 +2,13 @@ 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';
|
|
9
|
+
import os from 'node:os';
|
|
8
10
|
|
|
9
|
-
const tempDir = process.env.RUNNER_TEMP || process.env.TMPDIR; // TODO: Check on Windows
|
|
11
|
+
const tempDir = process.env.RUNNER_TEMP || process.env.TMPDIR || os.tmpdir(); // TODO: Check on Windows
|
|
10
12
|
|
|
11
13
|
if (!process.env.THEME_MODULE) {
|
|
12
14
|
throw new Error('Environment variable THEME_MODULE not specified.');
|
|
@@ -18,15 +20,8 @@ if (!tempDir) {
|
|
|
18
20
|
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
21
|
const visualsTestModulePath = fileURLToPath(new URL('..', import.meta.url));
|
|
20
22
|
const binaryPath = fileURLToPath(new URL('../node_modules/.bin', import.meta.url));
|
|
21
|
-
|
|
22
|
-
|
|
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) !== '/');
|
|
23
|
+
const sampleReactPackageJsonPath = import.meta.resolve('@public-ui/sample-react/package.json');
|
|
24
|
+
const workingDir = fileURLToPath(path.dirname(sampleReactPackageJsonPath));
|
|
30
25
|
|
|
31
26
|
if (!fs.existsSync(workingDir)) {
|
|
32
27
|
throw new Error('Could not find React Sample App package. Please install it with "npm install @public-ui/sample-react".');
|
|
@@ -35,19 +30,25 @@ if (!fs.existsSync(workingDir)) {
|
|
|
35
30
|
const buildPath = path.join(tempDir, `kolibri-visual-testing-build-${crypto.randomUUID()}`);
|
|
36
31
|
const rawPackageJsonPath = new URL(path.join(workingDir, 'package.json'), import.meta.url).href;
|
|
37
32
|
const packageJsonPath = process.platform === 'win32' ? pathToFileURL(rawPackageJsonPath) : rawPackageJsonPath;
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
});
|
|
33
|
+
const packageJsonContent = await readFile(new URL(packageJsonPath, import.meta.url), 'utf8');
|
|
34
|
+
const packageJson = JSON.parse(packageJsonContent);
|
|
41
35
|
|
|
42
36
|
console.log(`
|
|
43
|
-
Building React Sample App (v${packageJson?.
|
|
37
|
+
Building React Sample App (v${packageJson?.version ?? '#.#.#'}) …`);
|
|
44
38
|
|
|
45
|
-
child_process.spawnSync('pnpm', ['run', 'build',
|
|
39
|
+
const buildResult = child_process.spawnSync('pnpm', ['run', 'build', `--outDir="${buildPath}"`], {
|
|
46
40
|
cwd: workingDir,
|
|
47
41
|
encoding: 'utf-8',
|
|
48
42
|
shell: true,
|
|
49
43
|
});
|
|
50
44
|
|
|
45
|
+
if (buildResult.status !== 0) {
|
|
46
|
+
console.log('Build status:', buildResult.status);
|
|
47
|
+
console.log('Build stdout:', buildResult.stdout);
|
|
48
|
+
console.log('Build stderr:', buildResult.stderr);
|
|
49
|
+
console.log('Build error:', buildResult.error);
|
|
50
|
+
}
|
|
51
|
+
|
|
51
52
|
console.log(`React Sample App build finished. Directory:`, buildPath);
|
|
52
53
|
|
|
53
54
|
void (async () => {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { test } from '@playwright/test';
|
|
2
2
|
import { checkA11y, injectAxe } from 'axe-playwright';
|
|
3
3
|
import { ROUTES } from './sample-app.routes.js';
|
|
4
|
+
import process from 'process';
|
|
4
5
|
|
|
5
6
|
const themeName = (process.env.THEME_EXPORT || 'default').toLocaleLowerCase();
|
|
6
7
|
const rename = (snapshotName) => {
|
|
@@ -36,21 +37,31 @@ test.use({
|
|
|
36
37
|
},
|
|
37
38
|
});
|
|
38
39
|
|
|
39
|
-
const blocklist = [];
|
|
40
|
-
|
|
41
40
|
ROUTES.forEach((options, route) => {
|
|
42
|
-
|
|
41
|
+
// Skip unnecessary axe tests
|
|
42
|
+
if (options?.axe?.skip === true || process.argv.includes('--update-snapshots')) {
|
|
43
43
|
return;
|
|
44
44
|
}
|
|
45
45
|
test(`snapshot for ${route}`, async ({ page }, testInfo) => {
|
|
46
46
|
const outputPath = rename(testInfo.outputDir);
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
47
|
+
const hideMenusParam = `${route.includes('?') ? '&' : '?'}hideMenus`;
|
|
48
|
+
await page.goto(`/#${route}${hideMenusParam}`);
|
|
49
|
+
await page.waitForLoadState('networkidle');
|
|
50
|
+
await page.addStyleTag({
|
|
51
|
+
content: `
|
|
52
|
+
* {
|
|
53
|
+
transition: none !important;
|
|
54
|
+
animation: none !important;
|
|
55
|
+
}
|
|
56
|
+
`,
|
|
57
|
+
});
|
|
58
|
+
if (options?.snapshot?.viewportSize) {
|
|
59
|
+
await page.setViewportSize(options?.snapshot?.viewportSize);
|
|
50
60
|
}
|
|
51
|
-
if (options?.waitForTimeout) {
|
|
52
|
-
await page.waitForTimeout(options
|
|
61
|
+
if (options?.snapshot?.waitForTimeout) {
|
|
62
|
+
await page.waitForTimeout(options?.snapshot?.waitForTimeout);
|
|
53
63
|
}
|
|
64
|
+
|
|
54
65
|
await injectAxe(page);
|
|
55
66
|
await checkA11y(
|
|
56
67
|
page,
|
|
@@ -67,12 +78,12 @@ ROUTES.forEach((options, route) => {
|
|
|
67
78
|
html: true,
|
|
68
79
|
},
|
|
69
80
|
},
|
|
70
|
-
options?.axe?.skipFailures ??
|
|
81
|
+
true, // options?.axe?.skipFailures ?? false,
|
|
71
82
|
'html',
|
|
72
83
|
{
|
|
73
84
|
outputDirPath: outputPath.replace(/\/[^/]+$/, ''),
|
|
74
85
|
outputDir: `axe-${themeName}`,
|
|
75
|
-
reportFileName: `${route.replace(
|
|
86
|
+
reportFileName: `${route.replace(/[/?]/g, '-')}.html`,
|
|
76
87
|
},
|
|
77
88
|
);
|
|
78
89
|
});
|