karma.conf.js 3.94 KB
Newer Older
1
/* eslint-env node */
2
const path = require('path')
Johann-S's avatar
Johann-S committed
3
const ip = require('ip')
XhmikosR's avatar
XhmikosR committed
4
5
6
7
const babel = require('rollup-plugin-babel')
const istanbul = require('rollup-plugin-istanbul')
const resolve = require('rollup-plugin-node-resolve')

Johann-S's avatar
Johann-S committed
8
9
10
11
const {
  browsers,
  browsersKeys
} = require('./browsers')
12

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

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

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

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

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

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

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

Johann-S's avatar
Johann-S committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
68
69
    }),
    resolve()
Johann-S's avatar
Johann-S committed
70
71
72
73
74
75
76
77
  ],
  output: {
    format: 'iife',
    name: 'bootstrapTest',
    sourcemap: 'inline'
  }
}

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

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

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

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

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

XhmikosR's avatar
XhmikosR committed
170
module.exports = karmaConfig => {
Johann-S's avatar
Johann-S committed
171
172
173
  // 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)
174
}