我在用Jest在Vite。它可以工作,直到我从node_module导入一个模块。
import { defineComponent } from 'vue'
import { useCookies } from '@vueuse/integrations/useCookies'我犯了这个错误。
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,请告诉我。
谢谢。
发布于 2022-11-29 13:19:07
在您的moduleNameMapper文件中使用jest.config.js,如下所示:
module.exports = {
//...
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/$1",
},
};此外,如果您正在使用TypeScript,您也应该将以下内容添加到您的tsconfig.json文件中:
{
"compilerOptions": {
"paths": {
"@/*": ["./src"]
}
}
}https://stackoverflow.com/questions/68486297
复制相似问题