Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
6 tasks done
Jason3S opened this issue Aug 20, 2023 · 8 comments
Open
6 tasks done

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

Jason3S opened this issue Aug 20, 2023 · 8 comments

Comments

@Jason3S
Copy link

Jason3S commented Aug 20, 2023

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
Copy link

stackblitz bot commented Aug 20, 2023

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

@sheremet-va
Copy link
Member

sheremet-va commented Aug 21, 2023

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
Copy link
Author

Jason3S commented Aug 21, 2023

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
Copy link

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
Copy link

Mrtenz commented Apr 9, 2024

Is there a workaround for supporting ".cts"?

@christian-bromann
Copy link
Contributor

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
Copy link

taewanseoul commented Nov 20, 2024

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

@sheremet-va
Copy link
Member

sheremet-va commented Nov 20, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants