karma.conf.js 4.6 KB
Newer Older
B
bryk 已提交
1
// Copyright 2015 Google Inc. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4 5 6
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
B
bryk 已提交
7
//     http://www.apache.org/licenses/LICENSE-2.0
8 9
//
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
// 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,
35
    devDependencies: true,
36 37
  };

38 39 40 41 42
  return wiredep(wiredepOptions).js.concat([
    path.join(conf.paths.frontendTest, '**/*.js'),
    path.join(conf.paths.frontendSrc, '**/*.js'),
    path.join(conf.paths.frontendSrc, '**/*.html'),
  ]);
43 44 45 46 47 48 49
}

/**
 * Exported default function which sets Karma configuration. Required by the framework.
 *
 * @param {!Object} config
 */
50
module.exports = function(config) {
51
  let configuration = {
52
    basePath: '.',
53 54 55 56 57 58 59

    files: getFileList(),

    logLevel: 'WARN',

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

B
bryk 已提交
60 61
    browserNoActivityTimeout: 60 * 1000,  // 60 seconds.

62
    reporters: ['dots', 'coverage'],
S
Sebastian Florek 已提交
63 64

    coverageReporter: {
B
bryk 已提交
65
      dir: conf.paths.coverage,
S
Sebastian Florek 已提交
66 67 68 69 70
      reporters: [
        {type: 'html', subdir: 'html'},
        {type: 'lcovonly', subdir: 'lcov'},
      ],
    },
71

72
    preprocessors: {},  // This field is filled with values later.
73 74 75 76

    plugins: [
      'karma-chrome-launcher',
      'karma-jasmine',
S
Sebastian Florek 已提交
77
      'karma-coverage',
78 79
      'karma-ng-html2js-preprocessor',
      'karma-sourcemap-loader',
80
      'karma-browserify',
81
      'karma-sauce-launcher',
82 83 84 85 86 87 88 89 90
    ],

    // 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: [
S
Sebastian Florek 已提交
91 92 93
        // Browserify transform for the istanbul code coverage tool. Isparta istrumenter for ES6
        // code coverage. TODO(floreks): try to make import work instead of require
        ['browserify-istanbul', {'instrumenter': require('isparta')}],
94
        // Transform ES6 code into ES5 so that browsers can digest it.
B
bryk 已提交
95
        ['babelify'],
96
      ],
97 98 99 100
    },

    // karma-ng-html2js-preprocessor plugin config.
    ngHtml2JsPreprocessor: {
101
      stripPrefix: `${conf.paths.frontendSrc}/`,
102 103
      // Load all template related stuff under ng module as it's loaded with every module.
      moduleName: 'ng',
104
    },
105 106
  };

107
  // Use custom browser configuration when running on Travis CI.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  if (conf.test.useSauceLabs) {
    configuration.reporters.push('saucelabs');

    let testName;
    if (process.env.TRAVIS) {
      testName = `Karma tests ${process.env.TRAVIS_REPO_SLUG}, build ` +
          `${process.env.TRAVIS_BUILD_NUMBER}`;
      if (process.env.TRAVIS_PULL_REQUEST !== 'false') {
        testName += `, PR: https://github.com/${process.env.TRAVIS_REPO_SLUG}/pull/` +
            `${process.env.TRAVIS_PULL_REQUEST}`;
      }
    } else {
      testName = 'Local karma tests';
    }

    configuration.sauceLabs = {
      testName: testName,
      connectOptions: {port: 5757, logfile: 'sauce_connect.log'},
      public: 'public',
    },
    configuration.customLaunchers = {
      sl_chrome: {base: 'SauceLabs', browserName: 'chrome'},
      sl_firefox: {base: 'SauceLabs', browserName: 'firefox'},
      sl_ie: {base: 'SauceLabs', browserName: 'internet explorer'},
    };
    configuration.browsers = Object.keys(configuration.customLaunchers);
  } else {
    configuration.browsers = ['Chrome'];
136 137
  }

138 139 140 141 142 143 144 145
  // 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);
146
};