Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need help with code coverage when using Jest to test Angular app #454

Closed
ahnpnl opened this issue Feb 26, 2018 · 23 comments
Closed

Need help with code coverage when using Jest to test Angular app #454

ahnpnl opened this issue Feb 26, 2018 · 23 comments

Comments

@ahnpnl
Copy link
Collaborator

ahnpnl commented Feb 26, 2018

  • Issue

I tried to use Jest to test Angular app but the code coverage shows branch not covered for dependency injection in constructor. This issue is fixed if using Karma + Jasmine with Angular CLI. I hope someone can help me to point out what goes wrong, either in ts-jest or Jest

  • Expected behavior
    Code coverage should show correctly

  • Output from your debug log
    N.A

  • Link to a minimal repo that reproduces this issue
    https://github.com/AhnpGit/coverage-issue-jest
    Run yarn coverage and check coverage of app.component.ts. It shows yellow on dependency injection in constructor

cc @kulshekhar

@kulshekhar
Copy link
Owner

@AhnpGit I'll try to take a look at this after work today

@kulshekhar
Copy link
Owner

@AhnpGit I tested this and saw the issue you've stated. However, I'm not sure what the cause of this is.

Also, while I use angular and I use ts-jest, I don't use ts-jest with angular so I'm not very familiar with the preset.

As ts-jest doesn't handle coverage at all (it's handled in jest), I'm not sure there's anything that can be done here to fix this.

@ahnpnl
Copy link
Collaborator Author

ahnpnl commented Mar 1, 2018

The preset was created by @thymikee and he refused ts-jest in the precessor. Probably I need to debug jest coverage. Btw, is the runner jest-runner-tsc does the same thing as ts-jest ?

@thymikee
Copy link
Contributor

thymikee commented Mar 1, 2018

@AhnpGit can you test this with Jest master? We recently merged this: jestjs/jest#5692

@ahnpnl
Copy link
Collaborator Author

ahnpnl commented Mar 1, 2018

Sure i'll check. I saw that change in master yesterday but I didn't understand quite well what it can help

@thymikee
Copy link
Contributor

thymikee commented Mar 1, 2018

Ah, forgot we use TS for JS files too, so that shouldn't change anything.

@ahnpnl
Copy link
Collaborator Author

ahnpnl commented Mar 1, 2018

I suppose I need to debug jest coverage then :)

@ahnpnl
Copy link
Collaborator Author

ahnpnl commented Mar 3, 2018

@kulshekhar I just found out something suspicious about Angular itself. If I write test for a normal ts file, coverage is always correct no matter how many things I define in the constructor of the ts class. However, when adding Angular decorator @component, the coverage immediately goes wrong on the constructor. Do you think this might cause by TypeScript decorator or Angular itself ?

@kulshekhar
Copy link
Owner

kulshekhar commented Mar 3, 2018

@AhnpGit I'm not 100% certain but there's a chance that Angular's DI is causing this

@ahnpnl
Copy link
Collaborator Author

ahnpnl commented Mar 5, 2018

@kulshekhar you were right. I found out indeed it was caused by Angular DI. DI uses TypeScript feature emitDecoratorMetadata. This option will let TypeScript compiler generates .js file which has something like
__metadata("design:paramtypes", [TestService_1.TestService]) (TestService is my DI service).

I haven't tried to create a decorator with reflect meta yet to test but I think it will have the same issue.

I still don't know what should be the proper fix, but I guess something for Jest I assume. @thymikee do you think so ?

@kulshekhar
Copy link
Owner

@AhnpGit can you check whether v22.4.4 fixes this for you?

@ahnpnl
Copy link
Collaborator Author

ahnpnl commented Apr 19, 2018

@kulshekhar it seems to not working for me

@GeeWee
Copy link
Collaborator

GeeWee commented May 14, 2018

Have you also set the ignore decorator flags? You can view them either in the readme or the changed files inside PR #488

@ahnpnl
Copy link
Collaborator Author

ahnpnl commented May 14, 2018

Yes I tried, but none of them works for decorators which are created by Angular.

@huafu
Copy link
Collaborator

huafu commented Sep 19, 2018

@ahnpnl lemme know if 23.10 doesn't fix this ;-)

@huafu huafu closed this as completed Sep 19, 2018
@danil-z
Copy link

danil-z commented Oct 4, 2018

I'm experiencing the same issue

screen shot 2018-10-03 at 7 12 46 pm

@danil-z
Copy link

danil-z commented Oct 4, 2018

jest@23.6.0
ts-jest "23.10.0-beta.6",

@huafu
Copy link
Collaborator

huafu commented Oct 4, 2018

@danil-z 23.10.3 is out, use it instead it should fix your issue

@blackholegalaxy
Copy link

Still experiencing the same with ts-jest 23.10.3 and jest 23.6.0. Cleared the cache before running the tests.

@huafu
Copy link
Collaborator

huafu commented Oct 4, 2018

@blackholegalaxy please provide a minimal repo to reproduce and, since this issue is old and not opened by you, explain the exact problem which you personally have in this repo ;-)

@akaNightmare
Copy link

akaNightmare commented Oct 12, 2018

@huafu
image

package.json

"jest": {
        "moduleFileExtensions": [
            "js",
            "json",
            "ts"
        ],
        "testEnvironment": "node",
        "setupTestFrameworkScriptFile": "<rootDir>/__tests__/setup.js",
        "testMatch": [
            "**/__tests__/**/*(spec|test).ts",
            "**/?(*.)+(spec|test).ts"
        ],
        "transform": {
            "^.+\\.tsx?": "ts-jest"
        },
        "preset": "ts-jest/presets/js-with-ts",
        "testEnvironmentOptions": {},
        "collectCoverage": true,
        "collectCoverageFrom": [
            "app/services/**/*.ts"
        ],
        "testPathIgnorePatterns": [
            "/node_modules/",
            "dist/"
        ],
        "coverageDirectory": "<rootDir>/__tests__/__coverage__",
        "moduleNameMapper": {
            "^@middlewares(.*)$": "<rootDir>/app/middlewares$1",
            "^@enums(.*)$": "<rootDir>/app/enums$1",
            "^@services(.*)$": "<rootDir>/app/services$1",
            "^@common(.*)$": "<rootDir>/app/common$1",
            "^@root(.*)$": "<rootDir>/app$1",
            "^@package$": "<rootDir>/package.json"
        },
        "globals": {
            "ts-jest": {
                "tsConfig": "./__tests__/tsconfig.test.json"
            }
        }
    }

test.tsconfig.json

"inlineSourceMap": true,
"lib": [
            "es2017"
        ],
        "target": "es2017",

libs/packages

"jest": "^23.6.0",
"ts-jest": "^23.10.4",
"typescript": "3.1.2"

@danil-z
Copy link

danil-z commented Oct 17, 2018

problem remains, can be reproduced on any sample project with angular
Any example from angular site, just inject router to component and make trivial test

@danil-z
Copy link

danil-z commented Oct 17, 2018

here is repo to reproduce

https://github.com/danil-z/covjest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants