Skip to content

Commit 4379ef9

Browse files
tjonesloisakstenstrom
authored andcommitted
Add the vitest_test macro to the Bazel rules.
Vitest is a testing framework for vite applications. This is distributed with the NPM rules. This code exposes the vite testing framework for the Portal. This allows running the frontend vite framework tests as part of the larger bazel testing. This has been extracted from, and needs to be merged before #337
1 parent fe1fa62 commit 4379ef9

3 files changed

Lines changed: 57 additions & 16 deletions

File tree

frontend/BUILD.bazel

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_file")
22
load("@npm//:defs.bzl", "npm_link_all_packages")
33
load("@npm//frontend:vite/package_json.bzl", vite_bin = "bin")
44
load("//tools:protobuf.bzl", "get_proto_src")
5-
load(":vite.bzl", "vite_build")
5+
load(":vite.bzl", "vite_build", "vitest_test")
66

77
PROTO_FILES = {
88
"bazel_remote_execution": ("remote_execution", "build/bazel/remote/execution/v2", "@bazel_remote_apis//build/bazel/remote/execution/v2:remote_execution_proto"),
@@ -69,22 +69,30 @@ vite_bin.vite_binary(
6969
name = "vite_binary",
7070
)
7171

72+
FRONTEND_SRCS = glob(
73+
[
74+
"src/**",
75+
"public/**",
76+
],
77+
allow_empty = True,
78+
exclude = ["node_modules/**"],
79+
) + [
80+
"index.html",
81+
"package.json",
82+
"tsconfig.json",
83+
"vite.config.ts",
84+
":node_modules",
85+
]
86+
7287
vite_build(
7388
name = "dist",
74-
srcs = glob(
75-
[
76-
"src/**",
77-
"public/**",
78-
],
79-
allow_empty = True,
80-
exclude = ["node_modules/**"],
81-
) + [
82-
"index.html",
83-
"package.json",
84-
"tsconfig.json",
85-
"vite.config.ts",
86-
":node_modules",
87-
],
89+
srcs = FRONTEND_SRCS,
8890
visibility = ["//visibility:public"],
8991
vite_binary = ":vite_binary",
9092
)
93+
94+
vitest_test(
95+
name = "vitest_test",
96+
size = "small",
97+
srcs = FRONTEND_SRCS,
98+
)

frontend/vite.bzl

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("@bazel_lib//lib:copy_to_bin.bzl", _copy_to_bin = "copy_to_bin")
2+
load("@npm//frontend:vitest/package_json.bzl", _vitest_bin = "bin")
23

34
def _vite_build_rule_impl(ctx):
45
out_dir = ctx.actions.declare_directory(ctx.label.name)
@@ -49,11 +50,35 @@ def _vite_build_macro_impl(name, srcs, tags, **kwargs):
4950
name = name,
5051
srcs = [":{}".format(copy_to_bin_name)],
5152
tags = tags,
52-
**kwargs,
53+
**kwargs
5354
)
5455

5556
vite_build = macro(
5657
doc = "Builds a project with vite.",
5758
implementation = _vite_build_macro_impl,
5859
inherit_attrs = vite_build_rule,
5960
)
61+
62+
def vitest_test(name, srcs, args = ["run"], **kwargs):
63+
"""Runs the project's vitest test suite under Bazel.
64+
65+
Args:
66+
name: name of the test target.
67+
srcs: source files needed at test time: the test files themselves,
68+
the code under test, and vitest's configuration (vite.config.ts,
69+
tsconfig.json, package.json, node_modules).
70+
args: CLI args passed to `vitest`. Defaults to `["run"]` so the suite
71+
runs once and exits, instead of vitest's default watch mode.
72+
**kwargs: additional args forwarded to the underlying js_test rule,
73+
e.g. size or tags.
74+
"""
75+
_vitest_bin.vitest_test(
76+
name = name,
77+
data = srcs,
78+
args = args,
79+
# Run inside this directory (relative to the workspace root) so that
80+
# vitest picks up frontend/vite.config.ts and resolves the "@/" alias
81+
# and node_modules the same way `npm run test` does.
82+
chdir = native.package_name(),
83+
**kwargs
84+
)

frontend/vite.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ export default defineConfig({
5050
alias: {
5151
"@": path.resolve(__dirname, "./src"),
5252
},
53+
// Under Bazel, test/source files are exposed to the process via a
54+
// runfiles symlink tree. Vite's dev-server-style module graph (used by
55+
// vitest) follows symlinks to their real path by default, which resolves
56+
// outside that tree and isn't visible to the sandbox running the test.
57+
// Scoped to `vitest` (which sets process.env.VITEST) since enabling this
58+
// for `vite build` breaks Rolldown's resolution of pnpm's nested,
59+
// symlinked transitive dependencies.
60+
preserveSymlinks: !!process.env.VITEST,
5361
},
5462
test: {
5563
environment: "jsdom",

0 commit comments

Comments
 (0)