最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

node.js - Fastify Autoload with Typescript using Vitest - Stack Overflow

matteradmin6PV0评论

this is my scenario: I have a Fastify v5 server written in Typescript that I want to test using Vitest.

This is my npm script

vitest run --coverage --outputFile=results.xml

Following what suggested in the Fastify Demo project I am using fastify-cli to import the configured server as a plugin

onst AppPath = path.join(import.meta.dirname, "../server.ts");

export function config() {
  return {
    skipOverride: "true",
  };
}

// automatically build and tear down our instance
export async function build() {
  // you can set all the options supported by the fastify CLI command
  const argv = [AppPath];

  // fastify-plugin ensures that all decorators
  // are exposed for testing purposes, this is
  // different from the production setup
  const app = (await buildApplication(argv, config(), {})) as FastifyInstance;

  return app;
}

But, when I run my command, it says

TypeError: Unknown file extension ".ts" for /Users/mypc/fastify-scaffolding/src/server.ts
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:218:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:244:36)
    at defaultLoad (node:internal/modules/esm/load:122:22)
    at ModuleLoader.loadAndTranslate (node:internal/modules/esm/loader:479:32)
    at ModuleJob._link (node:internal/modules/esm/module_job:112:19) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

Can somebody help me with this? I've already made it work using the compiled JS after the build with tsx, but then the coverage is messed up, then I would like to find a way using the best approach possible.

this is my scenario: I have a Fastify v5 server written in Typescript that I want to test using Vitest.

This is my npm script

vitest run --coverage --outputFile=results.xml

Following what suggested in the Fastify Demo project I am using fastify-cli to import the configured server as a plugin

onst AppPath = path.join(import.meta.dirname, "../server.ts");

export function config() {
  return {
    skipOverride: "true",
  };
}

// automatically build and tear down our instance
export async function build() {
  // you can set all the options supported by the fastify CLI command
  const argv = [AppPath];

  // fastify-plugin ensures that all decorators
  // are exposed for testing purposes, this is
  // different from the production setup
  const app = (await buildApplication(argv, config(), {})) as FastifyInstance;

  return app;
}

But, when I run my command, it says

TypeError: Unknown file extension ".ts" for /Users/mypc/fastify-scaffolding/src/server.ts
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:218:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:244:36)
    at defaultLoad (node:internal/modules/esm/load:122:22)
    at ModuleLoader.loadAndTranslate (node:internal/modules/esm/loader:479:32)
    at ModuleJob._link (node:internal/modules/esm/module_job:112:19) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

Can somebody help me with this? I've already made it work using the compiled JS after the build with tsx, but then the coverage is messed up, then I would like to find a way using the best approach possible.

Share Improve this question asked Nov 16, 2024 at 17:15 SamDroidSamDroid 6531 gold badge10 silver badges29 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Fixed that setting the NODE_OPTIONS var this way

"test": "NODE_OPTIONS='--import tsx' vitest run --reporter junit --coverage --outputFile=results.xml"

I know that performances would be impacted because of the tsx import, but in my case is not so important

Post a comment

comment list (0)

  1. No comments so far