karma.conf.js 3.97 KB
Newer Older
1
/* eslint-env node */
XhmikosR's avatar
XhmikosR committed
2
/* eslint no-process-env: 0 */
3

4
const path = require('path')
Johann-S's avatar
Johann-S committed
5
6
7
8
9
const ip = require('ip')
const {
  browsers,
  browsersKeys
} = require('./browsers')
Johann-S's avatar
Johann-S committed
10
11
const babel = require('rollup-plugin-babel')
const istanbul = require('rollup-plugin-istanbul')
Johann-S's avatar
Johann-S committed
12
const resolve = require('rollup-plugin-node-resolve')
13

XhmikosR's avatar
XhmikosR committed
14
const { env } = process
15
16
const browserStack = env.BROWSER === 'true'
const debug = env.DEBUG === 'true'
17
const frameworks = [
Johann-S's avatar
Johann-S committed
18
  'jasmine'
19
20
]

Johann-S's avatar
Johann-S committed
21
const plugins = [
Johann-S's avatar
Johann-S committed
22
23
  'karma-jasmine',
  'karma-rollup-preprocessor'
Johann-S's avatar
Johann-S committed
24
25
]

Johann-S's avatar
Johann-S committed
26
27
28
29
30
const reporters = ['dots']

const detectBrowsers = {
  usePhantomJS: false,
  postDetection(availableBrowser) {
31
    if (env.CI === true || availableBrowser.includes('Chrome')) {
32
      return debug ? ['Chrome'] : ['ChromeHeadless']
Johann-S's avatar
Johann-S committed
33
34
35
    }

    if (availableBrowser.includes('Firefox')) {
36
      return debug ? ['Firefox'] : ['FirefoxHeadless']
Johann-S's avatar
Johann-S committed
37
38
39
40
41
42
43
44
45
46
47
48
49
    }

    throw new Error('Please install Firefox or Chrome')
  }
}

const customLaunchers = {
  FirefoxHeadless: {
    base: 'Firefox',
    flags: ['-headless']
  }
}

Johann-S's avatar
Johann-S committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const rollupPreprocessor = {
  plugins: [
    istanbul({
      exclude: ['js/src/**/*.spec.js']
    }),
    babel({
      // Only transpile our source code
      exclude: 'node_modules/**',
      // Include only required helpers
      externalHelpersWhitelist: [
        'defineProperties',
        'createClass',
        'inheritsLoose',
        'defineProperty',
        'objectSpread2'
      ],
      plugins: [
        '@babel/plugin-proposal-object-rest-spread'
      ]
Johann-S's avatar
Johann-S committed
69
70
    }),
    resolve()
Johann-S's avatar
Johann-S committed
71
72
73
74
75
76
77
78
  ],
  output: {
    format: 'iife',
    name: 'bootstrapTest',
    sourcemap: 'inline'
  }
}

Johann-S's avatar
Johann-S committed
79
80
81
let files = [
  'node_modules/hammer-simulator/index.js'
]
82

Johann-S's avatar
Johann-S committed
83
84
85
86
87
88
89
90
const conf = {
  basePath: '../..',
  port: 9876,
  colors: true,
  autoWatch: false,
  singleRun: true,
  concurrency: Infinity,
  client: {
Johann-S's avatar
Johann-S committed
91
    clearContext: false
Johann-S's avatar
Johann-S committed
92
93
94
  }
}

Johann-S's avatar
Johann-S committed
95
if (browserStack) {
Johann-S's avatar
Johann-S committed
96
97
  conf.hostname = ip.address()
  conf.browserStack = {
98
99
    username: env.BROWSER_STACK_USERNAME,
    accessKey: env.BROWSER_STACK_ACCESS_KEY,
Johann-S's avatar
Johann-S committed
100
101
102
103
    build: `bootstrap-${new Date().toISOString()}`,
    project: 'Bootstrap',
    retryLimit: 2
  }
Johann-S's avatar
Johann-S committed
104
  plugins.push('karma-browserstack-launcher', 'karma-jasmine-html-reporter')
Johann-S's avatar
Johann-S committed
105
106
  conf.customLaunchers = browsers
  conf.browsers = browsersKeys
Johann-S's avatar
Johann-S committed
107
  reporters.push('BrowserStack', 'kjhtml')
Johann-S's avatar
Johann-S committed
108
  files = files.concat([
Johann-S's avatar
Johann-S committed
109
    { pattern: 'js/src/**/*.spec.js', watched: false }
Johann-S's avatar
Johann-S committed
110
  ])
Johann-S's avatar
Johann-S committed
111
112
113
114
  conf.preprocessors = {
    'js/src/**/*.spec.js': ['rollup']
  }
  conf.rollupPreprocessor = rollupPreprocessor
Johann-S's avatar
Johann-S committed
115
116
117
118
119
120
121
122
123
} else {
  frameworks.push('detectBrowsers')
  plugins.push(
    'karma-chrome-launcher',
    'karma-firefox-launcher',
    'karma-detect-browsers',
    'karma-coverage-istanbul-reporter'
  )
  files = files.concat([
Johann-S's avatar
Johann-S committed
124
    { pattern: 'js/src/**/*.spec.js', watched: true }
Johann-S's avatar
Johann-S committed
125
126
  ])
  reporters.push('coverage-istanbul')
Johann-S's avatar
Johann-S committed
127
128
129
130
  conf.preprocessors = {
    'js/src/**/*.spec.js': ['rollup']
  }
  conf.rollupPreprocessor = rollupPreprocessor
Johann-S's avatar
Johann-S committed
131
132
133
  conf.customLaunchers = customLaunchers
  conf.detectBrowsers = detectBrowsers
  conf.coverageIstanbulReporter = {
134
    dir: path.resolve(__dirname, '../coverage/'),
Johann-S's avatar
Johann-S committed
135
136
137
138
139
    reports: ['lcov', 'text-summary'],
    thresholds: {
      emitWarning: false,
      global: {
        statements: 90,
Johann-S's avatar
Johann-S committed
140
141
        branches: 90,
        functions: 90,
Johann-S's avatar
Johann-S committed
142
        lines: 90
143
144
145
146
147
148
149
150
151
152
      },
      each: {
        overrides: {
          'js/src/dom/polyfill.js': {
            statements: 39,
            lines: 37,
            branches: 19,
            functions: 50
          }
        }
153
      }
154
    }
Johann-S's avatar
Johann-S committed
155
156
  }

157
  if (debug) {
Johann-S's avatar
Johann-S committed
158
159
160
    conf.hostname = ip.address()
    plugins.push('karma-jasmine-html-reporter')
    reporters.push('kjhtml')
161
162
163
164
    conf.singleRun = false
    conf.autoWatch = true
  }
}
Johann-S's avatar
Johann-S committed
165
166
167
168
169
170

conf.frameworks = frameworks
conf.plugins = plugins
conf.reporters = reporters
conf.files = files

XhmikosR's avatar
XhmikosR committed
171
module.exports = karmaConfig => {
Johann-S's avatar
Johann-S committed
172
173
174
  // possible values: karmaConfig.LOG_DISABLE || karmaConfig.LOG_ERROR || karmaConfig.LOG_WARN || karmaConfig.LOG_INFO || karmaConfig.LOG_DEBUG
  conf.logLevel = karmaConfig.LOG_ERROR || karmaConfig.LOG_WARN
  karmaConfig.set(conf)
175
}