question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to run tests without manually capturing browser

See original GitHub issue

Expected 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

  1. Installing karma-cli globally and running it from there.
  2. Running it with command line flags (e.g. --no-auto-watch and --single-run)
  3. Adding/removing autoWatch and singleRun in my karma conf
  4. Setting process.env.CHROME_BIN in my karma conf
  5. Running karma in with logLevel DEBUG and checking for errors in the Chrome console
  6. 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:

  1. npm i karma karma-chrome-launcher karma-mocha karma-mocha-reporter puppeteer mocha
  2. 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']
  });
};
  1. Create a basic test.
// tests/foo.js
var assert = require('assert');

describe('test', function() {
  it('should pass', function() {

  });
});
  1. 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:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tandrewnicholscommented, Mar 23, 2020

@devoto13 Great catch, yes. That’s the problem.

0reactions
tandrewnicholscommented, Mar 23, 2020

@devoto13 Interesting. Maybe it’s a typo. Let me try it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Karma not running unit tests due to "No captured browser ...
I'm trying to set up Karma to run AngularJS unit tests using Jasmine, but I can't get ...
Read more >
Google Chrome: Unable to run automated tests
Solution · Stop the currently running test. · Close Google Chrome, if open, and terminate any running Chrome-related background processes. · Insert the...
Read more >
Headless Chrome times out without executing any tests #154
When I run the tests on my OSX machine, all the tests get executed with the test runner on the headless chrome. But...
Read more >
Run manual tests - Azure Test Plans | Microsoft Learn
Microsoft Test Runner opens and runs in a new browser. Start the app that you want to test. Screenshot shows Test Runner recording...
Read more >
A simple guide to capture browser logs for failed tests - Medium
I recently felt this is really helpful for developers to pinpoint the exact issue. So here is how to capture browser logs for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found