karma.conf.js 3.87 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
const babel = require('rollup-plugin-babel')
const istanbul = require('rollup-plugin-istanbul')
6
const resolve = require('@rollup/plugin-node-resolve')
XhmikosR's avatar
XhmikosR committed
7

Johann-S's avatar
Johann-S committed
8
9
10
11
const {
  browsers,
  browsersKeys
} = require('./browsers')
12
const babelHelpers = require('../../build/babel-helpers.js')
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
50
51
52
53
54
55
56
57
    }

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

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

const conf = {
  basePath: '../..',
  port: 9876,
  colors: true,
  autoWatch: false,
  singleRun: true,
  concurrency: Infinity,
  client: {
Johann-S's avatar
Johann-S committed
58
    clearContext: false
59
60
61
  },
  files: [
    'node_modules/hammer-simulator/index.js',
62
    { pattern: 'js/tests/unit/**/*.spec.js', watched: !browserStack }
63
64
  ],
  preprocessors: {
65
    'js/tests/unit/**/*.spec.js': ['rollup']
66
67
68
69
  },
  rollupPreprocessor: {
    plugins: [
      istanbul({
70
        exclude: ['js/tests/unit/**/*.spec.js', 'js/tests/helpers/**/*.js']
71
72
73
74
75
      }),
      babel({
        // Only transpile our source code
        exclude: 'node_modules/**',
        // Include only required helpers
76
        externalHelpersWhitelist: babelHelpers,
77
78
79
80
81
82
83
84
85
86
87
        plugins: [
          '@babel/plugin-proposal-object-rest-spread'
        ]
      }),
      resolve()
    ],
    output: {
      format: 'iife',
      name: 'bootstrapTest',
      sourcemap: 'inline'
    }
Johann-S's avatar
Johann-S committed
88
89
90
  }
}

Johann-S's avatar
Johann-S committed
91
if (browserStack) {
Johann-S's avatar
Johann-S committed
92
93
  conf.hostname = ip.address()
  conf.browserStack = {
94
95
    username: env.BROWSER_STACK_USERNAME,
    accessKey: env.BROWSER_STACK_ACCESS_KEY,
Johann-S's avatar
Johann-S committed
96
97
98
99
    build: `bootstrap-${new Date().toISOString()}`,
    project: 'Bootstrap',
    retryLimit: 2
  }
Johann-S's avatar
Johann-S committed
100
  plugins.push('karma-browserstack-launcher', 'karma-jasmine-html-reporter')
Johann-S's avatar
Johann-S committed
101
102
  conf.customLaunchers = browsers
  conf.browsers = browsersKeys
XhmikosR's avatar
XhmikosR committed
103
104
105
106
107
  conf.captureTimeout = 300000
  conf.browserDisconnectTolerance = 0
  conf.browserDisconnectTimeout = 300000
  conf.browserSocketTimeout = 120000
  conf.browserNoActivityTimeout = 300000
Johann-S's avatar
Johann-S committed
108
  reporters.push('BrowserStack', 'kjhtml')
Johann-S's avatar
Johann-S committed
109
110
111
112
113
114
115
116
117
118
119
120
} else {
  frameworks.push('detectBrowsers')
  plugins.push(
    'karma-chrome-launcher',
    'karma-firefox-launcher',
    'karma-detect-browsers',
    'karma-coverage-istanbul-reporter'
  )
  reporters.push('coverage-istanbul')
  conf.customLaunchers = customLaunchers
  conf.detectBrowsers = detectBrowsers
  conf.coverageIstanbulReporter = {
121
    dir: path.resolve(__dirname, '../coverage/'),
Johann-S's avatar
Johann-S committed
122
123
124
125
126
    reports: ['lcov', 'text-summary'],
    thresholds: {
      emitWarning: false,
      global: {
        statements: 90,
Johann-S's avatar
Johann-S committed
127
128
        branches: 90,
        functions: 90,
Johann-S's avatar
Johann-S committed
129
        lines: 90
130
131
132
133
134
135
136
137
138
139
      },
      each: {
        overrides: {
          'js/src/dom/polyfill.js': {
            statements: 39,
            lines: 37,
            branches: 19,
            functions: 50
          }
        }
140
      }
141
    }
Johann-S's avatar
Johann-S committed
142
143
  }

144
  if (debug) {
Johann-S's avatar
Johann-S committed
145
146
147
    conf.hostname = ip.address()
    plugins.push('karma-jasmine-html-reporter')
    reporters.push('kjhtml')
148
149
150
151
    conf.singleRun = false
    conf.autoWatch = true
  }
}
Johann-S's avatar
Johann-S committed
152
153
154
155
156

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

XhmikosR's avatar
XhmikosR committed
157
module.exports = karmaConfig => {
Johann-S's avatar
Johann-S committed
158
159
160
  // 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)
161
}