I am trying to configure a basic test runner using Karma in order to test a TypeScript class.
However, when I attempt to run the tests with karma start
, I encounter an error stating that
ReferenceError: Calculator is not defined.
It seems like either the karma runner is not importing the transpiled source file or the preprocessor is failing to transpile the source code.
You can find my source code in this repository here, and the relevant parts are provided below. Can you help me figure out how to configure my setup properly?
Based on my current understanding, both the transpiler and the 'files' configuration property in karma should be loading the calculator class for me.
export class Calculator{
add ( a : number , b : number) : number {
return a + b;
}
}
/test/calculator.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);
});
});
package.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,
target: 'ES5',
module: 'amd',
noImplicitAny: true,
noResolve: true,
removeComments: true,
concatenateOutput: false
},
// transforming the filenames
transformPath: function(path) {
return path.replace(/\.ts$/, '.js');
}
}
...