简体   繁体   中英

Vitest with React Testing Library 'Unexpected Token'

I'm using react with ViteJS and Vitest for testing. I have set up the config for vitest and the test setup file too as you can see in the screenshots below.

vite.config.js

vite配置

src/test/setup.ts

在此处输入图像描述

src/app/App.tsx

在此处输入图像描述

src/app/App.spec.js

在此处输入图像描述

here's the error I'm getting:

在此处输入图像描述

I found a lot of sources on similar issues about the topic but nothing I tried worked. I also followed the documentation for the vite config and a lot of articles too. Everyone is saying the same thing but I'm still getting this error for some reason.

I think you forgot to add @testing-library to vite.config.js alias object.

I used the same config with jest and it worked fine. But in this situation, I got stuck and then tried to rename the filename to .tsx or .jsx instead of .ts or .js . After that, I re-ran the test file and everything started working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM