Vite with Jest: cannot find module start with alias '@'

Viewed 1158

I'm using Jest in Vite. It works until I import a module from node_module.

import { defineComponent } from 'vue'
import { useCookies } from '@vueuse/integrations/useCookies'

I got this error.

 FAIL  src/components/MyComponent/index.test.ts
  ● Test suite failed to run

    Cannot find module '@vueuse/integrations/useCookies' from 'src/components/MyComponent/index.vue'

    Require stack:
      src/components/MyComponent/index.vue
      src/components/MyComponent/index.test.ts

      16 | <script lang="ts">
      17 | import { defineComponent } from 'vue'
    > 18 | import { useCookies } from '@vueuse/integrations/useCookies'
         | ^
      19 |
      20 | export default defineComponent({

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:306:11)

I guess somehow Jest cannot resolve the module starts with '@'. Maybe I should write a moduleNameMapper? I also tried to install vite-jest, but somehow my app just crash, maybe I mess up something.

Anyway, thanks for your time, and if you need more information like my jest.config.js or vite.config.ts, please let me know.

Thank you.

0 Answers
Related