Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Add sourcemap support for Karma #15

Open
flut1 opened this issue Jul 7, 2016 · 6 comments
Open

Add sourcemap support for Karma #15

flut1 opened this issue Jul 7, 2016 · 6 comments
Assignees

Comments

@flut1
Copy link
Contributor

flut1 commented Jul 7, 2016

Currently when a test fails, Karma will output a stack trace to the JS that is compiled in-memory. This can make it a bit difficult to find the line number for the failing test in the TS source.

We should look into sourcemap support, for example integrating the karma-sourcemap-support module.

@flut1 flut1 self-assigned this Jul 7, 2016
@ThaNarie
Copy link
Member

Found a guy that got it working:
PatrickJS/PatrickJS-starter#178 (comment)

Working example in the project from this karma config:
https://github.com/kozlice/angular2-webpack-boilerplate/blob/master/karma.conf.js

@flut1
Copy link
Contributor Author

flut1 commented Jul 10, 2016

I tried this example last Friday for some hours but couldn't get it to work somehow. Worth another shot though

@ThaNarie
Copy link
Member

Got pretty far pretty quickly, but got stuck on this error (googling for about an hour didn't help):

webpack: wait until bundle finished: 
11 07 2016 01:51:29.307:ERROR [karma]: { [Error: no such file or directory]
  code: 'ENOENT',
  errno: 34,
  message: 'no such file or directory',
  path: '/_karma_webpack_/test/index.ts' }
Error: no such file or directory
    at MemoryFileSystem.readFileSync (/Volumes/Projects/Repos/seng/seng-boilerplate/node_modules/memory-fs/lib/MemoryFileSystem.js:107:10)
    at MemoryFileSystem.readFile (/Volumes/Projects/Repos/seng/seng-boilerplate/node_modules/memory-fs/lib/MemoryFileSystem.js:297:21)
    at doRead (/Volumes/Projects/Repos/seng/seng-boilerplate/node_modules/karma-webpack/index.js:156:26)
    at Plugin.readFile (/Volumes/Projects/Repos/seng/seng-boilerplate/node_modules/karma-webpack/index.js:160:3)
    at doNTCallback0 (node.js:408:9)
    at process._tickCallback (node.js:337:13)

After cloning the angular webpack boilerplate project, and copy/pasting stuff back and forth on both ends, making them as similar as possible, I finally spotted the difference: webpack entry should be a string, not an array!

Now it's just moving all stuff back where it belongs, and create a PR :)

@ThaNarie
Copy link
Member

With the default setup from the angular2-webpack-boilerplate it seemed that only source files that were touched by tests showed up in the coverage results, which wasn't what we wanted.

So I added the const sourcesContext = require.context(...) back into the index.ts, which resulted the "Unknown file" error.

I forked the 1.1.1 awesome-typescript-loader version and added a workaround to the issue (skipping fastEmit when file hasn't been added yet), and linked to that version in the package.json.

When trying to use the same setup in seng-event I got runtime errors on const enums.

Debugging the above, I ran webpack for both the dist and the test builds, using different tsconfig.json versions, and I found out why I got the duplicate definition errors that stopped me using the 2.0 version before. When you add a ../node_modules relative path to the exclude array in the tsconfig.json, it suddenly adds all the d.ts files it can find in your project, giving a lot of errors.

With the 2.0 issue out of the way, and setting preserveConstEnums to true in the tsconfig.json I finally got a working solution!

Time invested: around 8 hours now :)

mediamonks-arjan pushed a commit that referenced this issue Jul 11, 2016
Added remap-istanbul and all supporting karma plugins to correctly
remap the coverage results back to their TypeScript source.

Since all generations happens in memory by Webpack, we needed inline
sourcemap support, provided by the Karma plugins, the Webpack devtool
setting, and the webpack typescript loader.

The normal `ts-loader` doesn't support inline sourcemaps, so we switched
to `awesome-typescript-loader`. We created a new `tsconfig.test.json`
that is the same as the normal `tsconfig.json` but enables inline
sourcemaps by adding `"inlineSourceMap": true`.

The webpack dist builds have also switched to `awesome-typescript-loader`
to keep the configuration consistent.

To make the `awesome-typescript-loader` correctly work with const enums,
we needed to enable `preserveConstEnums` property in the `tsconfig.json`.

We also switched he `test/index.js` to `index.ts` and included
`webpack-env` in the `typings.json` so all wepack requires work in
TypeScript.

To have correct coverage reports consisting of _all_ the source files,
we've added a `Dummy.ts` file in the `src/` folder that is not
referenced by any tests, but must show up in the coverage results.
If this is not the case, we know our test setup is incorrect.

re #15
@flut1 flut1 reopened this Jul 13, 2016
@flut1
Copy link
Contributor Author

flut1 commented Jul 13, 2016

Sourcemap support now works for istanbul/coverage, but not for karma/mocha. If I have a failing test, it shows me a stacktrace that has line numbers from the compiled source.

I'll mess around with some settings, hopefully we can solve this without large changes to our setup.

@flut1
Copy link
Contributor Author

flut1 commented Jul 16, 2016

For some reason, sourcemaps do work within Webstorm after moving the configuration to the repo root. See #32. Running karma from the console still shows incorrect line numbers.

flut1 pushed a commit that referenced this issue Jul 25, 2016
Remove karma-source-map-support dependency, it is no longer needed.
The karma-sourcemap-loader module will handle source maps for us.

Add the sourcemap preprocessor to the test files as well so the
test file line numbers also match.

Instead of the devtool option in the webpack test config, use
the SourceMapDevToolPlugin. This will also generate source
maps on files with a .ts extension.

Fixes #15
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants