Skip to content

Instantly share code, notes, and snippets.

@WuglyakBolgoink
Last active December 15, 2021 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WuglyakBolgoink/953dfc2fab7aa46daf871e003429c437 to your computer and use it in GitHub Desktop.
Save WuglyakBolgoink/953dfc2fab7aa46daf871e003429c437 to your computer and use it in GitHub Desktop.
angularJS karma config with puppeteer
{
"presets": [
[
"@babel/preset-env",
{
"modules": false
}
]
],
"plugins": [
"@babel/plugin-proposal-optional-chaining",
"angularjs-annotate"
],
"env": {
"testing": {
"plugins": [
[
"istanbul",
{
"exclude": [
"**/*.spec.js"
]
}
]
],
"compact": false
}
},
"comments": false,
"compact": true
}
/* eslint-disable import/no-extraneous-dependencies */
'use strict';
process.env.CHROME_BIN = require('puppeteer').executablePath();
console.log('process.env.CHROME_BIN', process.env.CHROME_BIN);
const path = require('path');
module.exports = (config) => {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
// NOTE: 'parallel' must be the first framework in the list
frameworks: [
// 'parallel',
'jasmine'
],
customContextFile: './configs/tests/karma/context.html',
customDebugFile: './configs/tests/karma/debug.html',
// list of files / patterns to load in the browser
files: [
'node_modules/jquery/dist/jquery.min.js',
'node_modules/lodash/lodash.min.js',
'node_modules/angular/angular.js',
'node_modules/angular-mocks/angular-mocks.js',
'node_modules/angular-route/angular-route.min.js',
'node_modules/angular-sanitize/angular-sanitize.min.js',
'node_modules/angular-i18n/angular-locale_de-de.js',
'node_modules/angular-messages/angular-messages.min.js',
// ...insert your other libraries
// Moment JS
'node_modules/moment/min/moment.min.js',
'node_modules/moment/locale/de.js',
// insert your correct pattern-path to each folder and extensions
'www/!(lib)/**/*.html',
'www/!(lib)/**/*.module.js',
'www/!(lib)/**/*.js'
],
// list of files / patterns to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'www/!(lib)/**/*.html': ['ng-html2js'],
'www/!(lib)/**/*.js': ['babel']
},
parallelOptions: {
executors: 4, // Defaults to cpu-count - 1
shardStrategy: 'round-robin'
// shardStrategy: 'description-length'
// shardStrategy: 'custom'
// customShardStrategy: function(config) {
// config.executors // number, the executors set above
// config.shardIndex // number, the specific index for the shard currently running
// config.description // string, the name of the top-level describe string. Useful // for determining how to shard the current specs
// return config.
// }
},
ngHtml2JsPreprocessor: {
// strip this from the file path
stripPrefix: 'www/',
// - setting this option will create only a single module that contains templates
// from all the files, so you can load them all with module('foo')
// - you may provide a function(htmlPath, originalPath) instead of a string
// if you'd like to generate modules dynamically
// htmlPath is a originalPath stripped and/or prepended
// with all provided suffixes and prefixes
moduleName: 'MyApp',
// or define a custom transform function
// - cacheId returned is used to load template
// module(cacheId) will return template at filepath
cacheIdFromPath: (filepath) => {
return filepath.replace(/^www\//, '').replace(/\\/g, '/');
},
standalone: false,
htmlMinifier: false
},
// test results reporter to use
// possible values: 'dots', 'progress', 'spec', etc...
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: [
'spec'
// 'sonarqubeUnit',
// 'coverage-istanbul'
],
// any of these options are valid: https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-api/lib/config.js#L33-L39
coverageIstanbulReporter: {
// reports can be any that are listed here: https://github.com/istanbuljs/istanbuljs/tree/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib
reports: ['html', 'lcovonly', 'text-summary'],
// base output directory. If you include %browser% in the path it will be replaced with the karma browser name
dir: path.join(__dirname, 'reports/coverage'),
// Combines coverage information from multiple browsers into one report rather than outputting a report
// for each browser.
combineBrowserReports: true,
// if using webpack and pre-loaders, work around webpack breaking the source path
fixWebpackSourcePaths: true,
// Omit files with no statements, no functions and no branches from the report
skipFilesWithNoCoverage: true,
// Most reporters accept additional config options. You can pass these through the `report-config` option
'report-config': {
// all options available at: https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/html/index.js#L135-L137
html: {
// outputs the report in ./coverage/html
subdir: 'html'
}
},
// enforce percentage thresholds
// anything under these percentages will cause karma to fail with an exit code of 1 if not running in watch mode
// thresholds: {
// emitWarning: false, // set to `true` to not fail the test command when thresholds are not met
// // thresholds for all files
// global: {
// statements: 100,
// lines: 100,
// branches: 100,
// functions: 100
// },
// // thresholds per file
// each: {
// statements: 100,
// lines: 100,
// branches: 100,
// functions: 100,
// overrides: {
// 'baz/component/**/*.js': {
// statements: 98
// }
// }
// }
// },
verbose: false, // output config used by istanbul for debugging
// `instrumentation` is used to configure Istanbul API package.
instrumentation: {
// To include `node_modules` code in the report.
'default-excludes': false
}
},
specReporter: {
maxLogLines: 5, // limit number of lines logged per test
suppressErrorSummary: true, // do not print error summary
suppressFailed: false, // do not print information about failed tests
suppressPassed: false, // do not print information about passed tests
suppressSkipped: true, // do not print information about skipped tests
showSpecTiming: false, // print the time elapsed for each spec
failFast: false // test would finish with error when a first fail occurs.
},
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['ChromeHeadlessCI'], // 'Chrome', 'ChromeHeadless', 'ChromeNoSandboxHeadless'
// How long will Karma wait for a message from a browser before disconnecting from it (in ms).
browserNoActivityTimeout: 60000,
customLaunchers: {
ChromeHeadlessCI: {
base: 'ChromeHeadless',
flags: [
'--headless',
'--no-sandbox',
'--disable-gpu',
// https://stackoverflow.com/a/56941767
'--disable-dev-shm-usage'
]
},
ChromeNoSandboxHeadless: {
base: 'Chrome',
flags: [
'--no-sandbox',
// See https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md
'--headless',
'--disable-gpu',
// Without a remote debugging port, Google Chrome exits immediately.
'--remote-debugging-port=9222',
// https://stackoverflow.com/a/56941767
'--disable-dev-shm-usage'
]
}
},
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,
failOnEmptyTestSuite: false,
// Concurrency level
// how many browser should be started simultaneous
concurrency: 1,
// Configure how the browser console is logged with the following properties, all of which are optional:
browserConsoleLogOptions: {
level: 'debug',
format: '%b %T: %m',
terminal: false
}
});
};
{
// ...
"scripts": {
"test": "cross-env BABEL_ENV=testing karma start --no-auto-watch",
},
// ...
"devDependencies": {
"@babel/cli": "7.16.0",
"@babel/core": "7.16.0",
"@babel/eslint-parser": "7.16.3",
"@babel/plugin-proposal-optional-chaining": "7.16.0",
"@babel/preset-env": "7.16.4",
"@babel/register": "7.16.0",
"angular-mocks": "1.8.2",
"babel-plugin-angularjs-annotate": "0.10.0",
"babel-plugin-istanbul": "6.1.1",
"babel-plugin-transform-remove-console": "6.9.4",
"babel-plugin-transform-remove-debugger": "6.9.4",
"jasmine-core": "3.10.1",
"karma": "6.3.9",
"karma-babel-preprocessor": "8.0.1",
"karma-chrome-launcher": "3.1.0",
"karma-coverage-istanbul-reporter": "3.0.3",
"karma-jasmine": "4.0.1",
"karma-ng-html2js-preprocessor": "1.0.0",
"karma-parallel": "0.3.1",
"karma-spec-reporter": "0.0.32",
"puppeteer": "13.0.0",
"@types/angular": "1.8.3",
"@types/angular-mocks": "1.7.1",
"@types/angular-route": "1.7.2",
"@types/angular-sanitize": "1.8.1",
"@types/jasmine": "3.10.2",
"cross-env": "7.0.3",
}
// ....
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment