Jest test coverage does not recognize decorators
I am using jest to test and add test coverage on my typescript project. I have noticed that in test coverage I get that the inversify decorators are not covered.
Have anyone faced the same issue.
Also some examples of using inversify with unit tests would be great.
Hi we use mocha, chai and sinon with istanbul to run our unit test and test coverage and it works fine. I have never work with jest maybe is a jest issue?
I am using ava and can confirm that decorators make problems. But I think it's actually reflect-metadata that is causing this problem.
Do you think there is anything we can do about it? What is using Ava / Jest for test coverage? istanbul?
Jest is using instabul under the covers.
@lholznagel I do have the same feeling, that reflect-metadata is causing the issue.
I use for coverage nyc. For coverage with ava you need to use nyc because ava´s way of running tests.
The only solution I can think of, is to map the coverage files to the ts files not the js.
So just tested my idea. It works :tada:
When using sourcemaps they should automatically map to the ts files. When this is successful you see in the Istanbul html report, that there are all .ts files and no .js files. When you now open one of those files it shows the coverage of that file. Because we don´t have any decorators that are converted from ts to js we don´t have the problem that that code is not covered.
@lholznagel I am happy for your success :D Can you please share of how did you map the ts files in your coverage configuration?
So firstly I tested to use gulp-sourcemaps and gulp-typescript like I did before. The problem was, that because of some paths in the sourcemaps, nyc had some problems.
After some tinkering I changed added "inlineSourceMap": true to my tsconfig.
Whole tsconfig
{
"compilerOptions": {
"target": "es6",
"inlineSourceMap": true,
"module": "commonjs",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"moduleResolution": "node",
"declaration": false
},
"compileOnSave": false,
"exclude": [
"node_modules"
]
}
and that's basically it. When I now compile with tsc the javascript files all contain the sourcemaps for the typescript file and nyc uses them.
Hope it helps :)
Hi @ppsimatikas did the solution work for you? Can this issue be closed? Thanks
I have tried the ts configuration with no success.
Hi @ppsimatikas, sorry to hear that. Do you have a repo or similar? Maybe I can take a look at it and help.
Unfortunately I am not able to share the repo, but I can share the needed files with you.
jest.json
{
"collectCoverageFrom": [
"client/**/*.js",
"client/**/*.jsx",
"server/**/*.ts",
"server/**/*.tsx"
],
"coverageThreshold": {
"global": {
"functions": 50,
"lines": 50,
"statements": 50
}
},
"moduleFileExtensions": [
"js",
"jsx",
"ts",
"tsx"
],
"transform": {
"^.+\\.(ts|tsx)$": "<rootDir>/test_setup/preprocessor.js",
"^.+\\.(js|jsx)$": "<rootDir>/node_modules/babel-jest"
},
"testResultsProcessor": "<rootDir>/node_modules/ts-jest/coverageprocessor.js",
"testRegex": "/test/.*\\.spec\\.(ts|tsx|js|jsx)$",
"updateSnapshot": true,
"moduleNameMapper": {
"^.*[.](jpg|JPG|gif|GIF|png|PNG|less|LESS|css|CSS|scss|SCSS)$": "<rootDir>/test_setup/EmptyModule"
}
}
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"target": "es2015",
"inlineSourceMap": true,
"types": [
"node",
"reflect-metadata"
],
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"lib": ["es6", "dom"],
"allowJs": true,
"jsx": "react"
},
"exclude": [
"node_modules"
],
"compileOnSave": false,
"buildOnSave": false
}
Hm, tsconfig looks good. Never looked into jest so I have no idea if the config is okay.
When you now run the tests and nyc displays its output (look down for example). Are the files shown as .ts or .js?
Maybe chaning es2015 to es6 but I don´t think that this is the problem
---------------------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
---------------------|----------|----------|----------|----------|----------------|
All files | 78.1 | 73.68 | 66.25 | 78.02 | |
src | 76 | 85.71 | 42.86 | 76 | |
amqp.ts | 100 | 100 | 100 | 100 | |
---------------------|----------|----------|----------|----------|----------------|
Hi @ppsimatikas have you been able to solve this issue? Please note that I will close this issue in a few days If there is nothing else we can do to help.
ps: Thanks a lot @lholznagel for your help here 👍