Ionic 3 - Angular when import TestBed get Error : afterAll\nUncaught - javascript

I'm working on an Ionic 3 App and I have a problem when I want to use TestBed class. I use these commands to create the project.
ionic start myApp tabs
npm install karma-cli -g
npm install karma --save-dev
npm install jasmine --save-dev
npm install karma-jasmine --save-dev
npm install #types/jasmine --save-dev
npm install jasmine-core --save-dev
npm install ts-node --save-dev
npm install angular-cli --save-dev
I use karma to create this karma.conf.js :
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
'src/**/*Spec.js'
],
exclude: [
],
preprocessors: {
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
concurrency: Infinity
})
}
I create the src/app/pages/home/homeSpec.js file with this content :
import { TestBed } from '#angular/core';
describe("some tests", () => {
it("True", function () {
expect(true).toEqual(true);
});
});
I get the following output when I use karma start :
------------------- without TestBed import ----------------
27 02 2018 23:45:55.793:WARN [karma]: No captured browser, open http://localhost:9876/
27 02 2018 23:45:55.807:INFO [karma]: Karma v2.0.0 server started at http://0.0.0.0:9876/
27 02 2018 23:45:55.808:INFO [launcher]: Launching browser Chrome with unlimited concurrency
27 02 2018 23:45:55.812:INFO [launcher]: Starting browser Chrome
27 02 2018 23:45:57.600:INFO [Chrome 64.0.3282 (Mac OS X 10.13.3)]: Connected on socket ysC5F7d28isYt8oAAAAA with id 47800734
Chrome 64.0.3282 (Mac OS X 10.13.3): Executed 1 of 1 SUCCESS (0.002 secs / 0 secs)
------------------- with TestBed import ----------------
27 02 2018 23:46:04.564:INFO [watcher]: Changed file "/Volumes/JedemaKey/ionic_book/Chapitres/ionic/testtest/myApp/src/pages/home/homeSpec.js".
Chrome 64.0.3282 (Mac OS X 10.13.3) ERROR
{
"message": "An error was thrown in afterAll\nUncaught SyntaxError: Unexpected token {",
"str": "An error was thrown in afterAll\nUncaught SyntaxError: Unexpected token {"
}
Chrome 64.0.3282 (Mac OS X 10.13.3): Executed 0 of 0 ERROR (0.018 secs / 0 secs)
Why I get this error ?
How can I import TestBed class ?
Edit
I get same error il I can to import another class like in the following code :
import { Component } from '#angular/core';
// This line works in .ts module in same directory.
describe("Test de la page informations", () => {
it("True", function () {
expect(true).toEqual(true);
});
});
I get the same error if I give .ts extension to file...
Do you have a solution ?

