我正在尝试使用karma和karma-typescript-preprocessor自动执行单元测试。
然而,当我运行'grunt watch‘时,karma输出以下错误: error expect: /home/loic/Code/appName/src/app/app.spec.ts.ktp.ts(15,13):ERROR preprocessor.typescript: Cannot find name 'expect’。
这个错误发生在很多名称中:'describe,angular,expect‘等
奇怪的是,当我运行命令行'tsc /path/to/app.spec.ts‘时,创建了新的js文件,没有错误。
在我的karma.conf.js下面:
module.exports = function ( karma ) {
karma.set({
/**
* From where to look for files, starting with the location of this file.
*/
basePath: '../',
typescriptPreprocessor: {
// options passed to the typescript compiler
options: {
sourceMap: false, // (optional) Generates corresponding .map file.
target: 'ES5', // (optional) Specify ECMAScript target version: 'ES3' (default), or 'ES5'
module: 'amd', // (optional) Specify module code generation: 'commonjs' or 'amd'
noImplicitAny: false, // (optional) Warn on expressions and declarations with an implied 'any' type.
noResolve: true, // (optional) Skip resolution and preprocessing.
removeComments: true // (optional) Do not emit comments to output.
},
// transforming the filenames
transformPath: function(path) {
return path.replace(/\.ts$/, '.js');
}
},
/**
* This is the list of file patterns to load into the browser during testing.
*/
files: [
<% scripts.forEach( function ( file ) { %>'<%= file %>',
<% }); %>
'src/**/*.ts'
],
exclude: [
'src/assets/**/*.ts',
'src/typeScript/**/*.ts'
],
frameworks: [ 'jasmine' ],
plugins: [ 'karma-jasmine', 'karma-firefox-launcher', 'karma-typescript-preprocessor' ],
preprocessors: {
'**/*.ts': 'typescript'
},
/**
* How to report, by default.
*/
reporters: 'dots',
/**
* On which port should the browser connect, on which port is the test runner
* operating, and what is the URL path for the browser to use.
*/
port: 9018,
runnerPort: 9100,
urlRoot: '/',
/**
* Disable file watching by default.
*/
autoWatch: false,
/**
* The list of browsers to launch to test on. This includes only "Firefox" by
* default, but other browser names include:
* Chrome, ChromeCanary, Firefox, Opera, Safari, PhantomJS
*
* Note that you can also use the executable name of the browser, like "chromium"
* or "firefox", but that these vary based on your operating system.
*
* You may also leave this blank and manually navigate your browser to
* http://localhost:9018/ when you're running tests. The window/tab can be left
* open and the tests will automatically occur there during the build. This has
* the aesthetic advantage of not launching a browser every time you save.
*/
browsers: [
'Firefox'
]
});
};任何帮助都将不胜感激
发布于 2015-06-03 17:40:36
尝试删除行
noResolve: true,在typescriptPreprocessor配置中。这似乎会导致编译器无法正确解析您的引用。
发布于 2015-04-29 01:24:46
我也遇到了同样的问题,最终我不得不在序列typescript编译和karma测试中运行吞咽任务。
发布于 2015-07-10 05:41:12
将'noResolve‘值设置为false,并添加引用
我也在这里对此发表评论。
https://github.com/sergeyt/karma-typescript-preprocessor/issues/29
https://stackoverflow.com/questions/29912082
复制相似问题