Vitest with React Testing Library 'Unexpected Token'
Asked Answered
R

3

5

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 config

src/test/setup.ts

enter image description here

src/app/App.tsx

enter image description here

src/app/App.spec.js

enter image description here

here's the error I'm getting:

enter image description here

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.

Rhona answered 31/12, 2022 at 14:57 Comment(0)
R
12

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.

Rhona answered 4/1, 2023 at 14:19 Comment(0)
T
15

This might be obvious to most people but in my case the issue was that the test file also has to end in ".jsx" when trying to test a ".jsx" file.

App.test.jsx worked for App.jsx

Tarpley answered 13/2, 2023 at 10:38 Comment(1)
I was facing the same issue. And this worked for me :) Thanks Patrick.Manzanilla
R
12

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.

Rhona answered 4/1, 2023 at 14:19 Comment(0)
M
0

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

Montalvo answered 31/12, 2022 at 15:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.