Skip to content

Commit 3a53dd6

Browse files
committed
minor #3010 Configure Vitest for unit and browser tests (Kocal)
This PR was merged into the 2.x branch. Discussion ---------- Configure Vitest for unit and browser tests | Q | A | ------------- | --- | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Docs? | no <!-- required for new features --> | Issues | Fix #3008 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - For new features, provide some code snippets to help understand usage. - Features and deprecations must be submitted against branch main. - Update/add documentation as required (we can help!) - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Moved all existing assets unit tests under `assets/test/unit`, in order to introduce `assets/test/browser` for pure E2E tests using a real Symfony app (in a future PR) and real browsers. I slightly improved the `test_package.sh` for peerDependencies/importmap versions checks. Next steps are [#3009](#3009) Commits ------- 01bea65 Configure Vitest for unit and browser tests (use `@puppeteer`/browsers and webdriverio)
2 parents 5eb1153 + 01bea65 commit 3a53dd6

File tree

135 files changed

+1954
-457
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+1954
-457
lines changed

.gitattributes

Lines changed: 0 additions & 5 deletions
This file was deleted.

.github/workflows/browser-tests.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Browser Tests
2+
3+
defaults:
4+
run:
5+
shell: bash
6+
7+
on:
8+
push:
9+
paths-ignore:
10+
- 'src/*/doc/**'
11+
- 'src/**/*.md'
12+
- 'ux.symfony.com/**'
13+
- '.github/workflows/app-tests.yaml'
14+
- '.github/workflows/unit-tests.yaml'
15+
pull_request:
16+
paths-ignore:
17+
- 'src/*/doc/**'
18+
- 'src/**/*.md'
19+
- 'ux.symfony.com/**'
20+
- '.github/workflows/app-tests.yaml'
21+
- '.github/workflows/unit-tests.yaml'
22+
23+
jobs:
24+
js:
25+
runs-on: ubuntu-latest
26+
steps:
27+
- uses: actions/checkout@v4
28+
- run: npm i -g corepack && corepack enable
29+
- uses: actions/setup-node@v4
30+
with:
31+
node-version-file: '.nvmrc'
32+
cache: 'pnpm'
33+
cache-dependency-path: |
34+
pnpm-lock.yaml
35+
package.json
36+
src/**/package.json
37+
- run: pnpm install --frozen-lockfile
38+
39+
- name: Install browsers
40+
run: node ./bin/get_browsers.mjs
41+
42+
# TODO: Install the E2E app + run webserver
43+
- run: pnpm run test:browser

.github/workflows/functional-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Deprecated, will be removed in favor of browser-tests.yml
12
name: Functional Tests
23

34
defaults:

.github/workflows/unit-tests.yaml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,21 +143,16 @@ jobs:
143143
fi
144144
js:
145145
runs-on: ubuntu-latest
146-
strategy:
147-
fail-fast: false
148-
matrix:
149-
node-version: [ '18.x', '20.x', '22.x', '24.x' ]
150146
steps:
151147
- uses: actions/checkout@v4
152148
- run: npm i -g corepack && corepack enable
153149
- uses: actions/setup-node@v4
154150
with:
155-
node-version: ${{ matrix.node-version }}
151+
node-version-file: '.nvmrc'
156152
cache: 'pnpm'
157153
cache-dependency-path: |
158154
pnpm-lock.yaml
159155
package.json
160156
src/**/package.json
161157
- run: pnpm install --frozen-lockfile
162-
- run: pnpm exec playwright install chromium
163-
- run: pnpm run test
158+
- run: pnpm run test:unit

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
vendor/
21
node_modules
32

43
.doctor-rst.cache
@@ -7,3 +6,5 @@ node_modules
76

87
/composer.lock
98
/vendor
9+
10+
/browsers

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
22.11
1+
22.18

CONTRIBUTING.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ To set up the development environment, you need the following tools:
3939

4040
- [PHP](https://www.php.net/downloads.php) 8.1 or higher
4141
- [Composer](https://getcomposer.org/download/)
42-
- [Node.js](https://nodejs.org/en/download/package-manager) 22.11 or higher
42+
- [Node.js](https://nodejs.org/en/download/package-manager) 22.18 or higher
4343
- [Corepack](https://github.com/nodejs/corepack)
4444
- [PNPM](https://pnpm.io/) 10.13 or higher
4545

@@ -82,12 +82,16 @@ To help you with assets, you can run the following commands in a specific packag
8282
- `pnpm run build`: build (compile) assets from the package,
8383
- `pnpm run watch`: watch for modifications and rebuild assets from the package,
8484
- `pnpm run test`: run the tests from the package,
85+
- `pnpm run test:unit`: run the Unit tests from the package,
86+
- `pnpm run test:browser`: run the Browser tests from the package,
8587
- `pnpm run check`: run the formatter, linter, and sort imports, and fails if any modifications
8688
- `pnpm run check --write`: run the formatter, linter, imports sorting, and write modifications
8789

8890
Thanks to [PNPM Workspaces](https://pnpm.io/workspaces), you can also run these commands from the root directory of the project:
8991
- `pnpm run build`: build (compile) assets from **all** packages,
9092
- `pnpm run test`: run the tests from **all** packages,
93+
- `pnpm run test:unit`: run the Unit tests from **all** packages,
94+
- `pnpm run test:browser`: run the Browser tests from **all** packages,
9195
- `pnpm run check`: run the formatter, linter, and sort imports for **all** packages, and fails if any modifications
9296
- `pnpm run check --write`: run the formatter, linter, imports sorting for **all** packages, and write modifications
9397

@@ -112,7 +116,7 @@ docker run --rm -it -e DOCS_DIR='/docs' -v ${PWD}:/docs oskarstark/doctor-rst -
112116
```shell
113117
$ git checkout 2.x && \
114118
git fetch upstream && \
115-
git rebase upstream/2.x && \
119+
git reset --hard upstream/2.x && \
116120
git push origin 2.x
117121
```
118122

bin/get_browsers.mjs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import * as path from 'node:path';
2+
import {
3+
Browser,
4+
BrowserTag,
5+
detectBrowserPlatform,
6+
install as installBrowser,
7+
resolveBuildId,
8+
} from '@puppeteer/browsers';
9+
10+
const platform = detectBrowserPlatform();
11+
const installBrowserCommonOpts = {
12+
platform,
13+
cacheDir: path.join(import.meta.dirname, '../browsers'),
14+
downloadProgressCallback: 'default',
15+
};
16+
17+
// Lowest versions are computed from "defaults and fully supports es6-module" query,
18+
// see https://browsersl.ist/#q=defaults+and+fully+supports+es6-module
19+
20+
export const browsers = {
21+
'chrome@lowest': await installBrowser({
22+
...installBrowserCommonOpts,
23+
browser: Browser.CHROME,
24+
// The lowest version where:
25+
// - Chrome and associated Chromedriver could easily be downloaded
26+
// - there is no compatibility issues like "WebDriver Bidi command \"session.subscribe\" failed with error"
27+
// - there is no timeout issues when requesting Vitest webserver
28+
// @see https://raw.githubusercontent.com/GoogleChromeLabs/chrome-for-testing/refs/heads/main/data/known-good-versions-with-downloads.json
29+
buildId: '130.0.6669.0',
30+
}),
31+
'chrome@latest': await installBrowser({
32+
...installBrowserCommonOpts,
33+
browser: Browser.CHROME,
34+
buildId: await resolveBuildId(Browser.CHROME, platform, BrowserTag.STABLE),
35+
}),
36+
'firefox@lowest': await installBrowser({
37+
...installBrowserCommonOpts,
38+
browser: Browser.FIREFOX,
39+
buildId: 'stable_128.0',
40+
}),
41+
'firefox@latest': await installBrowser({
42+
...installBrowserCommonOpts,
43+
browser: Browser.FIREFOX,
44+
buildId: await resolveBuildId(Browser.FIREFOX, platform, BrowserTag.STABLE),
45+
}),
46+
};
47+
48+
if (import.meta.main) {
49+
console.log('Browsers installed:', browsers);
50+
}

bin/test_package.sh

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ PROJECT_DIR=$(dirname "$SCRIPT_DIR")
99
# Flag to track if any test fails
1010
all_tests_passed=true
1111

12-
# Check if we have at least one argument
13-
if [ $# -eq 0 ]
14-
then
15-
echo "No arguments supplied, please provide the package's path."
12+
# Check if we have enough arguments
13+
if [ $# -ne 2 ]; then
14+
echo "No arguments supplied, please provide the package's path and the test type (e.g. --unit or --browser)"
1615
fi
1716

1817
# Check if jq is installed
@@ -22,46 +21,58 @@ if ! command -v jq &> /dev/null; then
2221
fi
2322

2423
runTestSuite() {
25-
echo -e "Running tests for $workspace...\n"
26-
pnpm exec vitest --run || { all_tests_passed=false; }
24+
local testProject="$1"
25+
if [ "$testProject" != "unit" ] && [ "$testProject" != "browser" ]; then
26+
echo "Unknown test project: $testProject. Please use 'unit' or 'browser'."
27+
exit 1
28+
fi
29+
30+
echo -e "🧪 Running $testProject tests for $workspace...\n"
31+
pnpm exec vitest --run --config "vitest.config.$testProject.mjs" || { all_tests_passed=false; }
2732
}
2833

2934
processWorkspace() {
3035
local location="$1"
36+
local testProject="$2"
3137

3238
if [ ! -d "$location" ]; then
33-
echo "No directory found at $location"
39+
echo "No directory found at $location"
3440
return
3541
fi
3642

3743
package_json_path="$location/package.json"
3844
if [ ! -f "$package_json_path" ]; then
39-
echo "No package.json found at $package_json_path"
45+
echo "No package.json found at $package_json_path"
4046
return
4147
fi
4248

4349
workspace=$(jq -r '.name' "$package_json_path")
4450
if [ -z "$workspace" ]; then
45-
echo "No name found in package.json at $package_json_path"
51+
echo "No name found in package.json at $package_json_path"
4652
return
4753
fi
4854

49-
echo -e "Processing workspace $workspace at location $location...\n"
55+
echo -e "Processing workspace $workspace at location $location...\n"
5056

51-
echo "Checking '$package_json_path' for peerDependencies and importmap dependencies to have the same version"
57+
echo "⚙️ Checking '$package_json_path' for peerDependencies and importmap dependencies to have the same version"
5258
deps=$(jq -r '.peerDependencies | keys[]' "$package_json_path")
5359
for library in $deps; do
5460
version=$(jq -r ".peerDependencies.\"$library\"" "$package_json_path")
55-
importmap_version=$(jq -r ".symfony.importmap.\"$library\"" "$package_json_path")
61+
importmap_version=$(jq -r ".symfony.importmap.\"$library\" | if type == \"string\" then . else .version end" "$package_json_path")
62+
63+
if [ "$importmap_version" == null ]; then
64+
echo " ⚠ No importmap version found for $library in $package_json_path, skipping..."
65+
continue
66+
fi
5667

5768
if [ "$version" != "$importmap_version" ]; then
58-
echo " -> Version mismatch for $library: $version (peerDependencies) vs $importmap_version (importmap)"
59-
echo " -> You need to match the version of the \"peerDependency\" with the version in the \"importmap\""
60-
exit
69+
echo " Version mismatch for $library: $version (peerDependencies) vs $importmap_version (importmap)"
70+
echo " You need to match the version of the \"peerDependency\" with the version in the \"importmap\""
71+
exit 1
6172
fi
6273
done
6374

64-
echo "Checking '$package_json_path' for peerDependencies with multiple versions defined"
75+
echo "⚙️ Checking '$package_json_path' for peerDependencies with multiple versions defined"
6576
deps_with_multiple_versions=$(jq -r '.peerDependencies | to_entries[] | select(.value | contains("||")) | .key' "$package_json_path")
6677

6778
if [ -n "$deps_with_multiple_versions" ]; then
@@ -78,20 +89,26 @@ processWorkspace() {
7889
echo -e " - Install $library@$trimmed_version for $workspace\n"
7990
pnpm add "$library@$trimmed_version" --save-peer --filter "$workspace"
8091

81-
runTestSuite
92+
runTestSuite "$testProject"
8293
fi
8394
done
8495
done
8596

8697
echo " -> Reverting version changes from $package_json_path"
87-
git checkout -- "$package_json_path"
98+
git checkout -- "$package_json_path" "$PROJECT_DIR/pnpm-lock.yaml"
8899
else
89100
echo -e " -> No peerDependencies found with multiple versions defined\n"
90-
runTestSuite
101+
runTestSuite "$testProject"
91102
fi
92103
}
93104

94-
processWorkspace "$(realpath "$PWD/$1")"
105+
case "$2" in
106+
--unit) testProject="unit" ;;
107+
--browser) testProject="browser" ;;
108+
*) echo "Unknown test type: $2. Please use --unit or --browser."; exit 1 ;;
109+
esac
110+
111+
processWorkspace "$(realpath "$PWD/$1")" "$testProject"
95112

96113
# Check the flag at the end and exit with code 1 if any test failed
97114
if [ "$all_tests_passed" = false ]; then

biome.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
"**/*.json",
66
"**/*.md",
77
"**/bin/**/*.js",
8+
"**/src/**/assets/*.mjs",
89
"**/src/**/assets/src/**",
910
"**/src/**/assets/test/**",
1011
"!**/composer.json",
1112
"!**/vendor",
1213
"!**/package.json",
1314
"!**/node_modules",
1415
"!**/var",
15-
"!**/dist"
16+
"!**/dist",
17+
"!**/browsers"
1618
]
1719
},
1820
"linter": {

0 commit comments

Comments
 (0)