I am using Karma Angular Mocha Chai
in my project. I am doing TDD
and would like to test my changes. I did a console.log
in my test.js file but karma console
does not show that. I'm not sure how to even enable that?
Here is my karma.config.js
:
module.exports = function(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
frameworks: ['mocha', 'chai'],
// list of files / patterns to load in the browser
files: [
'src/main/webapp/js/angular.js',
'src/main/webapp/js/angular-simple-logger.js',
'src/main/webapp/js/services/myservice-test.js',
'src/main/webapp/js/controllers/*.js',
'src/test/webapp/**/*.js'
],
// list of files to exclude
exclude: [
],
client : {
captureConsole : true
},
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// 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: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}
Here is my package.json
:
{
"name": "myapp",
"version": "0.0.1",
"description": "my App",
"main": "src/main/webapp/index.html",
"directories": {
"doc": "docs"
},
"scripts": {
"test": "node_modules/.bin/karma start karma.config.js"
},
"repository": {
"type": "git",
"url": ""
},
"author": "",
"license": "ISC",
"devDependencies": {
"chai": "^3.5.0",
"karma": "^0.13.22",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^0.2.2",
"karma-mocha": "^0.2.2",
"mocha": "^2.4.5"
},
"dependencies": {
"angular": "^1.5.1",
"angular-mocks": "^1.5.1"
}
}
Programmers frequently use console. log to record errors or other informational messages in their Angular applications. Although this is fine while debugging your application, it's not a best practice for production applications.
The Jasmine test runner is just another web page made with HTML, CSS and JavaScript. This means you can debug it in the browser using the developer tools. Focus the browser window and open the developer tools. In Chrome, Firefox and Edge, you can use the F12 key.
Karma is the default test runner for applications created using Angular CLI. Being a direct product of the AngularJS team, Karma is very well suited for testing Angular or any other JavaScript-based project. Running on Node.
The console. log() method outputs a message to the web console. The message may be a single string (with optional substitution values), or it may be any one or more JavaScript objects.
Add
browserConsoleLogOptions: {
level: 'log'
},
to your config.set. This is a new (karma 1.5.0, circa 2017-02-20) refinement of karma behavior. See further discussion here: https://github.com/karma-runner/karma/issues/2582
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With