【发布时间】:2018-02-04 08:21:38
【问题描述】:
我正在使用 karma、webpack 和 typescript 设置开发环境,但是我遇到了 karma 没有在测试中应用自定义定义文件的问题。
这是我的项目文件结构:
// file structure
project/
config/
karma.conf.js
tests/
test-files.ts
...
src/
tsdefinitions.d.ts
other-ts-files.ts
...
tsconfig.json
这里是与 webpack 和 typescript 相关的 karma 配置部分
webpack: {
devtool: 'eval-source-map',
resolve: {
extensions: ['.webpack.js', '.web.js', '.ts', '.tsx', '.js']
},
module: {
loaders: [{
test: /\.tsx?$/,
loader: 'awesome-typescript-loader'
}]
}
},
webpackMiddleware: {stats: 'errors-only'},
mime: {'text/x-typescript': ['ts','tsx']},
basePath: '../',
files: [{
pattern: './test/**/*.ts',
watched: false
}],
preprocessors: {
'./test/**/*.ts': ['webpack', 'sourcemap']
}
最后是 tsconfig.json
{
"compilerOptions": {
"removeComments": true,
"allowSyntheticDefaultImports": true,
"baseUrl": "./",
"sourceMap": true,
"noImplicitAny": true,
"allowJs": true,
"module": "commonjs",
"target": "es2015",
"paths": {
"jquery": ["node_modules/jquery/dist/jquery"]
},
"exclude": ["node_modules"],
"files": [
"src/**/*.ts"
]
},
"awesomeTypescriptLoaderOptions": {
"useCache": false,
"useBabel": true,
"babelOptions": {
"presets": [
"es2015",
"stage-0"
]
}
}
}
我正在像这样运行业力测试
./node_modules/.bin/karma start ./config/karma.conf.js
现在,当我使用 karma 构建项目文件时,一切正常,它们构建时没有错误。
但是当我运行测试构建时,会出现很多缺失 tsdefinitions.d.ts 文件中声明的窗口界面属性的错误。
【问题讨论】:
-
我也遇到了同样的问题,在网上搜索了2个小时都没成功,请问您找到解决方案了吗?
-
我当前使用的解决方法是在通过
karma使用时将awesome-typescript-loader设置为transpileOnly。 -
你在用atl提供的paths插件吗?
-
我对 karma + typescript + webpack 中的自定义类型声明没有任何问题。你能给出一个缺少的声明接口的示例吗?
-
隐藏的依赖项出现了哪些错误?如果您缺少 windows 文件,这可能是一个更容易找到并可能解决问题的问题。
标签: typescript webpack karma-runner tsconfig