Skip to content

vitest setup file cannot import library from same workspace #31921

@jnizet

Description

@jnizet

Command

test

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

I would like to define custom vitest matchers (or locators, or whatever) in a common library of a CLI workspace in order to be able to use them in all the other projects of the workspace.

But if I try to import the library from the vitest setup file:

import 'common';

the tests fail with a vitest error pointing at the import statement inside the setup file.

Importing the library from inside the spec files works fine (but is far from being ideal).
Copying and pasting the code of the library inside the setup file works fine too.

Minimal Reproduction

https://stackblitz.com/edit/stackblitz-starters-j6wcm5mf?file=demo%2Fprojects%2Fdemo%2Fsrc%2Fvitest-setup.ts

  • cd demo
  • npm i
  • ng build --project common
  • ng test --watch=false --project demo

At this point, the unique test, using a custom assertion, should pass. That's because the custom assertion is defined directly inside the vitest-setup.ts file.

Now edit this file by uncommenting the import 'common'; line and removing the remaining of the file, and rerun ng test --watch=false --project demo, and you'll get the error from vitest.

Exception or Error

projects/demo/src/app/app.spec.ts [ projects/demo/src/app/app.spec.ts ]
Error: Failed to resolve import "common" from "projects/demo/src/vitest-setup.ts". Does the file exist?
  Plugin: vite:import-analysis
  File: /home/projects/stackblitz-starters-j6wcm5mf/demo/projects/demo/src/vitest-setup.ts:4:7
  1  |  import "common";
     |          ^
  2  |  
 ❯ TransformPluginContext._formatLog node_modules/vite/dist/node/chunks/config.js:29717:43
 ❯ TransformPluginContext.error node_modules/vite/dist/node/chunks/config.js:29714:14
 ❯ normalizeUrl node_modules/vite/dist/node/chunks/config.js:27837:18
 ❯ eval node_modules/vite/dist/node/chunks/config.js:27895:32
 ❯ TransformPluginContext.transform node_modules/vite/dist/node/chunks/config.js:27863:4
 ❯ EnvironmentPluginContainer.transform node_modules/vite/dist/node/chunks/config.js:29515:14
 ❯ loadAndTransform node_modules/vite/dist/node/chunks/config.js:23386:26

Your Environment

Angular CLI       : 21.0.0
Angular           : 21.0.0
Node.js           : 20.19.1
Package Manager   : npm 10.8.2
Operating System  : linux x64

┌───────────────────────────┬───────────────────┬───────────────────┐
│ Package                   │ Installed Version │ Requested Version │
├───────────────────────────┼───────────────────┼───────────────────┤
│ @angular/build            │ 21.0.0            │ ^21.0.0           │
│ @angular/cli              │ 21.0.0            │ ^21.0.0           │
│ @angular/common           │ 21.0.0            │ ^21.0.0           │
│ @angular/compiler         │ 21.0.0            │ ^21.0.0           │
│ @angular/compiler-cli     │ 21.0.0            │ ^21.0.0           │
│ @angular/core             │ 21.0.0            │ ^21.0.0           │
│ @angular/forms            │ 21.0.0            │ ^21.0.0           │
│ @angular/platform-browser │ 21.0.0            │ ^21.0.0           │
│ @angular/router           │ 21.0.0            │ ^21.0.0           │
│ ng-packagr                │ 21.0.0            │ ^21.0.0           │
│ rxjs                      │ 7.8.2             │ ~7.8.0            │
│ typescript                │ 5.9.3             │ ~5.9.2            │
│ vitest                    │ 4.0.13            │ ^4.0.8            │
└───────────────────────────┴───────────────────┴───────────────────┘

Anything else relevant?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions