Code Coverage for Typescript using karma-jasmine and istanbul

前端 未结 3 1573
名媛妹妹
名媛妹妹 2021-02-07 05:44

I am trying to get the Code Coverage for my typescript Code in karma framework using Istanbul in karma.conf typescript files are included and by karma typescript-preprocessor we

3条回答
  •  北海茫月
    2021-02-07 06:09

    Install karma-typescript:

    npm install karma-typescript --save-dev
    

    Put this in your karma.conf.js:

    frameworks: ["jasmine", "karma-typescript"],
    
    files: [
        { pattern: "src/**/*.ts" }
    ],
    
    preprocessors: {
        "**/*.ts": ["karma-typescript"]
    },
    
    reporters: ["progress", "karma-typescript"],
    
    browsers: ["Chrome"]
    

    This will run your Typescript unit tests on the fly and generate Istanbul html coverage that look like this:

    To run the above example you need to install a few packages:

    npm install @types/jasmine jasmine-core karma karma-chrome-launcher karma-cli karma-jasmine karma-typescript typescript

    This is the complete configuration for unit testing vanilla Typescript code, no tsconfig.json needed in this case. For more complex setups with Angular, React etc you can find examples in the examples folder and in the integration tests.

提交回复
热议问题