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 badges1 Answer
Reset to default 0Fixed 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