11

タイプスクリプトのコード カバレッジを取得しようとしています karma.conf でイスタンブールを使用するカルマ フレームワークのコード タイプスクリプト ファイルが含まれており、カルマ タイプスクリプト プリプロセッサによって、タイプスクリプト コードのユニット テストとコード カバレッジを実行できますが、コード カバレッジ レポートが表示されますトランスパイル JavaScript コード

typescript コードのカバレッジ レポートを取得するにはどうすればよいですか?

これが私のkarma.confファイルです。

module.exports = function(config) {
  config.set({

    // base path, that will be used to resolve files and exclude
    basePath: '',


    // frameworks to use
    frameworks: ['jasmine'],

    preprocessors: {
        'src/**/*.ts': ['typescript', 'coverage'],
        'test/**/*.ts': ['typescript']
    },
    typescriptPreprocessor: {
        options: {
            sourceMap: false, // (optional) Generates corresponding .map file.
            target: 'ES5', // (optional) Specify ECMAScript target version: 'ES3' (default), or 'ES5'
            module: 'amd', // (optional) Specify module code generation: 'commonjs' or 'amd'
            noImplicitAny: true, // (optional) Warn on expressions and declarations with an implied 'any' type.
            noResolve: false, // (optional) Skip resolution and preprocessing.
            removeComments: true, // (optional) Do not emit comments to output.
            concatenateOutput: false // (optional) Concatenate and emit output to single file. By default true if module option is omited, otherwise false.
        },
        // extra typing definitions to pass to the compiler (globs allowed)
        // transforming the filenames
        transformPath: function (path) {
            return path.replace(/\.ts$/, '.js');
        }

        //options: {
        //    sourceMap: true,
        //}
    },

    // list of files / patterns to load in the browser
    files: [

      'src/**/*.ts',
      'test/**/*.ts'
    ],


    // list of files to exclude
    exclude: [
      
    ],
    // test results reporter to use
    // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
    reporters: ['progress','coverage'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // Start these browsers, currently available:
    // - Chrome
    // - ChromeCanary
    // - Firefox
    // - Opera (has to be installed with `npm install karma-opera-launcher`)
    // - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
    // - PhantomJS
    // - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
    browsers: ['PhantomJS'],


    // If browser does not capture in given timeout [ms], kill it
    captureTimeout: 60000,


    // Continuous Integration mode
    // if true, it capture browsers, run tests and exit
    singleRun: false,
    plugins: [
  'karma-jasmine',
  'karma-chrome-launcher',
  'karma-phantomjs-launcher',
  'karma-typescript-preprocessor',
  'karma-coverage'
  //require('../../../node_modules/karma-typescript-preprocessor/index.js')
    ]

  });
};

4

3 に答える 3

13

インストールkarma-typescript:

npm install karma-typescript --save-dev

これを karma.conf.js に入れてください:

frameworks: ["jasmine", "karma-typescript"],

files: [
    { pattern: "src/**/*.ts" }
],

preprocessors: {
    "**/*.ts": ["karma-typescript"]
},

reporters: ["progress", "karma-typescript"],

browsers: ["Chrome"]

これにより、Typescript 単体テストがオンザフライで実行され、次のようなイスタンブール html カバレッジが生成されます。

上記の例を実行するには、いくつかのパッケージをインストールする必要があります。

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

これは、バニラの Typescript コードを単体テストするための完全な構成でtsconfig.jsonあり、この場合は必要ありません。examples folderAngular、React などを使用したより複雑な設定については、および で例を見つけることができますintegration tests

于 2016-09-03T12:18:58.720 に答える
1

プロジェクトに instanbul-remap を使用していますが、非常にうまく機能します。カバレッジ レポートを作成するには、次のシェル スクリプトを実行します。

#!/bin/bash

PROJECT_PATH="$(dirname $0)/../"

cd $PROJECT_PATH
echo Creating coverage reports for `pwd`

if [ ! -d "target/dist" ]; then
  echo
  echo "target/dist directory not found. Must compile source with \`npm install\` before running tests."
  echo
  exit 1;
fi

COVERAGE_DIR=target/coverage-raw
REMAP_DIR=target/coverage-ts

mkdir -p $COVERAGE_DIR
mkdir -p $REMAP_DIR

# run coverage on unit tests only
echo Creating coverage reports for unit tests
node_modules/.bin/istanbul cover --dir $COVERAGE_DIR nodeunit `find target/dist/test/ -name *.test.js` > /dev/null

# re-map the coverage report so that typescript sources are shown
echo Remapping coverage reports for typescript
node_modules/.bin/remap-istanbul -i $COVERAGE_DIR/coverage.json -o $REMAP_DIR -t html

echo Coverage report located at $REMAP_DIR/index.html

私たちのプロジェクトでは、ノード アプリケーションであるため、テスト ハーネスとして nodeunit を使用します。ただし、同様のアプローチがカルマにも機能することを期待しています。

于 2016-02-22T19:27:56.197 に答える
1

カルマとkarma-remap-istanbulうまく統合するものがあります。remap-istanbulドキュメンテーションはかなり自明ですが、1 つのこと - コンソールに概要を表示するには、設定text: undefinedを行います (それ以外の場合、テキスト出力はファイルに送られます)。

そのため、カルマから直接カバレッジの概要を取得することは可能ですが、完全な HTML レポートを生成できるようにするには、構成に関してさらに開発が必要なためts、ソースが同じディレクトリで利用できない場合があります。karma.config.js karma-remap-istanbul

于 2016-03-02T20:18:59.737 に答える