-
-
Notifications
You must be signed in to change notification settings - Fork 69
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
Typescript, Vitetest, and Autoload #366
Comments
@mcollina ping png :) |
Essentially vitest + autoload + ts are incompatible with each other and nobody found a way to make it work. It seems vitest is doing something interesting to support its own magic which is incompatible with our magic. PRs are absolutely welcome. |
Challenge accepted. |
@mcollina So... this was pretty simple.. ➜ fastify-autoload git:(master) npx npm-check-updates -u --enginesNode
Upgrading /Users/user/Code/fastify-autoload/package.json
[====================] 25/25 100%
@fastify/pre-commit ^2.0.2 → ^2.1.0
@fastify/url-data ^5.0.0 → ^5.4.0
@swc-node/register ^1.5.1 → ^1.8.0
@swc/core ^1.2.129 → ^1.4.2
@types/jest ^29.0.0 → ^29.5.12
@types/node ^20.1.0 → ^20.11.24
@types/tap ^15.0.5 → ^15.0.11
esbuild ^0.19.2 → ^0.20.1
esbuild-register ^3.4.1 → ^3.5.0
fastify ^4.0.0-rc.2 → ^4.26.2
fastify-plugin ^4.0.0 → ^4.5.1
jest ^28.1.3 → ^29.7.0
standard ^17.0.0 → ^17.1.0
tap ^16.0.0 → ^18.7.0
ts-jest ^28.0.7 → ^29.1.2
ts-node ^10.4.0 → ^10.9.2
tsd ^0.29.0 → ^0.30.7
tsm ^2.2.1 → ^2.3.0
tsx ^3.7.1 → ^4.7.1
typescript ^5.0.2 → ^5.3.3
vite ^5.0.0 → ^5.1.4
vitest ^0.34.1 → ^1.3.1 I then attached my code locally, and it ran: So, do you want to accept this many changes, or do you want me to pair it down to the minimum needed to pass both unit testing here and in my script? |
Please send the PR :) |
Roger. Let me get the unit tests working. Some of the unit test functionality has "warnings" due to the impending version 5 of Fastify. |
Well. Try to use Looks a bit related to each other. |
I am having the same issue. |
run tests with |
Can you provide a simple repo as a mvce? This actually should make it easier to fix, if you say that using env vars make it work. It means that the underlying code is working but the detection for vitest is not. If you provide a simple repo it becomes super easy to debug and patch it. |
I am closing in favor of #372 |
@mcollina, this issue still persists but I have a fix for it. The two magics are compatible at last! The issueBy default, vite-node transform, which Vitest uses to do its magic, is lazy and doesn't run for the code in the node_modules folder. The problem arises when you want to test import/require packages with the TS extension imported from the node_modules folder like @fastify/autoload does. TypeError: Unknown file extension ".ts" for /autoload-vitest/src/plugins/my-plugin.ts
{ code: 'ERR_UNKNOWN_FILE_EXTENSION' } The solutionThe way to fix this is to ensure the Node.js process that runs @fastify/autoload can handle TypeScript files.
test": "NODE_OPTIONS='--import tsx' vitest run"
test: {
server: {
deps: { inline: ["@fastify/autoload"] },
},
}, I think this is worth mentioning in the You can find here the repro with the instructions to fix it. |
|
I'm afraid I have to disagree it is not related to vitest. I prefer option one since it is more consistent and works in monorepos. Option 2 doesn't work in monorepos since vitest is not able to parse dependencies of dependencies. If you have appA importing package 1 that is importing |
It seems there are often issues with typescript support more broadly, do you think there is work to do on this area @climba03003? Maybe improving the documentation? At the moment, I'm not familiar with this part and it seems rather tricky... But I'm not opposed to work on it. |
It should not be included for
Lines 101 to 116 in 178979f
|
Oh sure, @climba03003, we might detect |
Yes, |
I think it is not needed, we can detect various of loader already. |
Yes, you are absolutely right! |
@climba03003 unfortunately, I have an update. Using option one breaks vitest: mocks are not working anymore. I suspect it is because another transformer retransforms what the vite-node already mocked. So the only viable option is 2, and it will not work 100% in monorepos when you have sub-dependencies. |
Prerequisites
Fastify version
4.26.1
Plugin version
5.8.0
Node.js version
20.11.1
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
14.2.1 (23C71)
Description
Greetings Fellow Fastify Community!
As a long-time user, I use fastify-cli as my primary method for running my applications. The
app.ts
files have the typical structure for doing this with the client (after Typescript compile) with no issue, and autoload works. I had in the past to skip doing unit testing, but this new app I am working on, unit testing is key to making sure everything is working correctly, plus if I get it working here, then others could use it as well.In short, the unit test has this code (view full source here):
However, no matter what I did, looked at, searched, etc. I keep getting this error:
Now if I take out:
replace it with
... as an example... it works. However, the autoload, either directly in the unit test or loading from the function, does not work.
Many thanks in advance, as I am not sure what is going on.
Steps to Reproduce
I have the updated code saved here with this error: https://github.com/JAMS-Project/node-microservice-itil/tree/1-test-build-basic-unit-test-structure
Updated Unit Test Here: https://github.com/JAMS-Project/node-microservice-itil/blob/1-test-build-basic-unit-test-structure/__tests__/basic.test.ts
Expected Behavior
Unit tests should run :)
The text was updated successfully, but these errors were encountered: