-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
fix: remove "type": "module"
from vitest/package.json
#1411
Conversation
✅ Deploy Preview for vitest-dev ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
I don't think this would be a long-term solution. Isn't that a config issue, or an issue of TypeScript? |
Currrently, using Isn't using |
Hello, I think this issue is related to this one: #1417 I'm also using In my opinion, if you want to write your package as ESM with I'm working on a little monorepo which is ESM first with TypeScript for experiment/demo purpose and I've already encountered the same issue here: tinyhttp/tinyhttp#359 edit: Sorry I didn't realize in your case you are using |
I think so too. We had an issue that was fixed by #1417, but I think that's it? Removing |
It seems these two are interchangable. Am I wrong? |
I don't see it as TypeScript's issue. The Since Vitest does support TypeScript projects that compile to CJS (as well as TS projects that compile to ESM but don't have |
You are not wrong. Truth be told, to run Vitest we don't even need it to be
Thank you for your explanation. This is more a configure issue. You have a single
As you might have guessed I prefer the second option, since for us DX has the highest priority. |
Maybe there is a third:
|
That was the first thing I tried, but it doesn't help. The |
I guess it might not work bellow TypeScript 4.7, where they added support for |
I tried not using This happens when I run
|
It doesn't work in 4.7+ either. TypeScript detects |
Yeah, what I said is only true for By the way, this is just technicalities, we probably should have appropriate extensions for all files. |
Then this is a bug in TypeScript, if my assumptions about transforming |
I've opened an issue on the TypeScript repo |
This is a rare enough edge case that Vitest should just work around it, instead of expecting TypeScript to support it. It's extremely rare that a package is ESM only but still allows CJS modules to import it. Of course, in reality, test files are not really CJS modules, but nonetheless, that's what TypeScript thinks. The work around isn't a big deal anyway, so the TypeScript team will probably "kick the can down the road" instead of quickly pushing a fix. |
Why is Vitest not compiling its sources to |
It reduces installation size/speed. Unless we wanted to support older versions of Node, there's no reason to publish CJS files. And if we did do that, we would have to remove |
Can you set up a minimal reproduction so I can try different solutions? Thanks |
@antfu https://github.com/aleclarson/repro/tree/vitest-nodenext
|
That's a weird situation. Ok I am fine with it and let's have it in the next minor as a breaking change. Can you also help to update other packages in the monorepo (namely |
Just noticed this issue, and I think this is a configuration issue than a Vitest/Typescript issue. According to the ts docs, A solution would be to use PS: I'm not sure why this PR fixes the issue, I think it should still error as before, maybe a bug in TypeScript. |
The issue is that for some people it is not possible to move their codebase to ESM, but tests still work, so we are going with the better DX than better configuration.
I too think it's a bug, I think it should've read an exports field and see its ESM, which mean |
The solution for now won't last though if TypeScript eventually fixes the issue. My thought was that TypeScript should still error out even after this PR's changes (currently it doesn't error, which feels like a bug). IIUC the config workaround is only needed for I think it's a ticking time-bomb though if we keep this change. The DX will bite back in the future. |
Looks like this can be reverted. There's a better solution that avoids the time-bomb issue that @bluwy mentioned. |
Projects whose
tsconfig.json
containsmodule
ormoduleResolution
value ofnodenext
ornode16
cannot importvitest
in their*.spec.ts
files without getting a TypeScript error about Vitest being incompatible with CommonJS.But in this case, TypeScript is reporting a false positive. Vitest compiles the
*.spec.ts
files, so it's not a problem that Vitest only has an ESM entry point.Therefore, we should remove
"type": "module"
fromvitest/package.json
to appease TypeScript. As a result, we also need to use.mjs
extensions for Vitest's compiled files, since they useimport
/export
syntax.Closes #325