I have a basic Angular/Typescript project with 12 rudimentary unit tests that run perfectly fine. Now I would like to get the coverage for these tests. I tried various approaches, and none of them worked, so I decided to start over with karma-coverage and ask for help here. :-)
Currently, when I run karma, I get an error message for every single source file that looks like this:
Failed to parse file: C:/Users/FRBA/Documents/MyProject/src/app/nav/new-panel/new-panel.component.ts
07 07 2017 07:54:35.832:ERROR [preprocessor.coverage]: Line 1: Unexpected token
at C:/Users/FRBA/Documents/MyProject/src/app/nav/new-panel/new-panel.component.ts
My karma.conf.js looks like this:
var path = require('path');
module.exports = function (config) {
config.set({
files: [
'src/app/**/*.ts',
'test/app/**/*.ts'
],
basePath: '',
frameworks: ['jasmine', '@angular/cli'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-ie-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage'),
require('@angular/cli/plugins/karma')
],
client: {
clearContext: false
},
angularCli: {
environment: 'dev'
},
reporters: ['progress', 'kjhtml', 'coverage'],
preprocessors: {
'src/app/**/*.ts': ['coverage']
},
coverageReporter: {
type : 'html',
dir : 'coverage/'
},
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome', 'IE'],
singleRun: false
});
};
Like I said, I have tried various approaches already - karma-coverage, karma-coverage-istanbul-reporter, karma-typescript, etc., and I always end up with various problems (karma generating empty reports, karma stopping to execute after the "10% building modules" line, and so on), so it seems I am doing something fundamentally wrong. Any additional pointers or tutorials that explain this to a karma (and Typescript) newbie would be appreciated. Thanks a lot!
To solve the "Uncaught SyntaxError: Unexpected token import" in TypeScript, set the module option to commonjs in your tsconfig.json file and make sure to compile your TypeScript files (e.g. with ts-node), and not to run them directly with node. Open your tsconfig.json file and make sure the module option is set to commonjs.
Alternatively, you can use an async function instead (since v6.3). Under the hood Karma uses ts-node to transpile TypeScript to JavaScript. If the resolved tsconfig.json has module configured as ES formats. You might get errors like SyntaxError: Unexpected token. This is due that in Node ES module formats are not supported.
If the resolved tsconfig.json has module configured as ES formats. You might get errors like SyntaxError: Unexpected token. This is due that in Node ES module formats are not supported. To overcome this issue you need to configure ts-node to use commonjs module format. Create a JavaScript configuration file that overrides the module format.
If you Google "jest unexpected token", there are several signs that this is a really nasty issue: There are a great many threads on the issue - on Stack Overflow and otherwise.
As mentioned in the karma-typescript
docs under configuration, you need to add karma-typescript
as a preprocessor. If you are testing TypeScript files (which I am assuming you are judging from the .ts extension), you need to transpile them into JavaScript for downstream processors to be able to parse them. The unexpected token error likely references a token that is only valid in TypeScript.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With