Karamajs only running one file - javascript

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.

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

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

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

Karma Tesing on Virtual Machine

I am trying to run karma tests as part of an automated TFS build. The build opens a browser window and fails to run headless on the build server. What is the best browser configuration for this scenario?
Thanks!
the package.json script is
"headlessTest":"karma start --browsers ChromeHeadless karma.conf.js"
// Karma configuration file, see link for more information
// https://karma-runner.github.io/0.13/config/configuration-file.html
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '#angular/cli'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('#angular/cli/plugins/karma'),
require('karma-phantomjs-launcher')
],
client:{
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
reports: [ 'html', 'lcovonly' ],
fixWebpackSourcePaths: true
},
angularCli: {
environment: 'dev'
},
reporters: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false
});
};
output in console window in TFS build
[33m01 08 2017 10:03:00.043:WARN [launcher]: [39mChromeHeadless have not captured in 60000 ms, killing.
[33m01 08 2017 10:03:02.087:WARN [launcher]: [39mChromeHeadless was not killed in 2000 ms, sending SIGKILL.
[33m01 08 2017 10:03:04.208:WARN [launcher]: [39mChromeHeadless was not``killed by SIGKILL in 2000 ms, continuing.
You can try lunching the chrome with "nosandbox".
Use following configuration in your karma project.conf.js:
browsers: ['ChromeNoSandbox'],
customLaunchers: {
ChromeNoSandbox: {
base: 'Chrome',
flags: ['--no-sandbox']
}
}
Reference this Github issue: https://github.com/karma-runner/karma-chrome-launcher/issues/73#issuecomment-236597429
UPDATE:
There is an issue for Karma : github.com/karma-runner/karma/issues/2652 , whatever you can try different versions combination for Karma and Chrome, someone mentioned that downgrading to karma 1.5 worked.
You can also try to increase the Timeout value, eg 600000, then try it again.

Karma returns error and executes 0 out of 0, even though I have one test using Jasime

This is my karma/karma.conf.js:
// Karma configuration
// Generated on Mon Jan 04 2016 16:17:18 GMT-0500 (EST)
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: ['jasmine'],
// list of files / patterns to load in the browser
files: [
],
// list of files to exclude
exclude: [
],
// 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: true,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}
and this is my karma/tests/test_post.js:
describe('Controller: MainCtrl', function() {
beforeEach(module('PostPageApp'));
var ctrl;
beforeEach(inject(function($controller) {
ctrl = $controller('MainCtrl');
}));
it('Show have an add and logout function', function() {
expect(ctrl.add).toBeDefined();
});
});
and this is ../post.js:
angular.module("PostPageApp", ["BaseApp"])
.controller("MainCtrl", ["$http", "$window", "BaseService", function($http, $window, BaseService) {
var self = this;
self.add = function() {
BaseService.add.post(self.post, function() {
self.cerrorMessages = BaseService.cerrorMessages;
});
};
self.logoutUser = function() {
BaseService.logout();
};
}]);
Now, when I do karma start, it returns this:
04 01 2016 16:48:10.137:INFO [karma]: Karma v0.13.17 server started at http://localhost:9876/
04 01 2016 16:48:10.144:INFO [launcher]: Starting browser Chrome
04 01 2016 16:48:13.138:INFO [Chromium 47.0.2526 (Ubuntu 0.0.0)]: Connected on socket ayhU7qR23sshUzi3AAAA with id 50222765
Chromium 47.0.2526 (Ubuntu 0.0.0): Executed 0 of 0 ERROR (0.013 secs / 0 secs)
Any idea why it is executing 0 out of 0 and returning an error? I thought it would run
it('Show have an add and logout function', function() {
expect(ctrl.add).toBeDefined();
});
Please note that I am new to using Karma and Jasmine so I am still trying to get the hang of all of this.
Thanks in advance.
It's not running any tests because you aren't telling it to load any files into the browser. I think a lot of people use RequireJS for this, but unfortunately I am not familiar with it.
In karma.conf.js under the files: section:
List your JS dependencies (like JQuery or Angular).
List the files that you are testing next.
List the test specs themselves last.
For instance:
files: [
'angular.js',
'app.js',
'app.spec.js'
]
If you don't want certain files to be included then put them in the exclude section. Make sure the file paths are relative to where karma.conf.js is located.
I would suggest specifying a default path and a pattern at ./karma.conf.js:
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: 'test/',
// list of files / patterns to load in the browser
files: [
{pattern: '**/*.js*', included: true}
],
It worked for me.

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

Categories

Resources