Bas*_*hen 7 testing unit-testing typescript jestjs babel-jest
我正在为我的 Web 应用程序设置测试,该应用程序使用 Jest 框架与带有 TypeScript 的 Vue 框架配合使用。一切似乎都工作正常,除了@我导入中的符号,例如import { Foo } from '@/bar. 无论是在我的测试文件中还是在源代码中,它都无法理解并返回:
Cannot find module '@/store' from 'src/tests/foo.spec.ts'
Run Code Online (Sandbox Code Playgroud)
这是我的 Jest 配置package.json:
"jest": {
"moduleFileExtensions": [
"js",
"ts",
"json",
"vue"
],
"testRegex": "(/tests/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"transform": {
".*\\.(vue)$": "vue-jest",
"^.+\\.tsx?$": "ts-jest",
"^.+\\.jsx?$": "babel-jest"
},
"testURL": "http://localhost/",
"collectCoverage": true,
"collectCoverageFrom": ["**/*.{ts,vue}", "!**/node_modules/**"],
"coverageReporters": ["html", "text-summary"]
}
Run Code Online (Sandbox Code Playgroud)
有人知道如何使其正常工作吗?
谢谢
我最终找到了一个解决方案,我将其附加到我的 Jest 配置中package.json:
"moduleNameMapper": {
"^@/(.*)$": "<rootDir>/src/$1"
},
Run Code Online (Sandbox Code Playgroud)