Finnaly, I found solution in this Github page (https://github.com/ionic-team/ionic-unit-testing-example/issues/79). I downgrade jasmine version to 2.99.0 by using this command :
npm install --save-dev jasmine#^2.99.0

Related

[phantomjs.launcher]: Fatal Windows exception, code 0xc0000005

Firstly, this could be a duplicated issue from PhantomJS has crashed - Fatal Windows exception, code 0xc0000005. I tried the solution which was given there but it is still not working for me
I set up a webpack 4 (4.16.5) project with karma 4.0.0 (before I used karma 1.x.x but the error was still the same). It starts happening since I added more unit testing to the project (although not too much test cases).
So here's what I get when I run the test (not all the time, for example if I run it 10 times, it usually work 2-3 times)
PhantomJS 2.1.1 (Windows 8.0.0): Executed 0 of 37 SUCCESS (0 secs / 0 secs)
12 02 2019 14:01:09.798:ERROR [phantomjs.launcher]: Fatal Windows exception, code 0xc0000005.
PhantomJS has crashed. Please read the bug reporting guide at
<http://phantomjs.org/bug-reporting.html> and file a bug report.
12 02 2019 14:01:09.910:ERROR [launcher]: PhantomJS crashed.
Fatal Windows exception, code 0xc0000005.
PhantomJS has crashed. Please read the bug reporting guide at
<http://phantomjs.org/bug-reporting.html> and file a bug report.
12 02 2019 14:01:09.910:ERROR [launcher]: PhantomJS stdout:
12 02 2019 14:01:09.911:ERROR [launcher]: PhantomJS stderr: Fatal Windows exception, code 0xc0000005.
PhantomJS has crashed. Please read the bug reporting guide at
<http://phantomjs.org/bug-reporting.html> and file a bug report.
12 02 2019 14:01:09.915:INFO [launcher]: Trying to start PhantomJS again (1/2).
PhantomJS 2.1.1 (Windows 8.0.0) ERROR
Disconnectedreconnect failed before timeout of 2000ms (transport error)
PhantomJS 2.1.1 (Windows 8.0.0): Executed 0 of 37 DISCONNECTED (5.91 secs / 0 secs)
I follow the link http://phantomjs.org/bug-reporting.html but can't find any valuable information.
Here's the content of my karma.conf.ts
module.exports = function karmaConfig(config) {
config.set({
singleRun: true,
frameworks: [
'jasmine'
],
files: [
'sdk/**/*.spec.ts'
],
preprocessors: {
'sdk/**/*.spec.ts': ['webpack', 'sourcemap'],
'sdk/**/!(*.spec).ts': ['coverage']
},
browsers: [
'PhantomJS'
],
reporters: [
'progress',
'coverage',
'junit'
],
coverageReporter: {
dir: 'coverage/',
reporters: [
{ type: 'text-summary' },
{ type: 'html' },
{
type: 'lcov',
dir: 'reports',
subdir: 'coverage'
}
]
},
junitReporter: {
outputFile: 'reports/junit/TEST-karma.xml',
useBrowserName: false
},
transports: ['polling'],
webpack: require('./webpack.config'),
webpackMiddleware: {
stats: 'errors-only'
},
logLevel: config.LOG_INFO,
});
};
Really appreciate for any suggestion on how to solve this issue, it is really annoying

Karamajs only running one file

I'm trying to test my client-side scripts using a gulp task in Karmajs with the browserify and mocha extensions, but only one test file is being run. Here is my gulp task
let Server = require('karma').Server;
gulp.task('client-test', function (done) {
new Server({
configFile: `${__dirname}/karma.conf.js`,
singleRun: true
}, done).start();
});
Here is my config file:
module.exports = function (config) {
config.set({
basePath: '../',
frameworks: ['mocha', 'browserify'],
files: [{ pattern: 'client/test/spec/**/*.js', included: true }],
preprocessors: {
"client/test/spec/**/*.js": ['browserify']
},
client: {
mocha: {
reporter: 'html',
ui: 'bdd'
}
},
browserify: {
debug: true,
transform: ['babelify', 'debowerify', 'brfs']
},
reporters: ['spec'],
port: 8081,
colors: true,
logLevel: config.LOG_INFO,
autowatch: true,
browsers: ['PhantomJS'],
phantomjsLauncher: {
exitOnResourceError: true
}
});
};
As you can see I'm using ** selectors to get js files in all subdirectories, but when I run the task I get the following output:
> node_modules/gulp/bin/gulp.js "client-test"
[12:24:26] Using gulpfile ~/workspace/gulpfile.js
[12:24:26] Starting 'client-test'...
[BABEL] Note: The code generator has deoptimised the styling of "/home/ubuntu/workspace/bower_components/angular-mocks/angular-mocks.js" as it exceeds the max of "100KB".
[BABEL] Note: The code generator has deoptimised the styling of "/home/ubuntu/workspace/bower_components/angular/angular.js" as it exceeds the max of "100KB".
[BABEL] Note: The code generator has deoptimised the styling of "/home/ubuntu/workspace/bower_components/rxjs/dist/rx.all.js" as it exceeds the max of "100KB".
08 06 2016 12:24:51.273:INFO [framework.browserify]: bundle built
08 06 2016 12:24:51.334:INFO [karma]: Karma v0.13.22 server started at http://0.0.0.0:8081/
08 06 2016 12:24:51.362:INFO [launcher]: Starting browser PhantomJS
08 06 2016 12:25:04.754:ERROR [phantomjs.launcher]: Fontconfig warning: ignoring C.UTF-8: not a valid language tag
08 06 2016 12:25:09.105:INFO [PhantomJS 2.1.1 (Linux 0.0.0)]: Connected on socket /#SqfLEWAUFbgjRpw6AAAA with id 21013769
AuthService
✓ Logs in a existing user
✓ Registers a new user
✓ Checks to see if a user is already authenticated
PhantomJS 2.1.1 (Linux 0.0.0): Executed 3 of 3 SUCCESS (0.152 secs / 0.041 secs)
TOTAL: 3 SUCCESS
Why is only one test file being run? What am I doing wrong?
It turns out that when there's an error in the second file, the tests won't run at all for the second file.

TypeError: undefined is not an object (evaluating 'scope.awesomeThings')

I got this error every time I run grunt test command. I setup a project using yo angular and try to run example code given in yeoman's scaffold. I don't what went wrong here, below are code which I tried to test.
controller/main.js
angular.module('brandPortalApp')
.controller('MainCtrl', function ($scope) {
$scope.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS',
'Karma'
];
});
test/controllers/main.js
'use strict';
describe('Controller: MainCtrl', function () {
// load the controller's module
beforeEach(module('brandPortalApp'));
var MainCtrl,
scope;
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
MainCtrl = $controller('MainCtrl', {
$scope: scope
});
}));
it('should attach a list of awesomeThings to the scope', function () {
expect(scope.awesomeThings.length).toBe(3);
});
});
karma.conf.js
// Karma configuration
// http://karma-runner.github.io/0.12/config/configuration-file.html
// Generated on 2016-05-27 using
// generator-karma 0.8.3
module.exports = function(config) {
'use strict';
config.set({
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// base path, that will be used to resolve files and exclude
basePath: '../',
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/angular-animate/angular-animate.js',
'bower_components/angular-aria/angular-aria.js',
'bower_components/angular-cookies/angular-cookies.js',
'bower_components/angular-messages/angular-messages.js',
'bower_components/angular-resource/angular-resource.js',
'bower_components/angular-route/angular-route.js',
'bower_components/angular-sanitize/angular-sanitize.js',
'bower_components/angular-touch/angular-touch.js',
'app/scripts/**/*.js',
'test/mock/**/*.js',
'test/spec/**/*.js'
],
// list of files / patterns to exclude
exclude: [],
// web server port
port: 8080,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: [
'PhantomJS'
],
// Which plugins to enable
plugins: [
'karma-phantomjs-launcher',
'karma-jasmine'
],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false,
colors: true,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// Uncomment the following lines if you are using grunt's server to run the tests
// proxies: {
// '/': 'http://localhost:9000/'
// },
// URL root prevent conflicts with the site root
// urlRoot: '_karma_'
});
};
In Terminal
Running "connect:test" (connect) task Started connect web server on
http://localhost:9001
Running "karma:unit" (karma) task WARN [watcher]: Pattern
"/Users/kiwitech/Brand-Portal/test/mock/**/*.js" does not match any
file. INFO [karma]: Karma v0.13.22 server started at
http://localhost:8080/ INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 2.1.1 (Mac OS X 0.0.0)]: Connected on socket
/#NDwIB4AQl7giaVxJAAAA with id 29519679 PhantomJS 2.1.1 (Mac OS X
0.0.0) Controller: MainCtrl should attach a list of awesomeThings to the scope FAILED
forEach#/Users/kiwitech/Brand-Portal/bower_components/angular/angular.js:322:24
loadModules#/Users/kiwitech/Brand-Portal/bower_components/angular/angular.js:4548:12
createInjector#/Users/kiwitech/Brand-Portal/bower_components/angular/angular.js:4470:30
workFn#/Users/kiwitech/Brand-Portal/bower_components/angular-mocks/angular-mocks.js:2464:60
/Users/kiwitech/Brand-Portal/bower_components/angular/angular.js:4588:53
TypeError: undefined is not an object (evaluating 'scope.todos') in /Users/kiwitech/Brand-Portal/test/spec/controllers/main.js (line
20)
/Users/kiwitech/Brand-Portal/test/spec/controllers/main.js:20:17 PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 1 of 1 (1 FAILED) ERROR
(0.005 secs / 0.015 secs) Warning: Task "karma:unit" failed. Use
--force to continue.
Aborted due to warnings.
I checked other question related to this but those solution not work for this.
Thanks for help!!
I'm not sure if you solved it, but in case you didn't, the issue is with paths in karma.conf.js. I had a similar issue with my angularApp.
As I'm not sure what your architecture is, I made test by myself (pretty much dirty, ie: not optimized), and of course paths should be changed according to the architecture you have.
jasmineApp folder:
├── app
│   └── scripts
├── bower_components
│   ├── angular
│   ├── angular-animate
│   ├── angular-aria
│   ├── angular-cookies
│   ├── angular-messages
│   ├── angular-mocks
│   ├── angular-resource
│   ├── angular-route
│   ├── angular-sanitize
│   └── angular-touch
└── test
└── controllers
testing file is test/controllers/main.js:
'use strict';
// run first test to check karma - I always run this simple test
// before starting with real tests
describe('Simple test', function(){
it("a is in fact 'hello world'", function(){
var a = "Hello world";
expect(a).toBe('Hello world');
});
});
describe('Controller: MainCtrl', function () {
// load the controller's module
beforeEach(module('brandPortalApp'));
var MainCtrl,
scope;
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
MainCtrl = $controller('MainCtrl', {
$scope: scope
});
}));
it('should attach a list of awesomeThings to the scope', function () {
expect(scope.awesomeThings.length).toBe(3);
});
});
controller file is app/controller/main.js:
angular.module('brandPortalApp',[])
console.log('loaded')
angular.module('brandPortalApp')
.controller('MainCtrl', function ($scope) {
$scope.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS',
'Karma'
];
});
karma.config.js is something like this:
// Karma configuration
// http://karma-runner.github.io/0.12/config/configuration-file.html
// Generated on 2016-05-27 using
// generator-karma 0.8.3
module.exports = function(config) {
'use strict';
config.set({
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// base path, that will be used to resolve files and exclude
basePath: '../',
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'jasmineApp/bower_components/angular/angular.js',
'jasmineApp/bower_components/angular-mocks/angular-mocks.js',
'jasmineApp/bower_components/angular-animate/angular-animate.js',
'jasmineApp/bower_components/angular-aria/angular-aria.js',
'jasmineApp/bower_components/angular-cookies/angular-cookies.js',
'jasmineApp/bower_components/angular-messages/angular-messages.js',
'jasmineApp/bower_components/angular-resource/angular-resource.js',
'jasmineApp/bower_components/angular-route/angular-route.js',
'jasmineApp/bower_components/angular-sanitize/angular-sanitize.js',
'jasmineApp/bower_components/angular-touch/angular-touch.js',
'jasmineApp/app/scripts/**/*.js',
//'test/mock/**/*.js',
'jasmineApp/test/controllers/main.js'
],
// list of files / patterns to exclude
exclude: [],
// web server port
port: 8080,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: [
'PhantomJS'
],
// Which plugins to enable
plugins: [
'karma-phantomjs-launcher',
'karma-jasmine'
],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false,
colors: true,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// Uncomment the following lines if you are using grunt's server to run the tests
// proxies: {
// '/': 'http://localhost:9000/'
// },
// URL root prevent conflicts with the site root
// urlRoot: '_karma_'
});
};
running test the output is OK:
21 07 2016 10:58:38.705:WARN [karma]: No captured browser, open http://localhost:8080/
21 07 2016 10:58:38.718:INFO [karma]: Karma v1.1.1 server started at http://localhost:8080/
21 07 2016 10:58:38.719:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
21 07 2016 10:58:38.755:INFO [launcher]: Starting browser PhantomJS
21 07 2016 10:58:39.017:INFO [PhantomJS 2.1.1 (Linux 0.0.0)]: Connected on socket /#BUObAGDOLqCbC0uRAAAA with id 56816897
PhantomJS 2.1.1 (Linux 0.0.0) LOG: 'loaded'
PhantomJS 2.1.1 (Linux 0.0.0): Executed 1 of 1 SUCCESS (0.006 secs / 0.009 secs)
Changing the path for main.js file in karma.config.js
from working one
'jasmineApp/app/scripts/**/*.js',
to this
'app/scripts/**/*.js', // wrong path
the error you got, is back:
21 07 2016 11:01:48.645:WARN [watcher]: Pattern "/var/www/NODEJS/app/scripts/**/*.js" does not match any file.
21 07 2016 11:01:48.664:WARN [karma]: No captured browser, open http://localhost:8080/
21 07 2016 11:01:48.674:INFO [karma]: Karma v1.1.1 server started at http://localhost:8080/
21 07 2016 11:01:48.674:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
21 07 2016 11:01:48.690:INFO [launcher]: Starting browser PhantomJS
21 07 2016 11:01:48.955:INFO [PhantomJS 2.1.1 (Linux 0.0.0)]: Connected on socket /#YK-fqsro3-ebMO2kAAAA with id 84208961
PhantomJS 2.1.1 (Linux 0.0.0) Controller: MainCtrl should attach a list of awesomeThings to the scope FAILED
forEach#jasmineApp/bower_components/angular/angular.js:321:24
loadModules#jasmineApp/bower_components/angular/angular.js:4592:12
createInjector#jasmineApp/bower_components/angular/angular.js:4514:30
workFn#jasmineApp/bower_components/angular-mocks/angular-mocks.js:3067:60
loaded#http://localhost:8080/context.js:151:17
jasmineApp/bower_components/angular/angular.js:4632:53
TypeError: undefined is not an object (evaluating 'scope.awesomeThings') in jasmineApp/test/controllers/main.js (line 20)
jasmineApp/test/controllers/main.js:20:19
loaded#http://localhost:8080/context.js:151:17
PhantomJS 2.1.1 (Linux 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.042 secs / 0.007 secs)
I hope will help.
I had the exact same issue and I solved it by fixing the path and the list of js files to be called in the karma.conf.js
files: [
'test/libs/jquery.min.js',
'test/libs/angular.js',
'test/libs/angular-mocks.js',
'test/libs/angular-animate.js',
'test/libs/angular-cookies.js',
'test/libs/angular-resource.js',
'test/libs/angular-route.js',
'test/libs/angular-touch.js',
'test/libs/angular-sanitize.js',
'app/scripts/app.js',
'app/scripts/**/*.js',
'test/spec/**/*.js'
],
Comparing my code with your I feel like maybe calling your app by a direct path (app/scripts/app.js in my case) could help.
Good luck

