我正在使用Jest为带有TypeScript和单个文件组件的Vue js项目编写测试。
我认为问题在于'vue-di-container'使用(.d.ts)声明,因此,如果有任何方法可以跳过所有声明(.d.ts)文件的类型检查,我认为将解决问题
我在CMD上运行测试时显示错误消息,并且显示错误消息:
测试套件无法运行
Cannot find module 'vue-di-container' from 'LoaderService.ts'
However, Jest was able to find:
'../../services/LoaderService.ts'
You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['ts', 'tsx', 'js', 'jsx', 'json', 'd.ts', 'node'].
See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string
However, Jest was able to find:
'./MyUserService.ts'
You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['ts', 'tsx', 'js', 'jsx', 'json', 'd.ts', 'node'].
See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string
However, Jest was able to find:
'../MyUsers/MyUsers.ts'
You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['ts', 'tsx', 'js', 'jsx', 'json', 'd.ts', 'node'].
See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string
However, Jest was able to find:
'../components/Index/Index.ts'
You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['ts', 'tsx', 'js', 'jsx', 'json', 'd.ts', 'node'].
See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:259:17)
at Object.<anonymous> (ClientApp/services/LoaderService.ts:180:25)
at Object.<anonymous> (ClientApp/components/MyAppointments/MyAppointmentService.ts:356:48)
我的jest.config.ts:
module.exports = {
moduleFileExtensions: [
'ts',
'tsx',
'js',
'jsx',
'json',
'node'
],
moduleNameMapper: {
"@/(.*)": "<rootDir>/ClientApp/$1"
},
moduleDirectories: [
'node_modules',
'node_modules/vue-di-container',
'ClientApp'
],
modulePaths: [
'./node_modules'
],
roots: [
'ClientApp'
],
transform: {
'^.+\\.vue$': 'vue-jest',
'.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
'^.+\\.(ts|js)x?$': 'ts-jest',
'^.+\\.ts?$': 'babel-jest',
"/dts-jest/.+\\.ts$": "dts-jest/transform",
"^.+\\.html$": "vue-template-loader-jest"
},
transformIgnorePatterns: ['/node_modules/(?!@jest)', "/node_modules/(?!vue-loading-spinner)"],
testMatch: [
'**/tests/*.spec.(js|jsx|ts|tsx)'
],
testURL: 'http://localhost/',
verbose: true,
collectCoverage: true,
collectCoverageFrom: [
'ClientApp/**/*.{ts,vue}',
'!ClientApp/main.ts'
]
}
我的babel.config.js:
module.exports = {
plugins: [
"@babel/plugin-transform-typescript",
["@babel/plugin-transform-modules-commonjs", {
"allowTopLevelThis": true
}]],
presets: [
'@vue/app'
]
}