2017-09-11 8 views
0

webpackとkaramaの設定をアンダーコードのように設定しました。Karma、Mocha、Webpackでイスタンブールカバレッジが動作しない

webpack.config.js

/* global module, process, require, __dirname */ 

const webpack = require('webpack'); 
const ExtractTextPlugin = require('extract-text-webpack-plugin'); 
const fs = require('fs'); 

const gc = require('./global.config.js'); 

// extract style 
const extractCss = new ExtractTextPlugin({ 
    filename: 'css/[name].css' 
}); 
const extractSass = new ExtractTextPlugin({ 
    filename: 'css/[name].css' 
}); 

// dir path 
const sourceDir = gc.basePath+ gc.baseDir + gc.resourceDir; 
const outputDir = gc.baseDir + gc.resourceDir + gc.distDir; 

const devPort = 9090; 
const devOutputDir = gc.resourceDir + gc.distDir; 

// generate entry 
const fileDir = sourceDir + '/js'; 
const read = (dir) => 
    fs.readdirSync(dir) 
     .reduce((files, file) => { 
      const filePath = dir + '/' +file; 
      if (fs.statSync(filePath).isDirectory()) { 
       return files.concat(read(filePath)); 
      } 

      if (file !== 'entry.js') { 
       return files; 
      } 

      return files.concat(filePath); 
     }, []); 
const getEntry = (files) => { 
    const entry = {}; 
    files.forEach(file => { 
     const key = file.replace(fileDir + '/', '').replace('.js', ''); 
     entry[key] = ['babel-polyfill', file]; 
    }); 
    return entry; 
}; 
const entry = getEntry(read(fileDir)); 

// webpack config 
const config = { 
    entry: Object.assign({}, gc.vendors.entry, entry), 
    output: { 
     path: __dirname + outputDir, 
     filename: 'js/[name].bundle.js', 
     publicPath: '/', 
     libraryTarget: 'umd', // export itself to a global var 
     library: 'sc' // name of the global var: 'sc' 
    }, 
    resolve: { 
     modules: ['node_modules'], 
     extensions: ['.js', '.jsx', '.css', '.scss'] 
    }, 
    devtool: 'source-map', 
    cache: true, 
    plugins: [ 
     extractCss, 
     extractSass, 
     new webpack.ProvidePlugin(gc.vendors.providePlugin) 
    ], 
    devServer: { 
     host: '0.0.0.0', 
     publicPath: 'http://localhost:' + devPort + '/', 
     port: devPort, 
     disableHostCheck: true, 
     noInfo: true, 
     inline: true, 
     proxy: { 
      '**': 'http://localhost:8080' 
     } 
    }, 
    module: { 
     rules: [{ 
      enforce: 'pre', 
      test: /\.(js|jsx)?$/, 
      loader: 'eslint-loader', 
      exclude: /(node_modules|bower_components)/ 
     }, { 
      test: /\.(js|jsx)?$/, 
      loader: 'babel-loader', 
      exclude: /(node_modules|bower_components)/, 
      query: { 
       cacheDirectory: true, 
       presets: ['es2015', 'react'] 
      } 
     }, { 
      test: /\.css$/, 
      use: extractCss.extract({ 
       fallback: 'style-loader', 
       use: 'css-loader' 
      }) 
     }, { 
      test: /\.scss$/, 
      use: extractSass.extract({ 
       use: [{ 
        loader: 'css-loader', options: { 
         sourceMap: true 
        } 
       }, { 
        loader: 'sass-loader', options: { 
         sourceMap: true 
        } 
       }], 
       // use style-loader in development 
       fallback: 'style-loader' 
      }) 
     }] 
    } 
}; 

const env = process.env.NODE_ENV; 

// develop phase 
if (env === 'development') { 
    config.output.publicPath = 'http://localhost:' + devPort + devOutputDir; 
} 

// production phase 
if (env === 'production') { 
    config.devtool = '#source-map'; 
    config.plugins = (config.plugins || []).concat([ 
     new webpack.optimize.CommonsChunkPlugin({ 
      name: 'vendors' 
     }), 
     new webpack.DefinePlugin({ 
      'process.env': { 
       NODE_ENV: '"production"' 
      } 
     }), 
     new webpack.optimize.UglifyJsPlugin({ 
      sourceMap: true, 
      compress: { 
       warnings: false 
      } 
     }), 
     new webpack.LoaderOptionsPlugin({ 
      minimize: true 
     }) 
    ]) 
} 

