首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Vite with Jest:无法找到以别名“@”开头的模块

Vite with Jest:无法找到以别名“@”开头的模块
EN

Stack Overflow用户
提问于 2021-07-22 14:07:37
回答 1查看 1.3K关注 0票数 5

我在用Jest在Vite。它可以工作,直到我从node_module导入一个模块。

代码语言:javascript
运行
复制
import { defineComponent } from 'vue'
import { useCookies } from '@vueuse/integrations/useCookies'

我犯了这个错误。

代码语言:javascript
运行
复制
 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)

我想Jest不知何故无法解析以“@”开头的模块。也许我应该写一个moduleNameMapper?我也试着安装vite-jest,但不知怎么的,我的应用程序崩溃了,也许我搞砸了什么。

无论如何,谢谢您的时间,如果您需要更多的信息,如我的jest.config.js或vite.config.ts,请告诉我。

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2022-11-29 13:19:07

在您的moduleNameMapper文件中使用jest.config.js,如下所示:

代码语言:javascript
运行
复制
module.exports = {
  //...
  moduleNameMapper: {
    "^@/(.*)$": "<rootDir>/src/$1",
  },
};

此外,如果您正在使用TypeScript,您也应该将以下内容添加到您的tsconfig.json文件中:

代码语言:javascript
运行
复制
{
  "compilerOptions": {
    "paths": {
      "@/*": ["./src"]
    }
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68486297

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档