karma.conf.js 4.0 KB

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