Typescript coverage %

I'm using

"devDependencies": {
"@types/jasmine": "2.6.3",
"@types/node": "8.0.50",
"jasmine": "2.8.0",
"jasmine-core": "2.8.0",
"karma": "1.7.1",
"karma-chrome-launcher": "2.2.0",
"karma-coverage": "1.1.1",
"karma-jasmine": "1.1.0",
"karma-typescript": "3.0.8",
"typescript": "2.6.1"
}

 


Setting up .ts for coverage in the preprocessor doesn't work.

 

preprocessors: {
'**/*.ts': ['coverage']
},



When I change it to:

 '**/*.js': ['coverage']



It does work as expected, but only for .js files which are bundled under .ts files and are a pain to check.

http://prntscr.com/h86qka

I have to click each .ts file to see the coverage percentage for .js files.

I've tried karma-typescript, which does the behaviour I'm looking for, but it is compiling all the .ts files before each test, which is unnecessarily long.

0
3 comments

when not using karma-typescript, how do you compile your typescript files? What does your karma.config look like?

 

1
Avatar
Permanently deleted user

Thanks for the quick answer.

Before: Typescript files were compiling automatically to Javascript by Webstorm. Simple tests were working and I could run the tests by clicking the arrow button in .ts files or by running karma. http://prntscr.com/h89omu

I've setup karma from this video: https://www.youtube.com/watch?v=oyWW_V4wALs

However I just realized that I can't run complex tests without using karma-typescript. Like testing files that have imported modules. (I might be missing something, maybe there is a way to do this using some Webstorm settings?)

Right now: I'm back to using karma-typescript and the coverage shows up as expected. Imported modules can be tested too. 

 

My new complaint: If possible I would like to skip the compile process of karma-typescript. (Not directly Webstorm related I guess)

Or if these is a recommended way of testing my .ts files in Webstorm I'll be happy to learn about it. 

 

My karmaconfig atm: 

 

module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', 'karma-typescript'],
files: [
'code/**/*.ts'
// 'code/**/*.js' //@Elena This was active before and .ts was commented out.
],
preprocessors: {
'**/*.ts': ['karma-typescript', 'coverage'],
// '**/*.js': ['coverage'] //@Elena this was active before and .ts was commented out.
},
reporters: ['progress', 'coverage', 'karma-typescript'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['Chrome'],
singleRun: false,
concurrency: Infinity,
exclude: []
})
}

 

 

 

0

>Typescript files were compiling automatically to Javascript by Webstorm

With your configuration, Karma knows nothing about Typescript, only generated js files are loaded and served by karma server. As TypeScript files are not processed, they can't be instrumented for coverage

 

>I might be missing something, maybe there is a way to do this using some Webstorm settings?

It has absolutely nothing to do with WebStorm (just as all other issues mentioned above), and can't be fixed on WebStorm end. TypeScript compiler can transpile modules to the following formats:

- target: es5, modules:commonjs: CommonJS modules; can only be run with Node.js

- target: es5, modules:amd: AMD modules, can be run in browser, but require installing RequireJS library and formatting your code for RequireJS

-target: es5, modules:system: to be used with SystemJS, require re-writing your code for SystemJS module loader format

- target: es6: ECMAScript 6 modules, can be run natively by neither browser nor Node.js

 

Karma tests are executed in browser, so, when using modules, the only options are `amd` and `system`. Both have their drawbacks, and both require special coding style and setup. So the industry standard currently is using different preprocessors that compile modules and bundle them so that they can be run in browser. 'karma-typescript' is just one of them; another popular one is karma-webpack (https://github.com/webpack-contrib/karma-webpack) - it's more powerful and is normally used for more complex applications

 

>If possible I would like to skip the compile process of karma-typescript. (Not directly Webstorm related I guess)

Not related to WebStorm in any way. Please see https://github.com/monounity/karma-typescript/issues/70 for explanation

 

> if these is a recommended way of testing my .ts files in Webstorm I'll be happy to learn about it. 

No, there are no recommended ways to set up unit tests; we do not force users to choose this or that tool, library or configuration

1

Please sign in to leave a comment.