fastify-autoload icon indicating copy to clipboard operation
fastify-autoload copied to clipboard

Typescript, Vitetest, and Autoload

Open Bugs5382 opened this issue 11 months ago • 7 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the bug has not already been reported

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):

import fastify, {FastifyInstance } from "fastify";
import {fileURLToPath} from "node:url";
import {dirname, join} from "path";
import {describe, test, beforeEach, afterEach, expect } from 'vitest';
import buildApp from '../src/app'

const fileName = fileURLToPath(import.meta.url)
const dirName = dirname(fileName)

let server: FastifyInstance

beforeEach(async () => {
  server = await buildApp(fastify())

  await server.ready()
})
afterEach(async () => {
  // called once after all tests run
  await server.close()
})

However, no matter what I did, looked at, searched, etc. I keep getting this error:

TypeError: Unknown file extension ".ts" for /Users/hel/Documents/removedGIT.nosync/jams/node-microservice-itil/src/plugins/graphql.ts
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:203:36)
    at defaultLoad (node:internal/modules/esm/load:143:22)
    at ModuleLoader.load (node:internal/modules/esm/loader:409:7)
    at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:291:45)
    at link (node:internal/modules/esm/module_job:76:21)

Now if I take out:

  server = await buildApp(fastify())

replace it with

  server = await fastify()
server.register(customHealthCheck)

... 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 :)

Bugs5382 avatar Feb 29 '24 15:02 Bugs5382

@mcollina ping png :)

Bugs5382 avatar Mar 02 '24 01:03 Bugs5382

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.

mcollina avatar Mar 02 '24 09:03 mcollina

Challenge accepted.

Bugs5382 avatar Mar 02 '24 14:03 Bugs5382

@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:

image

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?

Bugs5382 avatar Mar 04 '24 18:03 Bugs5382

Please send the PR :)

mcollina avatar Mar 05 '24 18:03 mcollina

Roger. Let me get the unit tests working. Some of the unit test functionality has "warnings" due to the impending version 5 of Fastify.

Bugs5382 avatar Mar 05 '24 23:03 Bugs5382

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.

Well. Try to use VITEST=true together with FASTIFY_AUTOLOAD_TYPESCRIPT=1, see also: https://github.com/fastify/help/issues/1014#issuecomment-2002007010. My repository wasn't using Vitetest framework, but I still needed to use this environment variable as a workaround for my "normal" ESM typescript project with ts-node. See also my demo repo: https://github.com/melroy89/fastify-node-ts-issue

Looks a bit related to each other.

melroy89 avatar Mar 16 '24 15:03 melroy89

I am having the same issue.

zt-9 avatar Apr 18 '24 06:04 zt-9

run tests with VITEST=true FASTIFY_AUTOLOAD_TYPESCRIPT=1 does not solve the issue my command "test:vitest": "FASTIFY_AUTOLOAD_TYPESCRIPT=1 VITEST=true vitest run"

zt-9 avatar Apr 18 '24 08:04 zt-9

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.

Uzlopak avatar Apr 18 '24 09:04 Uzlopak