haoqunjiang / vite-jest

First-class Vite integration for Jest
MIT License
406 stars 51 forks source link

Error when using vite-react-jsx #9

Open jonrimmer opened 3 years ago

jonrimmer commented 3 years ago

We are using the new jsx runtime using vite-react-jsx, but this causes an error when we run tests. I have reproduced it here: https://github.com/jonrimmer/vite-jest/tree/problem-jsx

The app runs fine when I run pnpnm dev, but when I run pnpm jest:unit on react-app-ts example, I get an error saying React is not defined...

 FAIL  src/App.test.tsx
  ✕ renders learn react link (52 ms)

  ● renders learn react link

    ReferenceError: React is not defined

       7 |   const [count, setCount] = useState(initial);
       8 |
    >  9 |   return (
         |   ^
      10 |     <div className={styles.app}>
      11 |       <header className={styles.appHeader}>
      12 |         <img src={logo} className={styles.appLogo} alt="logo" />

      at App (src/App.tsx:9:3)
      at Object.<anonymous> (src/App.test.tsx:5:3)

  console.error
    Error: Uncaught [ReferenceError: React is not defined]

...but the React import should be automatically added when I am using vite-react-jsx.