I just recently started working with Karma runner in our UI5 app. Wrote some unit tests, ran them... all worked fine so far.
However, now I´ve decided to track the code coverage. To measure it, I need to run preprocessor on my source code. And this is where I stumbled upon a problems - I am currently trying to make it work and both have some kind of problems
- npm package
karma-coverage
as a preprocessor - after installing it, I set it up in karma.conf.js like this
preprocessors: { "webapp/helpers/**/*.js": ['coverage'], "webapp/controller/**/*.js": ['coverage'], },
This works fine on helpers
folder since it contains only one file with simple javascript. However, when it tries to process controller
folder which has files with some ES6 code, each file fails with errors such as these
Unexpected token function
Unexpected token ...
As a second option, I tried to use
karma-babel-preprocessor
which should be able to handle also ES6 code. This is how my karma.conf.js file looks likepreprocessors: { "webapp/helpers//.js": ['babel'], "webapp/controller//.js": ['babel'], },
babelPreprocessor: { options: { presets: ['@babel/preset-env'], sourceMap: 'inline' } , filename: function (file) { return file.originalPath.replace(/\.js$/, '.es5.js'); }, sourceFileName: function (file) { return file.originalPath; } },
However, this one is not even able to find the js file (even though the address is the same as in the case of coverage preprocesor) and returns this error.
Error: failed to load 'sbn/portal/helpers/formatter.js' from .webapp/helpers/formatter.js: 404 - Not Found
at https://openui5.hana.ondemand.com/resources/sap-ui-core.js:86:37
Does someone have an idea how I can get the coverage data while using these packages or any other ones? There is a lots of conflicting info on the web, most of it several years old while various karma-related npm packages keep popping up each month so I am really not sure which one would be the best to use.
Thank a lot
We had the same problem and we managed to fix it integrating babel in a ui5-building-tool step.
This is how our package.json looks like:
This is how the ui5.yaml is looking like
This is how the transpile.js is looking like:
Be aware that this file should be placed in the root-dir/lib folder. root-dir is the folder where ui5.yaml is residing.
And finally this is the karma.conf.js setup:
In our project this setup works fine with ES6 code and prints the coverage.
Hope this you help you. Please give me a feedback how this works.