karma.conf.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* eslint-env node */
  2. 'use strict'
  3. const path = require('path')
  4. const ip = require('ip')
  5. const { babel } = require('@rollup/plugin-babel')
  6. const istanbul = require('rollup-plugin-istanbul')
  7. const { nodeResolve } = require('@rollup/plugin-node-resolve')
  8. const replace = require('@rollup/plugin-replace')
  9. const {
  10. browsers,
  11. browsersKeys
  12. } = require('./browsers')
  13. const ENV = process.env
  14. const BROWSERSTACK = Boolean(ENV.BROWSERSTACK)
  15. const DEBUG = Boolean(ENV.DEBUG)
  16. const JQUERY_TEST = Boolean(ENV.JQUERY)
  17. const frameworks = [
  18. 'jasmine'
  19. ]
  20. const plugins = [
  21. 'karma-jasmine',
  22. 'karma-rollup-preprocessor'
  23. ]
  24. const reporters = ['dots']
  25. const detectBrowsers = {
  26. usePhantomJS: false,
  27. postDetection(availableBrowser) {
  28. // On CI just use Chrome
  29. if (ENV.CI === true) {
  30. return ['ChromeHeadless']
  31. }
  32. if (availableBrowser.includes('Chrome')) {
  33. return DEBUG ? ['Chrome'] : ['ChromeHeadless']
  34. }
  35. if (availableBrowser.includes('Chromium')) {
  36. return DEBUG ? ['Chromium'] : ['ChromiumHeadless']
  37. }
  38. if (availableBrowser.includes('Firefox')) {
  39. return DEBUG ? ['Firefox'] : ['FirefoxHeadless']
  40. }
  41. throw new Error('Please install Chrome, Chromium or Firefox')
  42. }
  43. }
  44. const conf = {
  45. basePath: '../..',
  46. port: 9876,
  47. colors: true,
  48. autoWatch: false,
  49. singleRun: true,
  50. concurrency: Number.POSITIVE_INFINITY,
  51. client: {
  52. clearContext: false
  53. },
  54. files: [
  55. 'node_modules/hammer-simulator/index.js',
  56. {
  57. pattern: 'js/tests/unit/**/!(jquery).spec.js',
  58. watched: !BROWSERSTACK
  59. }
  60. ],
  61. preprocessors: {
  62. 'js/tests/unit/**/*.spec.js': ['rollup']
  63. },
  64. rollupPreprocessor: {
  65. plugins: [
  66. replace({
  67. 'process.env.NODE_ENV': '"dev"',
  68. preventAssignment: true
  69. }),
  70. istanbul({
  71. exclude: [
  72. 'node_modules/**',
  73. 'js/tests/unit/**/*.spec.js',
  74. 'js/tests/helpers/**/*.js'
  75. ]
  76. }),
  77. babel({
  78. // Only transpile our source code
  79. exclude: 'node_modules/**',
  80. // Inline the required helpers in each file
  81. babelHelpers: 'inline'
  82. }),
  83. nodeResolve()
  84. ],
  85. output: {
  86. format: 'iife',
  87. name: 'bootstrapTest',
  88. sourcemap: 'inline'
  89. }
  90. }
  91. }
  92. if (BROWSERSTACK) {
  93. conf.hostname = ip.address()
  94. conf.browserStack = {
  95. username: ENV.BROWSER_STACK_USERNAME,
  96. accessKey: ENV.BROWSER_STACK_ACCESS_KEY,
  97. build: `bootstrap-${new Date().toISOString()}`,
  98. project: 'Bootstrap',
  99. retryLimit: 2
  100. }
  101. plugins.push('karma-browserstack-launcher', 'karma-jasmine-html-reporter')
  102. conf.customLaunchers = browsers
  103. conf.browsers = browsersKeys
  104. reporters.push('BrowserStack', 'kjhtml')
  105. } else if (JQUERY_TEST) {
  106. frameworks.push('detectBrowsers')
  107. plugins.push(
  108. 'karma-chrome-launcher',
  109. 'karma-firefox-launcher',
  110. 'karma-detect-browsers'
  111. )
  112. conf.detectBrowsers = detectBrowsers
  113. conf.files = [
  114. 'node_modules/jquery/dist/jquery.slim.min.js',
  115. {
  116. pattern: 'js/tests/unit/jquery.spec.js',
  117. watched: false
  118. }
  119. ]
  120. } else {
  121. frameworks.push('detectBrowsers')
  122. plugins.push(
  123. 'karma-chrome-launcher',
  124. 'karma-firefox-launcher',
  125. 'karma-detect-browsers',
  126. 'karma-coverage-istanbul-reporter'
  127. )
  128. reporters.push('coverage-istanbul')
  129. conf.detectBrowsers = detectBrowsers
  130. conf.coverageIstanbulReporter = {
  131. dir: path.resolve(__dirname, '../coverage/'),
  132. reports: ['lcov', 'text-summary'],
  133. thresholds: {
  134. emitWarning: false,
  135. global: {
  136. statements: 90,
  137. branches: 89,
  138. functions: 90,
  139. lines: 90
  140. }
  141. }
  142. }
  143. if (DEBUG) {
  144. conf.hostname = ip.address()
  145. plugins.push('karma-jasmine-html-reporter')
  146. reporters.push('kjhtml')
  147. conf.singleRun = false
  148. conf.autoWatch = true
  149. }
  150. }
  151. conf.frameworks = frameworks
  152. conf.plugins = plugins
  153. conf.reporters = reporters
  154. module.exports = karmaConfig => {
  155. conf.logLevel = karmaConfig.LOG_ERROR
  156. karmaConfig.set(conf)
  157. }