我想使用 Karma 设置一个基本的测试运行程序来测试 Typescript 类。
当我运行测试时karma start
,我收到错误ReferenceError: Calculator is not defined.
据推测,karma 运行程序可能不会导入已转译的源代码,或者预处理器不会转译源代码。
我的来源是在回购协议中 https://github.com/monkeyhouse/KarmaTypescriptConfigQuestion与下面的相关部分。我怎样才能让配置工作/我缺少什么?
我目前的理解是,转换器和业力“文件”配置属性将为我加载计算器类。
transpiler => lib/calculator.ts => lib/calculator.ts
files => okay, loading lib/**/.js
/lib/计算器.ts
export class Calculator{
add ( a : number , b : number) : number {
return a + b;
}
}
/测试/计算器.test.js
describe('Demo Test Runner', function() {
var calc = new Calculator();
it('should return 3 for 1 + 2', function() {
expect( calc.add(1,2) ).toBe(3);
});
});
包.json
...
"devDependencies": {
"jasmine-core": "^2.5.2",
"karma": "^1.3.0",
"karma-chrome-launcher": "^2.0.0",
"karma-jasmine": "^1.0.2",
"karma-typescript-preprocessor": "^0.3.0"
}
karma.conf.js
module.exports = function(config) {
config.set({
...
frameworks: ['jasmine'],
files: [
'lib/**/*.js',
'test/**/*.js'
],
preprocessors: {
'**/*.ts': ['typescript']
},
typescriptPreprocessor: {
// options passed to the typescript compiler
options: {
sourceMap: true, // (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: true, // (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.
concatenateOutput: false // (optional) Concatenate and emit output to single file. By default true if module option is omited, otherwise false.
},
// transforming the filenames
transformPath: function(path) {
return path.replace(/\.ts$/, '.js');
}
}
...