Grunt Karma runs tests but blocks

I have a grunt task to run Karma unit tests using Phantom JS. The tests run but the task doesn't exit. This blocks any other task from starting till I manually kill the karma:unit task using ctrl+c.
My karma.conf.js file is:
// Karma configuration
// Generated on Thu Mar 06 2014 13:17:21 GMT-0500 (Eastern Standard Time)
module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',
// frameworks to use
frameworks: ['mocha', 'requirejs', 'chai', 'sinon'],
// list of files / patterns to load in the browser
files: [
{ pattern: 'src/vendor/**/*.js', included: false },
{ pattern: 'src/*.js', included: false },
{ pattern: 'src/app/*_test.js', included: false },
{ pattern: 'src/app/**/*.js', included: false },
{ pattern: 'src/app/*_test.js', included: false },
'test-main.js'
],
// list of files to exclude
exclude: [
'src/app/main.js',
'**/Gruntfile.js'
],
// test results reporter to use
// possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
reporters: ['progress', 'coverage'],
preprocessors: {
'src/app/**/!(*_test).js': 'coverage'
},
// 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, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera (has to be installed with `npm install karma-opera-launcher`)
// - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
// - PhantomJS
// - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
browsers: ['PhantomJS'],
// If browser does not capture in given timeout [ms], kill it
captureTimeout: 10000,
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: true
});
};
My Grunt config for the karma task is:
karma: {
options: {
configFile: 'karma.conf.js'
},
unit: {
autowatch: true,
singleRun: true,
}
},
When I run grunt karma:unit, I get:
Running "karma:unit" (karma) task
INFO [karma]: Karma v0.12.0 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.7 (Windows 7)]: Connected on socket V2NFfUtyUi_gl0gWqbov with id 17494532
PhantomJS 1.9.7 (Windows 7): Executed 2 of 2 SUCCESS (0.012 secs / 0 secs)
but it never gets to Done, without errors until I press ctrl+C
I reinstalled karma and PhantomJS in the project and the problem cleared. I noticed that when I changed my karma configuration to use Chrome, everything worked normally so I figured the issue had to be with PhantomJS
npm install karma --save-dev
npm install phantomjs --save-dev

