DEV Community

Cover image for Effortless Testing Setup for React with Vite, TypeScript, Jest, and React Testing Library

Effortless Testing Setup for React with Vite, TypeScript, Jest, and React Testing Library

Teyim Asobo on January 28, 2024

Recently, I have been learning to write some tests in React using Jest and RTL(React Testing Library), which has been a fun and eye-opening adventu...
Collapse
 
sergey_kachaliuk profile image
Sergey Kachaliuk

Thank for the article! Guys if you're getting an error:
"Cannot use JSX unless the '--jsx' flag is provided" then I hope this might help you(worked for me):

I am using Vite + React + TypeScript + Jest. I deleted tsconfig.app.json (comes out of the box) and tsconfig.node.json. So it's only tsconfig.json(code below). And it solved my problem with imports and jsx.

// tsconfig.json
{
    "compilerOptions": {
        "target": "ES2020",
        "useDefineForClassFields": true,
        "lib": ["ES2020", "DOM", "DOM.Iterable"],
        "module": "ESNext",
        "skipLibCheck": true,
        "moduleResolution": "bundler",
        "allowImportingTsExtensions": true,
        "resolveJsonModule": true,
        "isolatedModules": true,
        "noEmit": true,
        "jsx": "react-jsx",
        "esModuleInterop": true,
        "strict": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "noFallthroughCasesInSwitch": true
    },
    "include": ["src", "./jest.setup.ts"]
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
rajaerobinson profile image
Rajae Robinson

Great post, it's definitely not straightforward to setup unit testing for a React + Vite project. I recently wrote a similar article on this as well; it covers how to setup vitest, RTL, Husky, ESLint & Prettier.

Collapse
 
teyim profile image
Teyim Asobo

Thanks Rajae. Will definitely checkout your article.

Collapse
 
s1ckgit profile image
Антон Князев

screenshot

I get this error when i try to run tests :(

don't know how to fix this, i've already tried everything :/

Collapse
 
oleksii_malichenko_117267 profile image
Oleksii Malichenko

try to remove offensive words should help!

Collapse
 
fpsska profile image
Eugene

@s1ckgit, а ты это здорово придумал с label, жалко никто не оценил! Ставлю кла$$ за оригинальность :)

Collapse
 
hamza_naeemkhan_b5d12c66 profile image
Hamza Naeem Khan

I have fixed my issue by adding and pinning the versions of jest-environment and jest-config to the same version as jest.

"identity-obj-proxy": "^3.0.0",
"jest": "^27.5.1",
"jest-environment-jsdom": "^27.5.1",

Collapse
 
theavocadocoder profile image
Kelechi Nwa-uwa

This was really helpful for me. Thanks!

Collapse
 
premkumarg profile image
Prem Kumar G

Thank you, it's working. I got ts error, but after adding "esModuleInterop": true in tsconfig.json it went away.

Collapse
 
isaachagoel profile image
Isaac Hagoel

Thanks! this was a huge time saver

Collapse
 
teyim profile image
Teyim Asobo

I am happy you found it useful Isaac. :)

Collapse
 
buarki profile image
buarki

no fluffs and straight to the point, cheers mate!

Collapse
 
ujjwal_tiwari_ca11e514a2b profile image
Ujjwal Tiwari

it is really good! Saved a lot of time. Thank you so much