In grunt we execute unit test in PhantomJs by following ways.
karma: {
options: {
configFile: 'karma.conf.js',
runnerPort: 9876,
browsers: ['Chrome']
},
unitTestDev: {
browsers: ['PhantomJS'],
singleRun: true
}
}
grunt.registerTask('unitTest',['karma:unitTestDev']);
Here is karma.conf.js file.
// Karma configuration
// Generated on Thu Apr 03 2014 13:44:39 GMT-0500 (Central Daylight Time)
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: [
{pattern: 'dist/lib/jquery/dist/jquery.js', watched: false, served: true, included: true},
{pattern: 'dist/lib/jasmine-jquery/lib/jasmine-jquery.js', watched: false, served: true, included: true},
{pattern: 'dist/lib/angular/angular.js', watched: false, served: true, included: true},
{pattern: 'dist/lib/angular-route/angular-route.min.js', watched: false, served: true, included: true},
{pattern: 'dist/lib/angular-mocks/angular-mocks.js', watched: false, served: true, included: true},
{pattern: 'dist/main.min.js', watched: false, served: true, included: true},
{pattern: 'public/test/unit/**/*Spec.js', watched: false, served: true, included: true},
// fixtures
{pattern: 'public/test/mock/**/*.json', watched: true, served: true, included: false}
],
// 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', 'html', 'junit', 'story'],
htmlReporter: {
outputDir: 'build/unit-test/html-report',
templatePath: 'node_modules/karma-html-reporter/jasmine_template.html'
},
junitReporter: {
outputFile: 'build/unit-test/junit-report/test-results.xml',
suite: ''
},
// 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: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
});
};
When execute "karma:unitTestDev" command, then getting following errors.
Running "karma:unitTestDev" (karma) task
INFO [karma]: Karma v0.11.14 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
ERROR [launcher]: Cannot start PhantomJS
Error: spawn OK
INFO [launcher]: Trying to start PhantomJS again (1/2).
ERROR [launcher]: Cannot start PhantomJS
Error: spawn OK
INFO [launcher]: Trying to start PhantomJS again (2/2).
ERROR [launcher]: Cannot start PhantomJS
Error: spawn OK
ERROR [launcher]: PhantomJS failed 2 times (cannot start). Giving up.
Warning: Task "karma:unitTestDev" failed. Use --force to continue.
Aborted due to warnings.
We are getting above issue in Windows and Linux OS. In Mac, karma successfully execute tests on PhantomJS.
In windows, If we execute above test in Chrome browser, instead of PhantomJS, then karma successfully execute all unit tests.
Here are karma related dependencies in Package.json
"phantomjs": "~1.9.7-3",
"karma": "~0.12.3",
"karma-chrome-launcher": "~0.1.3",
"karma-jasmine": "~0.1.5",
"karma-html-reporter": "~0.2.3",
"karma-junit-reporter": "~0.2.1",
"grunt-karma": "~0.8.2",
"karma-phantomjs-launcher": "~0.1.3"
Let me know if are there any known issue in karma or grunt with PhantomJS.
There is an issue with "Karma-phantomjs-launcher" v0.1.3. Change it to v0.1.4 and then it work fine.
Related
I have a problem with setting up units tests in project. When I run tests with IE everything works fine I can see 4/4 tests executed. With chrome i am getting error Empty test suite. Chrome is launched but it looks like it can;t find tests and tests for chrome are not executed.
Karma.config.js
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', 'angular-cli'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-ie-launcher'),
require('karma-remap-istanbul'),
require('angular-cli/plugins/karma')
],
files: [
{ pattern: './src/test.ts', watched: false }
],
preprocessors: {
'./src/test.ts': ['angular-cli']
},
remapIstanbulReporter: {
reports: {
html: 'coverage',
lcovonly: './coverage/coverage.lcov'
}
},
angularCli: {
config: './angular-cli.json',
environment: 'dev'
},
reporters: ['progress', 'karma-remap-istanbul'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: true
});
};
test.ts is just searching unittests within app folder:
.then(() => require.context('./', true, /\.spec\.ts/))
I found that the issue was with reporters section, after 2 days of agony I changed file to
reporters: config.angularCli && config.angularCli.codeCoverage
? ['progress', 'karma-remap-istanbul']
: ['progress'],
removed mime section and it works!
i am trying to use loadFixtures() on my test [jasmine] with karma but it's not working. i am using ' test/fixtures ' as my fixtures dir
i am trying to get 'html' files to load as fixture
i am using typescript
my jasmine unit
beforeEach(()=>{
jasmine.getFixtures().fixturesPath = 'fixtures';
loadFixtures('test.html');
})
and the packages are
"devDependencies": {
"gulp": "^3.9.1",
"gulp-typescript": "^2.13.6",
"jasmine": "^2.4.1",
"jasmine-jquery": "^2.1.1",
"karma": "^1.2.0",
"karma-chrome-launcher": "^2.0.0",
"karma-fixture": "^0.2.6",
"karma-html2js-preprocessor": "^1.0.0",
"karma-jasmine": "^1.0.2",
"karma-jasmine-jquery": "^0.1.1",
"merge2": "^1.0.2"
}
now come to karma config file
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-jquery',
'jasmine',
'fixture'
],
// list of files / patterns to load in the browser
files: [
'bower_components/jquery/dist/jquery.js',
'./bin/test/**/*.js',
'./dest/**/*.js',
{
pattern: './test/fixtures/*.html',
watched: true,
served: true,
included: false
}
],
// 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: false,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}
I am currently learning how to unit test in Ionic 2 using Josh Monrony's tutorial.
I am on Step 4: Create and Run a Unit Test.
When I run npm test, Chrome opens, but it never stops loading.
Then the console reports a timeout:
What is causing the timeout?
Here is my karma.conf.js file:
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', 'browserify'],
// list of files / patterns to load in the browser
files: [
'node_modules/es6-shim/es6-shim.js', // TypeError: undefined is not a constructor (evaluating 'new exports.Map()')
'node_modules/reflect-metadata/Reflect.js', // 'Uncaught reflect-metadata shim is required when using class decorators'
'node_modules/zone.js/dist/zone.js', // Zone.js dependencies (Zone undefined)
'node_modules/zone.js/dist/jasmine-patch.js',
'node_modules/zone.js/dist/async-test.js',
'node_modules/zone.js/dist/fake-async-test.js',
'app/**/*.spec.ts',
{pattern: 'node_modules/reflect-metadata/Reflect.js.map', included: false, served: true}, // 404 on the same
{pattern: 'www/build/**/*.html', included: false},
],
// list of files to exclude
exclude: [
'node_modules/angular2/**/*_spec.js',
'node_modules/ionic-angular/**/*spec*'
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'**/*.ts': ['browserify']
},
browserify: {
debug: true,
transform: [
['browserify-istanbul', {
instrumenter: require('isparta'),
ignore: ['**/*.spec.ts','**/*.d.ts'],
}]
],
plugin: [
['tsify']
]
},
// 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,
proxies: {
'/build': '/base/www/build'
},
// 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 are similar questions, but they either don't have answers or the answers don't work:
Karma error - Chrome have not captured in 60000 ms, killing
Karma test runner - fails to capture chrome
https://github.com/karma-runner/karma/issues/1206
I had the same problem, and solved it deleting the npm directory and recreating it with npm update. That worked for me.
I wonder if I am missing something trivial here, but I can not see any test reports if I have set up singlerun to true in karma config. It only shows that the browsers were launched and that is it. I can click on DEBUG and inspect the browser console log that way, but I would feel that one should be also see the results in the terminal too.
Thanks for the help!
My karma.config.js:
basePath: '../',
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'],
frameworks: ['mocha', 'chai'],
files: [
{ pattern: 'test/vendor/indexeddbshim.min.js', watched: false },
{ pattern: 'tests.webpack.js', watched: false },
],
preprocessors: {
'tests.webpack.js': ['webpack'],
},
webpack: {
resolve: {
root: [
path.resolve('./test/vendor'),
],
alias: {
backbone: 'backbone',
underscore: 'underscore',
},
},
module: {
loaders: [
{
// test: /^\.js$/,
exclude: /(node_modules|bower_components|vendor)/,
loader: 'babel-loader',
},
],
},
},
webpackServer: {
noInfo: true,
},
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// 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,
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
plugins: [
require('karma-webpack'),
require('karma-mocha'),
require('karma-chai'),
require('karma-phantomjs-launcher'),
require('karma-chrome-launcher'),
],
logLevel: config.LOG_INFO, });
From the comment above:
Setting singleRun: false assumes that you are explicitly start the karma-client manually.
This means that you start karma (technically the karma-server), then go to another terminal and type karma run.
Setting singleRun: true in your karma configuration will call karma run for you.
Here's the doc:
Karma configuration - requirejs version
How do I get Karama to play with backbone? All the docs I see you don't have to shim anything anymore, but I get errors whether I do or not. Karma is hosting my files and finding all the deps. I'm testing a Backbone app, not an Angular app. It seems that loading global _ is the main problem currently.
I've tried this shim in RequireJSConfig.js (loaded from Karma config) and I've also tried it without.
...
shim: {
'underscore':{
exports:'_'
}
...
I've confirmed the actual files are resolved by running Karma in debug mode:
DEBUG [watcher]: Resolved files:
C:/Users/vspazzy/AppData/Roaming/npm/node_modules/karma-jasmine/lib/jasmine.js
C:/Users/vspazzy/AppData/Roaming/npm/node_modules/karma-jasmine/lib/adapter.js
C:/workspace/das-web/src/test/RequireJSConfig.js
C:/workspace/das-web/src/main/resources/js/lib-clean/backbone-1.1.2.min.js
C:/workspace/das-web/src/main/resources/js/lib-clean/jquery-2.1.0.js
C:/workspace/das-web/src/main/resources/js/lib-clean/require-2.1.1.full.js
C:/workspace/das-web/src/main/resources/js/lib-clean/underscore-1.5.2.min.js
...
This is my Karma.conf.js:
module.exports = function(config) {
config.set({
basePath: 'src',
frameworks: ['jasmine'],
reporters: ["coverage"],
preprocessors: {
"**/*.js": "coverage"
},
files: [
'test/KarmaUnitConfig.js',
// require, backbone, jquery and underscore
{pattern: 'main/resources/js/lib-clean/*.js', watched: true, included: true, served: true},
// unit test helper
{pattern: 'test/lib/testr.js', watched: true, included: true, served: true},
//the unit tests
{pattern: 'test/js/unit/**/*.js', watched: true, included: true, served: true},
{pattern: 'test/js/unit/*.js', watched: true, included: true, served: true},
{pattern: 'test/js/helpers/*.js', watched: true, included: true, served: true},
// the app
{pattern: 'main/**/*.js', watched: true, included: true, served: true},
// the stubs
{pattern: 'test/js/stubs/**/*.js', watched: true, included: true, served: true}
],
// list of files to exclude
exclude: [
//crappy cluttered lib folder
'main/resources/js/lib/*.js',
// avoid minified (it is a package of whole application)
'main/app.min.js',
//ignore the old test html
'main/test/integration.html',
'main/test/integration.min.html',
'main/test/unit.html',
'main/test/unit.min.html',
'main/test/unit-headless.html'
],
reporters: ['progress'],
// web server port
port: 9876,
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_DEBUG,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
browsers: ['Chrome'],
singleRun: false,
/*
assuming these are deprecated because these are bundled now
plugins: [
'karma-requirejs',
'karma-jasmine',
'karma-chrome-launcher',
'karma-firefox-launcher'
]*/
});
};
So, at this point Karma throws this error while running tests:
Uncaught TypeError: Cannot call method 'each' of undefined
at http://localhost:9876/base/main/resources/js/lib-clean/backbone-1.1.2.min.js?69870e4d6c4da615282785f4b175f2dd9920d66a:219
--> on this line in backbone
_.each(listenMethods, function(implementation, method) {