karma.conf.js 3.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
// Copyright 2015 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview Configuration file for Karma test runner.
 *
 * Specification of Karma config file can be found at:
 * http://karma-runner.github.io/latest/config/configuration-file.html
 */
import path from 'path';
import wiredep from 'wiredep';

import conf from './conf';


/**
 * Returns an array of files required by Karma to run the tests.
 *
 * @return {!Array<string>}
 */
function getFileList() {
  // All app dependencies are required for tests. Include them.
  let wiredepOptions = {
    dependencies: true,
    devDependencies: true
  };

  return wiredep(wiredepOptions).js
    .concat([
      path.join(conf.paths.frontendTest, '**/*.js'),
      path.join(conf.paths.frontendSrc, '**/*.js'),
      path.join(conf.paths.frontendSrc, '**/*.html')
    ]);
}


/**
 * Exported default function which sets Karma configuration. Required by the framework.
 *
 * @param {!Object} config
 */
export default function(config) {
  let configuration = {
    basePath: conf.paths.base,

    files: getFileList(),

    logLevel: 'WARN',

    frameworks: ['jasmine', 'browserify'],

    browsers: ['Chrome'],

    reporters: ['progress'],

    preprocessors: {}, // This field is filled with values later.

    plugins: [
      'karma-chrome-launcher',
      'karma-jasmine',
      'karma-ng-html2js-preprocessor',
      'karma-sourcemap-loader',
      'karma-browserify'
    ],

    // karma-browserify plugin config.
    browserify: {
      // Add source maps to outpus bundles.
      debug: true,
      // Make 'import ...' statements relative to the following paths.
      paths: [conf.paths.frontendSrc, conf.paths.frontendTest],
      transform: [
        // Transform ES6 code into ES5 so that browsers can digest it.
        'babelify'
      ]
    },

    // karma-ng-html2js-preprocessor plugin config.
    ngHtml2JsPreprocessor: {
      stripPrefix: conf.paths.frontendSrc + '/', 
      moduleName: conf.frontend.moduleName
    }
  };

  // Convert all JS code written ES6 with modules to ES5 bundles that browsers can digest.
  configuration.preprocessors[path.join(conf.paths.frontendTest, '**/*.js')] = ['browserify'];
  configuration.preprocessors[path.join(conf.paths.frontendSrc, '**/*.js')] = ['browserify'];

  // Convert HTML templates into JS files that serve code through $templateCache.
  configuration.preprocessors[path.join(conf.paths.frontendSrc, '**/*.html')] = ['ng-html2js'];

  config.set(configuration);
}