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
Related
I am trying to run my unit tests for Typescript class, but I am getting an error about missing Promise when connected to PhantomJS browser. Below I attach some configuration I am using. What I want to achieve is to write tests in Typescript and use ES6 features like imports and arrow functions.
karma.conf.js
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['systemjs', 'jasmine'],
plugins: [
'es6-module-loader',
'karma-systemjs',
'karma-jasmine',
"karma-spec-reporter",
"karma-phantomjs-launcher"
],
files: [
'app/test/**/*.spec.ts',
],
systemjs: {
configFile: './karma.system.conf.js',
config: {
baseURL: './'
},
serveFiles: [
]
},
exclude: [],
preprocessors: {},
reporters: ['spec'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['PhantomJS'],
singleRun: true,
concurrency: Infinity
})
}
karma.system.conf.js
System.config({
paths: {
'es6-module-loader': 'node_modules/es6-module-loader/dist/es6-module-loader.js',
'jasmine': 'node_modules/karma-jasmine/*',
systemjs: 'node_modules/systemjs/dist/system.js',
typescript: 'node_modules/typescript/lib/typescript.js',
'plugin-typescript': 'node_modules/plugin-typescript/lib/plugin.js'
},
meta: {
'*.ts': {
format: 'es6'
}
},
packages: {
'app/': { defaultExtension: 'ts' }
},
transpiler: 'typescript',
});
'karma start' output
PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
ReferenceError: Can't find variable: Promise
at node_modules/systemjs/dist/system.js:5
PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 0 of 0 ERROR (0.042 secs / 0 secs)
PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR LOG: 'Error: Not setup properly. window.Promise is undefined'
Do anyone has an idea what is wrong with that setup?
So, I think I have managed to solve my problem by myself at the end. Configuration which worked for me is:
karma.conf.js
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['systemjs', 'jasmine'],
plugins: [
'karma-systemjs',
'es6-module-loader',
'karma-jasmine',
"karma-spec-reporter",
"karma-phantomjs-launcher"
],
files: [
{pattern: 'app/**/*.ts', included: false, watched: true},
{pattern: 'node_modules/systemjs/dist/system-polyfills.js', included: true, watched: true},
'app/test/**/*.spec.ts',
],
systemjs: {
configFile: './karma.system.conf.js',
config: {
baseURL: ''
},
serveFiles: [
]
},
exclude: [],
preprocessors: {},
reporters: ['spec'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['PhantomJS'],
singleRun: true,
concurrency: Infinity
})
}
karma.system.conf.js
System.config({
paths: {
'systemjs': 'node_modules/systemjs/dist/system.js',
'system-polyfills': 'node_modules/systemjs/dist/system-polyfills.js',
'es6-module-loader': 'node_modules/es6-module-loader/dist/es6-module-loader.js',
'jasmine': 'node_modules/karma-jasmine/*',
typescript: 'node_modules/typescript/lib/typescript.js',
'plugin-typescript': 'node_modules/plugin-typescript/lib/plugin.js',
'phantomjs-polyfill': 'node_modules/phantomjs-polyfill/bind-polyfill.js'
},
map: {
'systemjs': 'node_modules/systemjs/dist/system.js',
'system-polyfills': 'node_modules/systemjs/dist/system-polyfills.js',
'es6-module-loader': 'node_modules/es6-module-loader/dist/es6-module-loader.js'
},
meta: {
'*.ts': {
format: 'es6'
}
},
packages: {
'app/': { defaultExtension: 'ts' }
},
transpiler: 'typescript',
});
Of course it was necessary to install used packages with npm install. What is essential, you have to use systemjs at version 0.19.24 for some reason. Leaving it here for anyone who will face the same problem in future.
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.
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.