vitest-dev / vitest

Next generation testing framework powered by Vite.
https://vitest.dev
MIT License
12.68k stars 1.14k forks source link

Cannot use vitest with NestJS and @types/fhir package #6342

Closed studiojms closed 1 month ago

studiojms commented 1 month ago

Describe the bug

When using vitest in a project that uses NestJS, Typescript, and the package @types/fhir (being imported into the project from fhir/r4), the package is not resolved by vitest and swc (direct dependency to use vitest with NestJS). I tried setting up vitest without swc, but it didn't work.

The error that happens when tests are run is:

Error: Failed to load url fhir/r4 (resolved id: fhir/r4) in ./src/observation/entities/observation.model.ts. Does the file exist?
 ❯ loadAndTransform node_modules/vite/dist/node/chunks/dep-NjL7WTE1.js:51787:17

Reproduction

Sample repo: https://github.com/studiojms/nest-vitest-fhir-sample The problem can be reproduced by running yarn test in this repo

System Info

System:
    OS: macOS 14.5
    CPU: (8) arm64 Apple M3
    Memory: 70.92 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.15.1 - ~/.nvm/versions/node/v20.15.1/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v20.15.1/bin/yarn
    npm: 10.7.0 - ~/.nvm/versions/node/v20.15.1/bin/npm
  Browsers:
    Brave Browser: 127.1.68.137
    Chrome: 127.0.6533.101
    Safari: 17.5
  npmPackages:
    @vitest/coverage-v8: ^2.0.5 => 2.0.5 
    vitest: ^2.0.5 => 2.0.5

Used Package Manager

yarn

Validations

sheremet-va commented 1 month ago

I don't think type files can actually be resolved by Vite if they are not annotated with a type keyword. Try this instead:

import type { Observation as FhirObservation } from 'fhir/r4';
studiojms commented 1 month ago

@sheremet-va I tried adding the type keyword as you suggested, and it fixed the problem. Thank you!

studiojms commented 1 month ago

Using the type keyword solved the main problem