I'm trying to set up a build process for TypeScript using Webpack. Everything's working fine for most parts. However I can't get source-maps to work properly in the Karma test runner.
Problem description
Let's say I have a typescript file test.spec.ts
(1). This file is transpiled by TypeScript to some ES5 source with inline source-maps (2). Finally that ES5 source is bundled by Webpack 4 using eval-source-maps (3) and served to Chrome by the Karma test runner.
By inspecting sources in the Karma Debug Runner in Chrome I can see that all three stages of transpilation are actually available to the browser:
- (1) is source-mapped as
webpack://test.spec.ts?c161
- (2) is source-mapped as
webpack-internal://test.spec.ts
- (3) is available as
localhost:9876/base/test.spec.ts
In the Chrome console I also get proper stack traces for all errors thrown during test execution. These include line numbers source-mapped to (1) as I would expect. For example:
Error: Oh no!
at Function.MyClass.myBadMethod (test.spec.ts?c161:9)
at UserContext.eval (test.spec.ts?c161:21)
at <Jasmine>
However Karma itself logs errors with line numbers only source-mapped to (2). For example:
Error: Oh no!
at Function.MyClass.myBadMethod (webpack-internal:///./src/test.spec.ts:8:15)
at UserContext.eval (webpack-internal:///./src/test.spec.ts:17:17)
at <Jasmine>
This is totally unhelpful, since (2) is just some intermediary source and never even written to disk. In fact I don't need source-maps for (2) and don't understand why they are included in the first place. If possible I would like to try and disable them (while keeping proper source-maps for (1), of course).
Anyways, the important thing is to get Karma to report stack traces with line numbers relative to the original files, just as it is done in the Chrome console. If necessary I'm also willing to trade execution speed for that
If you can't think of a full-fledged solution, but have some insights into TypeScript / TS-Loader / Webpack / Karma-Webpack / Karma: I'm also interested in any arguments that help pinpoint the problem along that toolchain.
Minimal test configuration
devDependencies from package.json
"devDependencies": {
"@types/jasmine": "~2.8.7",
"jasmine": "~3.1.0",
"karma": "~2.0.2",
"karma-chrome-launcher": "~2.2.0",
"karma-jasmine": "~1.1.2",
"karma-webpack": "~3.0.0",
"ts-loader": "~4.3.0",
"typescript": "~2.8.3",
"webpack": "~4.8.3"
}
karma.conf.js
module.exports = function(config) {
config.set({
frameworks: ['jasmine'],
browsers: ['Chrome'],
files: [
'./test.spec.ts'
],
mime: {
'text/x-typescript': ['ts']
},
preprocessors: {
'**/*.ts': ['webpack']
},
plugins: [
'karma-chrome-launcher',
'karma-jasmine',
'karma-webpack'
],
webpack: {
devtool: 'eval-source-map',
mode: 'development',
module: {
rules: [
{
test: /\.ts$/,
loader: 'ts-loader',
}
]
},
resolve: {
extensions: [ '.ts' ]
}
},
webpackMiddleware: {
logLevel: 'error'
}
});
};
tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"module": "es2015",
"moduleResolution": "node",
"sourceMap": true,
"target": "es5",
"types": [
"jasmine"
]
}
}