unittesting - Jest-test met TypeScript herkent import-alias niet
Ik ben de tests aan het opzetten voor mijn webapplicatie, die werkt met het Vue-framework en TypeScript, met het Jest-framework. Alles lijkt goed te werken, behalve met het @-symbool in mijn imports, bijvoorbeeld import { Foo } from '@/bar. Of het nu in mijn testbestanden of in de broncode is, het begrijpt het niet en geeft terug:
Cannot find module '@/store' from 'src/tests/foo.spec.ts'
Hier is mijn Jest-configuratie in 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"]
}
Heeft iemand enig idee hoe je het goed kunt laten werken?
Dank je
Antwoord #1
Ik vond uiteindelijk een oplossing, ik voegde dit toe aan mijn Jest-configuratie in package.json:
"moduleNameMapper": {
"^@/(.*)$": "<rootDir>/src/$1"
},
Antwoord #2
Uit de ts-jest documentatie sectie over pad-koppeling:
// 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>/' } */),
}
