vitest-dev / vitest

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

Importing TypeScript file from JavaScript file with the .js extension fails. #5999

Open hornta opened 5 months ago

hornta commented 5 months ago

Describe the bug

When I import a TypeScript file from within a JavaScript file like so export { foo } from "./my-file.js"; (my-file is a .ts file on the FS). This should not error because TypeScript files can be imported using the JS extension.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-ivcrw5?file=test%2Fbasic.test.ts

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @vitest/ui: latest => 1.6.0 
    vite: latest => 5.3.2 
    vitest: latest => 1.6.0

Used Package Manager

npm

Validations

sheremet-va commented 5 months ago

This is Vite's limitation. It allows using .js extension for .ts files only if the importer (index.js in your case) is also a .ts file. If you disagree with this decision, you can start a discussion or open an issue in the vite repository.

wparsons-heins-globality commented 2 months ago

Hey, the biggest problem with this is that it differentiates from how typescript is handling the same situation, so code that compiles just fine cannot be tested in the same way. Which for me was a big reason to drop jest for vitest in the first place.

It sounds like I'd now have to convert every single .js file to .ts to make sure we're never importing a .ts as .js, even though this is exactly what I have to do so support NodeNext module resolution.

If Vite does decide to keep it this way, is there anything we can do to get around it without migrating .js to .ts?

nkeil commented 1 month ago

Unfortunate to see this not supported, but seems like it's being worked on here: https://github.com/vitejs/vite/issues/8993

vjpr commented 1 week ago

Doesn't work for .svelte files either.

https://github.com/vitejs/vite/blob/c45576ab18c42ce950ba76f0f389ad3dde2ab012/packages/vite/src/node/utils.ts#L298-L299