最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论