vitest-dev / vitest

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

Fails to run / parse `.cts` source files. #3987

Open Jason3S opened 1 year ago

Jason3S commented 1 year ago

Describe the bug

While working with an application with a mix of .mts and .cts files, vitest fails to correctly parse the .cts source files. It seems to be treating them as .cjs files.

image

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-xrug9w?file=README.md

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: 16.20.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.4.2 - /usr/local/bin/npm
    pnpm: 8.6.10 - /usr/local/bin/pnpm
  npmPackages:
    @vitest/ui: latest => 0.34.2 
    vite: latest => 4.4.9 
    vitest: latest => 0.34.2 

Used Package Manager

npm

Validations

stackblitz[bot] commented 1 year ago

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

sheremet-va commented 1 year ago

Interesting. Vite doesn't process .cts files at all. I guess we should provide our own plugin to transform this?

I am not sure if we should even support generated CJS, since we cannot process require calls there (if in your example you had import test from './test.cts' it would've been transformed into a require('./test.cjs'), and Vitest doesn't intercept require at all because Vite's plugin system is asynchronous, so it would try to run TypeScript file as a JavaScript file and we wouldn't be able to do anything.

I would recommend running tests on already transformed files in your case.

Jason3S commented 1 year ago

Note: One workaround I found was to compile the .cts files in place (into the same dir) to .cjs file. But this breaks down when trying to generate a coverage report.

Example Setup: https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell/src/lib

wijionejs commented 1 year ago

Note: One workaround I found was to compile the .cts files in place (into the same dir) to .cjs file. But this breaks down when trying to generate a coverage report.

Example Setup: https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell/src/lib

Here is a library that may help you to intercept and mock require calls: vitest-mock-require

Mrtenz commented 7 months ago

Is there a workaround for supporting ".cts"?

christian-bromann commented 5 months ago

In case this is useful to someone, here is a simple Stackblitz example: https://stackblitz.com/edit/vitest-dev-vitest-zbjtkh?file=src%2Fbasic.cts

taewanseoul commented 1 week ago

Any update regarding parsing .cts source files? (couldn't work around getting RollupError from parsing typescript syntax in .cts file...)

sheremet-va commented 1 week ago

There are no plans to support .cts files out of the box.