How to get better test reports in the console?

I have a fairly simple karma.config.js file
basePath = '../';
files = [
JASMINE,
JASMINE_ADAPTER,
'js/lib/angular*.js',
'test/lib/angular/angular-mocks.js',
'js/**/*.js',
'test/unit/**/*.js'
];
autoWatch = true;
browsers = ['PhantomJS'];
When I run karma start config/karma.conf.js --single-run I'm getting the following output
$ karma start config/karma.conf.js --single-run
[2013-06-24 23:47:08.750] [DEBUG] config - autoWatch set to false, because of singleRun
INFO [karma]: Karma server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9 (Mac)]: Connected on socket id LwMoWxzIbSUuBsvIqB_m
PhantomJS 1.9 (Mac): Executed 6 of 6 SUCCESS (0.073 secs / 0.02 secs)
I've been searching for something to tell me how to get the output of the tests that get logged (e.g. SUCCESS Unit: services myService should behave)
The only way I can see the output of the tests is to open Chrome and click 'Debug', then show the developer tools console. I want the messages to be logged out to the terminal, but I cannot figure out how to get this working.
Fixed by installing the karma-spec-reporter
npm install karma-spec-reporter --save-dev
and adding this my karma.config.js
reporters: ['spec'],
According to karma documentation
By default, Karma loads all NPM modules that are siblings to it and their name matches karma-*.
but some users have had to add the following to their config
plugins: ['karma-spec-reporter']
Just another detail - if you keep the default reporter 'progress' in the karma.config.js, like below:
reporters: ["progress", "spec"]
or another console reporter, the "spec" reporter output won't work.
You should keep only the "spec" one, or the "spec" with other browser reporters. For example:
reporters: ["spec", "coverage"]
I wrote a reporter to make the output more readable: karma-helpful-reporter
Has some nice customization options: https://github.com/whyboris/karma-helpful-reporter
Instal instructions inside, basically npm install --save-dev karma-helpful-reporter and then add to the Karma configuration plugins section:
plugins: [
require('karma-helpful-reporter')
],
reporters: [ 'helpful' ],
You can also use the Karma-mocha-reporter
as reporter and you should have a clean report in your console.
npm i karma-mocha-reporter --save-dev
// karma.conf.js
module.exports = function(config) {
config.set({
frameworks: ['jasmine'],
// reporters configuration
reporters: ['mocha']
});
};
Sometimes, e.g in #angular/cli environment you should require this like:
plugins: [
...
require('karma-mocha-reporter'),
...
]
Here is my working (draft) configuration without the section 'plugins' (actually I don't fully understand why I should need to specify them ...):
package.json
"devDependencies": {
[...]
"grunt-karma": "~0.9.0",
"karma": "~0.12.24",
"karma-jasmine": "~0.2.3",
"karma-chrome-launcher": "~0.1.5",
"karma-phantomjs-launcher": "~0.1.4",
"karma-spec-reporter": "0.0.13"
}
karma.conf.js
module.exports = function (config) {
config.set({
frameworks: ['jasmine'],
reporters: ['spec'],
browsers: ['PhantomJS']
});
};
Gruntfile.js
karma: {
options: {
configFile: 'karma.conf.js',
files: [
'app/libs/angular.js',
'app/libs/angular-resource.js',
'app/libs/angular-route.js',
[...]
'app/modules/**/*-spec.js'
]
},
unit: {
singleRun: true
}
}
Now when I run grunt karma messages from the *-spec.js files (describe('message', function() ...)) are displayed nicely in the console.

Categories

Resources