尝试使用karma-jasmine
+ typescript
+ karma-typescript-preprocessor
进行单元测试时出现问题。
我在堆栈跟踪中不断收到以下错误:
23 04 2016 14:53:46.194:ERROR [preprocessor.typescript]: Cannot read property 'text' of undefined
at /home/booyanach/WebstormProjects/cookie-wrapper/src/cookie.wrapper.spec.ts
TypeError: Cannot read property 'text' of undefined
at TypeScriptSimple.toJavaScript (/home/booyanach/WebstormProjects/cookie-wrapper/node_modules/typescript-simple/index.js:162:28)
at TypeScriptSimple.compile (/home/booyanach/WebstormProjects/cookie-wrapper/node_modules/typescript-simple/index.js:71:25)
at /home/booyanach/WebstormProjects/cookie-wrapper/node_modules/karma-typescript-preprocessor/index.js:22:26
at nextPreprocessor (/home/booyanach/WebstormProjects/cookie-wrapper/node_modules/karma/lib/preprocessor.js:35:26)
at /home/booyanach/WebstormProjects/cookie-wrapper/node_modules/karma/lib/preprocessor.js:114:9
at module.exports (/home/booyanach/WebstormProjects/cookie-wrapper/node_modules/isbinaryfile/index.js:28:12)
at /home/booyanach/WebstormProjects/cookie-wrapper/node_modules/karma/lib/preprocessor.js:79:7
at /home/booyanach/WebstormProjects/cookie-wrapper/node_modules/graceful-fs/graceful-fs.js:78:16
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:404:3)
这是我的karma.conf.file:
module.exports = function(config) {
config.set({
basePath: './',
frameworks: ['jasmine'],
files: [
{ pattern: './**/*.ts', included: false }
],
exclude: [
'dist/',
'node_modules/',
'typings/',
'coverage/'
],
preprocessors: {
'./src/*.ts': ['typescript']
},
typescriptPreprocessor: {
options: {
sourceMap: false,
target: 'ES5',
module: 'commonjs'
},
transformPath: function(path) {
return path.replace(/\.ts$/, '.js');
},
typings: [
'typings/browser.d.ts'
]
},
reporters: [],
port: 9999,
colors: true,
logLevel: config.LOG_DEBUG,
autoWatch: true,
browsers: ['PhantomJS'],
singleRun: true,
concurrency: Infinity
})
};
注意:cookie.wrapper.spec.ts不为空