【发布时间】:2017-08-26 22:50:18
【问题描述】:
我有一个非常小的项目,我正在尝试为其设置单元测试。该项目在直接使用tsc 时编译得很好,但是,在尝试执行使用 karma-typescript 框架的测试时,我收到以下 Typescript 编译错误:
错误:
错误 [compiler.karma-typescript]: src/drawing/canvas.model.ts(1,23): 错误 TS2307:找不到模块 'utils'。
错误 [compiler.karma-typescript]: src/models/grid.model.ts(1,23): 错误 TS2307: 找不到模块 'utils'。
错误 [compiler.karma-typescript]: src/utils/specs/obj.utils.spec.ts(1,23): 错误 TS2307: 找不到 模块'utils'。
项目结构: 我已经建立了一个结构如下的项目:
|-dist/
|-node_modules/
|-src/
| |
| |-drawing/
| | |
| | |-canvas.model.ts
| | ________________________________
| | | import { Utils } from 'utils'; | Karma Fails (tsc is fine)
| | --------------------------------
| |
| |-models/
| | |
| | |-grid.model.ts
| | ________________________________
| | | import { Utils } from 'utils'; | Karma Fails (tsc is fine)
| | --------------------------------
| |
| |-utils/
| | |
| | |-index.ts
| | | _________________________
| | | | export module Utils {} |
| | | -------------------------
| | |
| | |-specs/
| | | |
| | | |-obj.utils.spec.ts
| | | ________________________________
| | | | import { Utils } from 'utils'; | Karma Fails (tsc is fine)
| | | --------------------------------
| | |
|-karma.config.js
|-tsconfig.json
|-package.json
我很清楚,我的 tsconfig.json 文件与内部用于 karma-typescript 的编译器设置之间存在差异。这是我如何构建这两个文件的方式。根据documentation for karma-typescript,我可以在我的 karma.conf 文件中配置几个选项,告诉 karma-typescript 尊重我在我的 Typescript 配置文件中的设置,即"paths" 属性,这是我已经向 Typescript 指定了在哪里查找我的 utils 模块。
KARMA.CONF.JS
// Karma configuration
// Generated on Fri Mar 31 2017 16:42:11 GMT-0700 (PDT)
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine', 'karma-typescript'],
// Plugins
plugins: [
'karma-spec-reporter',
'karma-jasmine',
'karma-chrome-launcher',
'karma-jasmine-html-reporter',
'karma-typescript'
],
// list of files / patterns to load in the browser
files: [{
pattern: "./src/**/*.ts"
}],
// list of files to exclude
exclude: ['**/*.spec.js'],
// Karma Typescript compiler options
karmaTypescriptConfig: {
bundlerOptions: {
resolve: {
directories: ["src", "node_modules", "src/utils"]
}
}
},
compilerOptions: {
module: 'commonjs',
moduleResolution: 'node',
paths: {
'utils': ['./src/utils'], 'utils/*': ['./src/utils/*']
}
},
tsconfig: './tsconfig.json',
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
"**/*.ts": ["karma-typescript"]
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress', 'kjhtml', 'spec', "karma-typescript"],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}
这是我的 Typescript 配置文件。请注意,我在 tsconfig 文件的 "paths" 部分中注册了 "utils",这有助于 Typescript 编译器的 module resolution process。这适用于普通的 Typescript 编译,但这可能是因为我的 Typescript 编译器实际上尊重了我的 tsconfig 文件中的设置。我正在使用 Typescript 2.0.10。但似乎 karma-typescript 正在使用 Typescript 2.2.2,这可能是错误的潜在来源。我必须针对那个版本运行我的编译器,看看是否会产生同样的错误。
TSCONFIG.JSON
{
"compileOnSave": true,
"compilerOptions": {
"baseUrl": ".",
"outDir": "./dist",
"paths": {
"utils/*": ["./src/utils/*"],
"utils": ["./src/utils"]
},
"declaration": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": ["es5", "dom"],
"mapRoot": "./",
"module": "es6",
"moduleResolution": "node",
"sourceMap": true,
"target": "es5",
"rootDirs": ["./dist", "."]
},
"exclude": ["./node_modules", "./dist", "**/*.d.ts"],
"include": ["./src/**/*.ts"]
}
谁能帮我解决这个问题?我对 Typescript 很满意,但对 Karma 很陌生。我已经搜索了大约 2 天的文档,试图让这些简单的单元测试运行,但无济于事。至少我的路径结构不是这样。任何帮助将不胜感激!
更新: 我尝试将我本地安装的 Typescript 更新为 2.2.2,以匹配同样是 2.2.2 的 Karma-Typescript 版本。同样的错误,同样的场景——我的本地版本编译得很好,但 Karma-Typescript 版本不支持。
【问题讨论】:
标签: javascript typescript karma-runner