Unable to run tests without manually capturing browser
See original GitHub issueExpected behaviour
I know I’ve used karma in the past and run tests in the terminal where they just run, without me having to open a browser on a specific port, but in my current project, I cannot get this behavior to work.
Actual behaviour
First of all, I am attempting to do this with “ChromeHeadless” using puppeteer. If I use autoWatch: true
/noSingleRun, I get the message WARN [karma]: No captured browser, open http://localhost:9876/
. If I open that url, the tests run (but never before). That may be the correct behavior for that case (although it seems strange cause it’s not headless then, but whatever, I can live with that). If I use autoWatch: false
/singleRun, I would expect the tests to just run, but they don’t. I get INFO [karma-server]: Karma v4.4.1 server started at http://0.0.0.0:9876/
and then nothing happens until I load that url. Then the tests will run and karma will exit. I’ve been looking around for the solution to this for the last couple days, but I’m not finding anything. The goal here is that I need a way to run tests in travis. Obviously, there you can’t manually capture a browser by loading a url. If I’m missing something obvious, I apologize, but I’ve tried a variety of things - many of the fixes you can find if you google “karma not capturing browser” - such as
- Installing karma-cli globally and running it from there.
- Running it with command line flags (e.g.
--no-auto-watch
and--single-run
) - Adding/removing autoWatch and singleRun in my karma conf
- Setting
process.env.CHROME_BIN
in my karma conf - Running karma in with logLevel DEBUG and checking for errors in the Chrome console
- Deleting and reinstalling node_modules (the great javascript fixall)
It’s probably worth noting that in all cases, once the tests do run, they pass and there are no errors (compilation or otherwise).
Environment Details
- Karma version (output of
karma --version
): 4.4.1 - Node version: 11.15.0
- Relevant part of your
karma.config.js
file: not sure which parts are “relevant” so I’m posting it all. I can probably post some or all of the webpack config if that proves to be necessary. I suppose I should try running a simple test case that doesn’t use webpack just to rule that (but I haven’t done that at this point).
let webpack = require('./webpack.config.js');
delete webpack.entry;
webpack.resolve.alias.src = require('path').resolve(__dirname, 'src');
webpack.resolve.alias.helpers = require('path').resolve(__dirname, 'tests/helpers');
webpack.mode = 'development';
module.exports = (config) => {
config.set({
files: [
{ pattern: 'tests/!(helpers)/**/*.js' }
],
frameworks: ['mocha'],
preprocessors: {
'tests/!(helpers)/**/*.js': ['webpack']
},
webpack,
reporters: ['mocha', 'coverage'],
coverageReporter: {
dir: 'coverage/',
reporters: [
{ type: 'html' },
{ type: 'text-summary' }
]
},
client: {
mocha: {
color: true,
diff: true
}
},
plugins: [
require('karma-chrome-launcher'),
require('karma-coverage'),
require('karma-mocha'),
require('karma-mocha-reporter'),
require('karma-webpack')
],
browser: ['ChromeHeadless']
});
};
Steps to reproduce the behaviour
I don’t think I’ve done anything special or unusual, so I assume the reproduction steps would be something like:
npm i karma karma-chrome-launcher karma-mocha karma-mocha-reporter puppeteer mocha
- Create a minimal config:
module.exports = (config) => {
config.set({
autoWatch: false,
singleRun: true,
files: [
{ pattern: 'tests/**/*.js' }
],
frameworks: ['mocha'],
reporters: ['mocha'],
client: {
mocha: {
color: true,
diff: true
}
},
plugins: [
require('karma-chrome-launcher'),
require('karma-mocha'),
require('karma-mocha-reporter')
],
browser: ['ChromeHeadless']
});
};
- Create a basic test.
// tests/foo.js
var assert = require('assert');
describe('test', function() {
it('should pass', function() {
});
});
- And then run it. Probably helpful to create an npm script like
"test": "karma run"
so you don’t have to drill into node_modules/.bin
Edit: I tried this simple config and experienced this behavior, although I had to use karma start
instead of karma run
for some reason, otherwise I got “There is no server listening on port 9876.” I don’t get that in my actual repo.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (2 by maintainers)
Top GitHub Comments
@devoto13 Great catch, yes. That’s the problem.
@devoto13 Interesting. Maybe it’s a typo. Let me try it.