module.exports = config; 

karma.config.js

/* global module, require */ 

const path = require('path'); 
const webpackConfig = require('./webpack.config.js'); 
const gc = require('./global.config.js'); 

// dir path 
const sourceDir = gc.baseDir + gc.resourceDir; 
const sourcePattern = gc.basePath + gc.baseDir + gc.resourceDir + '/js/**/*.js'; 
const testPattern = gc.basePath + gc.baseDir + gc.testDir + '/**/*.spec.js'; 

const preprocessor = {}; 
preprocessor[sourcePattern] = ['webpack', 'coverage']; 
preprocessor[testPattern] = ['webpack']; 

// add webpack test config 
const rules = [{ 
    test: /sinon.*\.js$/, 
    loader: "imports-loader?define=>false,require=>false" 
}, { 
    enforce: 'post', 
    test: /\.js/, 
    exclude: /(node_modules|bower_components)/, 
    include: path.join(sourceDir), // instrument only testing sources with Istanbul, after ts-loader runs 
    loader: 'istanbul-instrumenter-loader' 
}]; 
webpackConfig.module.rules = webpackConfig.module.rules.concat(rules); 
webpackConfig.module['noParse'] = [/sinon/]; 
webpackConfig.resolve['alias'] = {sinon: 'sinon/pkg/sinon'}; 

module.exports = function (config) { 
    config.set({ 
     frameworks: [ 
      'mocha', 
      'chai', 
      'sinon' 
     ], 

     files: [ 
      {pattern: sourcePattern, watched: false}, 
      {pattern: testPattern, watched: false} 
     ], 

     browsers: ['Chrome', 'Firefox'], 

     preprocessors: preprocessor, 

     // report 
     reporters: ['mocha', 'coverage'], 
     coverageReporter: { 
      dir: path.join(__dirname, 'coverage'), 
      reporters: [ 
       { 
        type: 'html', 
        subdir: 'report-html' 
       }, 
       { 
        type: 'lcov', 
        subdir: 'report-lcov' 
       }, 
       { 
        type: 'cobertura', 
        subdir: '.', 
        file: 'cobertura.txt' 
       }, 
       { 
        type: 'text', 
        subdir: '.', 
        file: 'report-text.txt' 
       }, 
       { 
        type: 'text-summary' 
       } 
      ], 
      fixWebpackSourcePaths: true 
     }, 

     // client 
     client: { 
      mocha: { 
       // change Karma's debug.html to the mocha web reporter 
       reporter: 'html', 

       // require specific files after Mocha is initialized 
       require: [require.resolve('bdd-lazy-var/bdd_lazy_var_global')], 

       // custom ui, defined in required file above 
       ui: 'bdd-lazy-var/global', 
      } 
     }, 

     // webpack 
     webpack: webpackConfig, 
     webpackMiddleware: { 
      stats: 'errors-only' 
     }, 
    }); 
}; 

しかし、それは私が予想に動作しません。それはすべてバベルで蒸散されているので、カバレッジが悪い。 jQueryのようなインクルードライブラリについて調べました。

[INFO] =============================== Coverage summary =============================== 
[INFO] Statements : 19.59% (704/3593) 
[INFO] Branches  : 6.11% (208/3402) 
[INFO] Functions : 17.32% (111/641) 
[INFO] Lines  : 19.59% (703/3588) 
[INFO] ================================================================================ 

私の設定で何が問題になっていますか?それは私の全体のコードです。 https://github.com/egaoneko/maven-spring-webpack-scaffold

答えて

0

イスタンブールはES6ではあまりよくありません。ビルドフォルダを作成し、変換されたES6またはJSXコードをプレーンJSに、イスタンブールが理解できるビルドに配置します。それは、私がコードカバレッジのためにイスタンブールを使い始めたときに学んた欠点の1つです。さらに、イスタンブールはあなたのテストを理解できるように、バニラのJavaScriptであなたのテストを書く。私はES6で自分の機能を書いて、mochaとchaiでvanilla Javascriptでテストしました。

私は私のpackage.json

nyc --reporter=html --reporter=text mocha test/**/*.test.js

私のテストのためで npm runにこの作品を使用していますので、私は私のテストを持っていますか?

関連する問題