const webpack = require('webpack');

module.exports = function karmaConfig(config) {
  config.set({
    frameworks: ['jasmine'],
    plugins: [
      'karma-jasmine',
      'karma-sourcemap-writer',
      'karma-sourcemap-loader',
      'karma-webpack',
      'karma-phantomjs-launcher',
    ],

    files: ['./src/karma.entry.js'],
    preprocessors: {
      // Run tests and tested code through webpack to handle template
      // and css imports. Also enable sourcemaps in Karma for more
      // useful error stack traces.
      './src/karma.entry.js': ['webpack', 'sourcemap'],
      './src/**/!(*.test).js': ['sourcemap'],
    },

    webpack: {
      entry: './src/karma.entry.js',
      // Used to provide real line numbers for error stack traces.
      devtool: 'inline-source-map',
      module: {
        rules: [
          // Lint source code.
          {
            enforce: 'pre',
            test: /\.js$/,
            exclude: /node_modules/,
            use: 'eslint-loader',
          },
          // Allows us to use ES6 in our test and source files.
          {
            test: /\.js$/,
            use: 'babel-loader',
            exclude: /node_modules\/(?!(autonumeric)\/).*/,
          },
          // Handles importing HTML templates into our component JS files.
          {
            test: /\.html$/,
            use: 'html-loader',
          },
          // Ignore imports of CSS or asset files when unit testing.
          {
            test: /\.(scss|css|svg|eot|woff|woff2|ttf)/,
            use: 'null-loader',
          },
        ],
      },
      plugins: [
        // Ensure jQuery gets shimmed onto window when running the
        // unit tests or directives that use non-JQLite features will
        // blow up.
        new webpack.ProvidePlugin({
          $: 'jquery',
          jQuery: 'jquery',
          'window.jQuery': 'jquery',
        }),
      ],
    },

    // prevent console spamming when running in Karma.
    webpackServer: { noInfo: true },

    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_DEBUG,
    autoWatch: true,
    browsers: ['PhantomJS'],
  });
};
