TypeScript Import Fails in Vitest Unit Tests
Matthew-Benson opened this issue Β· comments
π bug report
Affected Rule
The issue is caused by the rule: nodejs_test
Is this a regression?
No, I don't think so. I tried multiple rules_nodejs v5.x.x versions with the same results.
Description
TS imports from "vitest" like import { describe } from "vitest";
is not working when running Vitest through Bazel.
The issue seems to only affect test files themselves. Imports in vite.config.ts
seem to be working correctly.
Any other local or npm imports in test files seem to work. The only package where imports fail are from importing from "vitest".
I have other tests with imports like
import { mount } from "@vue/test-utils";
import HelloWorld from "../HelloWorld.vue";
and they don't have import problems.
Interestingly, using Yarn to run vitest run
and bazel test //:unit_test
will yield different results - there's no problem importing from Vitest when running with Yarn despite everything else being the same.
π¬ Minimal Reproduction
https://github.com/Matthew-Benson/bazel_vitest_error_demo
π₯ Exception or Error
bazel test //:static_unit_test --test_output=all
==================== Test output for //:static_unit_test:
RUN v0.20.3 /home/user/.cache/bazel/_bazel_root/c310c63eebc309323fdaffd3dfd9b3e3/sandbox/processwrapper-sandbox/898/execroot/demo/bazel-out/k8-fastbuild/bin/static_unit_test.sh.runfiles/demo
β― static_import.test.ts (0 test)
β―β―β―β―β―β― Failed Suites 1 β―β―β―β―β―β―β―
FAIL static_import.test.ts [ static_import.test.ts ]
Error: No test suite found in file /home/user/.cache/bazel/_bazel_root/c310c63eebc309323fdaffd3dfd9b3e3/sandbox/processwrapper-sandbox/898/execroot/demo/bazel-out/k8-fastbuild/bin/static_unit_test.sh.runfiles/demo/static_import.test.ts
β―β―β―β―β―β―β―β―β―β―β―β―β―β―β―β―β―β―β―β―β―β―β―β―[1/1]β―
Test Files 1 failed (1)
Tests no tests
Start at 22:09:24
Duration 5.95s (setup 1ms, collect 786ms, tests 0ms)
================================================================================
I wanted to reproduce the issue without Bazel to get a better understanding of the problem, but I was unable to reproduce. I don't think this error is related to the contents, path, or symlinking of node_modules/vitest
. There is a bit of info about this debugging in the README.md
of the reproduction repo.
Vitest can be bad about hiding some underlying errors, and I suspect there's some problem with resolving the import { describe } from "vitest";
, which means test()
doesn't run and this test doesn't get registered with Vitest.
π Your Environment
Operating System:
Debian 11 (bullseye)
Output of bazel version
:
Bazelisk version: v1.11.0
Starting local Bazel server and connecting to it...
Build label: 5.2.0
Build target: bazel-out/k8-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Tue Jun 7 16:02:26 2022 (1654617746)
Build timestamp: 1654617746
Build timestamp as int: 1654617746
Rules_nodejs version:
5.5.2
Anything else relevant?
There is a workaround in this case that we can enable global Vitest imports in the test config, and there's an example of that in the reproduction repo, but it's an inconvenience to need global imports for test files and introduces some undesirable "magic" that makes it less clear what's going on.
@Matthew-Benson wow, I'm sorry we never replied to you, especially as you've done so much work already to isolate the problem.
As you may have seen, this repo isn't maintained anymore. I suspect this problem goes away with the newer rules_js since it is generally more compatible with existing node programs.
Are you still looking for help with this?
Yes, we still have this issue. I hadn't seen the new rules_js
announcement - let me try switching out the toolchain in the minimal reproduction example and see if that resolves the issue.
If you could leave this issue open for now I'd be happy to test it and report back.
How can I add a dependency to vitest for pnpm to the package.json
to test this? When using rules_nodejs
, we managed the package.json
with bazel's version of yarn loaded in the WORKSPACE with
load("@build_bazel_rules_nodejs//:index.bzl", "node_repositories", "yarn_install")
using something like:
function yarn { bazel run @yarn//:yarn -- "$@" }
yarn install my-module
or creating bazel rules that run yarn, but I don't see pnpm when running
bazel query //... | grep 'pnpm'
from the rules_js Vue example.
EDIT
Updating here for visibility in case someone else stumbles upon this - you can use bazel's managed pnpm via
bazel run -- @pnpm//:pnpm --dir $PWD "$@"
See Migrate - install pnpm and FAQ - Bazel managed pnpm.
For instance, to add vitest to the Vue example without downloading or creating node_modules:
function pnpm { bazel run -- @pnpm//:pnpm --dir $PWD "$@" }
pnpm install -D vitest -w --lockfile-only
@alexeagle please take a look at https://github.com/Matthew-Benson/bazel_rules_js_vitest_demo
The same issue seems to still persist with rules_js
. It's hard to say exactly why it fails on import {...} from "vitest"
when running with Bazel and not when running with pnpm. I suspect that Vitest is doing something strange with imports, but the internals of the project a bit opaque.
Would you like me to create an issue on the rules_js
repo?
This issue has been automatically marked as stale because it has not had any activity for 6 months. It will be closed if no further activity occurs in 30 days. Collaborators can add a "cleanup" or "need: discussion" label to keep it open indefinitely. Thanks for your contributions to rules_nodejs!
This issue was automatically closed because it went 30 days without any activity since it was labeled "Can Close?"