Typescript compiler error when running tests using Vitest

See original GitHub issue

Prerequisites

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

Fastify version

3.28.0

Node.js version

16

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

12.3

Description

I would like to test my fastify app using vitest, however I have noticed there is a typescript error I can’t seem to get around.

When I run my tests Fastify errors, fastify-autoload cannot import plugin ... . To fix this error compile TypeScript to JavaScript or use 'ts-node' to run your app..

I can successfully run the tests when I compile them to Javascript and import the javascript version in the test. This does however, feel a bit hacky. It can also impact the code coverage (depending on how you are generating the javascript).

Steps to Reproduce

Stackbliz link: https://stackblitz.com/edit/node-sdwsoh

Run the npm run test command.

Expected Behavior

Tests can be run in vitest using Typescript without having to compile first, and import the javascript version.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:4
  • Comments:39 (21 by maintainers)

github_iconTop GitHub Comments

2reactions
Uzlopakcommented, Oct 12, 2022

I have a potential PR

1reaction
jclaessens97commented, Oct 14, 2022

Might be a config issue on my side, but when I now try my repro again (with the updated package), I get the following:

TypeError: Unknown file extension ".ts" for /Users/jclaessens/dev/github/vitest-alias-repro/packages/api/src/plugins/cors.ts
Serialized Error: {
  "code": "ERR_UNKNOWN_FILE_EXTENSION",
}

This is the same issue I got when I tried to do the changes myself.

Again, it might be a config issue on my side, but it’s unclear to me at this point.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing Types | Guide - Vitest
Keep in mind that Vitest doesn't run or compile these files, they are only statically analyzed by the compiler, and because of that...
Read more >
How do I fix TSC errors when using vite? - Stack Overflow
I have jest tests in my project as well. When I run vite build it seems to be compiling and bundling my test...
Read more >
Testing with Vitest - This Dot Labs
I'm using the VITEST env variable to differentiate when running tests, but if your configuration is too different, you can use another ...
Read more >
Introduction to Vitest: in-source testing using Typescript | CodeX
Vitest is a testing toolkit designed to make setting up, writing, and running tests easy. Want to find out more about Vite? Keep...
Read more >
How to Do Test-Driven Development with Svelte and Vitest
It helps in mocking the DOM API and running the tests in a reliable method. ... If you are using TypeScript, your TypeScript...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found