בדיקות יחידה - מבחן Jest עם TypeScript לא מזהה כינויי ייבוא
אני מכין את הבדיקות עבור אפליקציית הווב שלי, שעובדת עם מסגרת Vue ו-TypeScript, תוך שימוש במסגרת Jest. הכל נראה עובד בסדר, מלבד הסימן @ בייבוא שלי, למשל, import { Foo } from '@/bar. בין אם בקבצי הבדיקה שלי או בקוד המקור, הוא לא מבין את זה ומחזיר:
Cannot find module '@/store' from 'src/tests/foo.spec.ts'
הנה התצורה שלי ל-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"]
}
למישהו יש מושג איך לגרום לזה לעבוד כמו שצריך?
תודה
תשובה #1
בסופו של דבר מצאתי פתרון, הוספתי זאת לקונפיגורציה של Jest שלי ב-package.json:
"moduleNameMapper": {
"^@/(.*)$": "<rootDir>/src/$1"
},
תשובה #2
מהחלק של מיפוי מסלולים בתיעוד של ts-jest:
// jest.config.js
const { pathsToModuleNameMapper } = require('ts-jest/utils')
// In the following statement, replace `./tsconfig` with the path to your `tsconfig` file
// which contains the path mapping (ie the `compilerOptions.paths` option):
const { compilerOptions } = require('./tsconfig')
module.exports = {
// [...]
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths /*, { prefix: '<rootDir>/' } */),
}
