Karma --auto-watch no longer works

TarkaDaal picture TarkaDaal · Jul 15, 2015 · Viewed 14.2k times · Source

My Karma installation used to auto-watch - when I saved a .js file, it'd re-run the tests. It's been a couple of months since I did any JavaScript, and now I come to use it again, the auto-watch feature isn't working. Here is my karma.conf:

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: [
          'jQuery/jquery-1.10.2.js',
          'jasmine/jasmine.js',
          'jasmine-jquery/jasmine-jquery.js',
          'Angular/angular.js',
          'Angular/angular-route.js',
          'Angular/angular-mocks.js',
          'Angular/angular-animate.min.js',
          'Angular/angular-sanitize.min.js',
          'Angular/angular-cache.min.js',
          'emcommon.js',
          'Moment/moment.js',
          'ViewModels/Common/*.js',
          'ViewModels/Settings/*.js',
          'Tests/Common/*.js',
          'Tests/Settings/*.js',
        ],

        // 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: {
            '../ViewModels/**/*.js': 'coverage'
        },


        // test results reporter to use
        // possible values: 'dots', 'progress'
        // available reporters: https://npmjs.org/browse/keyword/karma-reporter
        reporters: ['progress', 'coverage'],

        coverageReporter: {
            type: 'html',
        },

        // 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,
        usePolling: 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
    });
};

I've read and followed the advice here. I've tried setting usePolling to true. I've used three different programs to save the file (VS, Sublime, and Notepad) to rule that out. If I stop Karma and restart it, it picks up the changes and they pass/fail as expected, but it will not see the files change while it's running.

Moving from Karma 0.12.7 to 0.13.0 makes no difference to the problem.

Answer

Kasper Lewau picture Kasper Lewau · Jul 17, 2015

Where's the output?

First of all, I think it would be of some help to see the output of a run with the following CLI options:

# add --single-run, or kill the process manually when finished.
karma start karma.conf.js --auto-watch --log-level debug > log.txt

Then paste the contents of log.txt to a pastebin. It'd probably be too big to put as an edit to your question.


A different browser, mayhaps?

I would also try to run a different browser than Chrome, I wouldn't rule out that it may be the cause of issue as every example in karma-runner#895 shows Chrome as the browser of choice (albeit this is an old issue and resolved by the looks of it).

Give it a try with PhantomJS and/or Chrome Canary, and see if that does the trick.


Go with a task-runner?

Do you by any chance have a task runner as part of your local setup? If so, you could kill the --auto-watch and use an equivalent solution in the task runner of your choice.

The way we've set up our grunt task is as follows:

karma: {
  options: {
    configFile: 'karma.conf.js'
  },
  watch: {
    background: false,
    singleRun: false
  }
}

With the following karma.conf settings:

module.exports = function(config) {
  config.set({
    frameworks: ['mocha'],
    basePath: '',
    files: [ /** redacted **/ ],
    urlRoot: '/base/app/',
    reporters: ['progress'],
    logLevel: config.LOG_INFO,
    browsers: ['PhantomJS'],
    singleRun: true
  });
};

Try running with sudo?


Blank slate configuration

Run karma init newconf.js, then give this one a whirl:

karma start newconf.js --auto-watch

I think seeing some output from your tests would be very helpful here. And preferably some version numbers of:

  • Chrome
  • NodeJS
  • Karma
  • Test Framework (Mocha/Chai, Jasmine)

Edit#1

Please have a go with a karma.conf looking like the following:

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine'],
    files: [
      'some_jasmine_spec.js',
    ],
    reporters: ['progress'],
    port: 9000,
    colors: true,
    logLevel: config.LOG_DEBUG,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

Where the some_jasmine_spec.js file would look like so:

describe('dummy_test', function() {
  it('just runs', function() {
    expect(true).to.be.false;
  });
});

Put the some_jasmine_spec.js file in the same folder as this stripped down karma.conf and see if it yields a different result.


Edit#2

I just noticed that the last line in your files array isn't being read. If you look at the output of your first log file:

# The last DEBUG [watcher] entry
# 27 - [36m17 07 2015 14:35:37.160:DEBUG [watcher]: [39mWatching "c:/Projects/Gazelle - EstateManager/DEV-Container/(9112) ViewDevice/EstateManagerUI/EstateManagerUI/Scripts/Tests/Settings"

# The last DEBUG [web-server] entry
# 102 - [36m17 07 2015 14:35:38.321:DEBUG [web-server]: [39mserving (cached): c:/Projects/Gazelle - EstateManager/DEV-Container/(9112) ViewDevice/EstateManagerUI/EstateManagerUI/Scripts/Tests/Settings/viewschedulemodule.tests.js

So, none of the files in the ./*/*.js pattern are being read.

I would try to change it to ./**/*.js, or even **/*.js.

Even so, the dummy jasmine test should've done the trick had it been related to files not being included.

Edit #3

Running low on ideas here but;

I would try changing the basePath to ../ and removing ../ from all other file references. This is more so a scratching of my (itchy) curiosity than a 'valid' concern. But hey, trying doesn't hurt.


Edit #4

Last ditch effort; Pop open a terminal and run the following (sorry, I'm on a UNIX based system - my MS-DOS isn't up to par, so to speak...). Let's clean it all out, kill the cache, reinstall the packages you require and give it another go.

Cleanup/out

  • $ rm -rf node_modules # clean out your local node modules
  • $ npm cache clean # clean out the npm cache
  • $ npm uninstall karma karma-cli karma-chrome-launcher karma-coverage karma-firefox-launcher karma-ie-launcher karma-jasmine jasmine -g # uninstall assorted karma/jasmine libraries globally

Backup

  • $ mv karma.conf.js karma.conf.bak.js # backup your karma.conf

Reinstallation

  • $ npm install karma karma-cli karma-chrome-launcher karma-coverage karma-firefox-launcher karma-ie-launcher karma-jasmine jasmine -g # reinstall assorted karma/jasmine libraries globally
  • $ npm install # reinstall your local node modules

Reinitialisation

  • $ karma init # initialise a new karma.conf.js file
    • make as few modifications as possible to the new karma.conf.js file
  • $ karma start karma.conf.js --auto-watch --log-level debug
    • optionally, pipe the above out into a new log.txt and upload it.
    • this is so that we can compare the two and see if anything really stands out.

Debugging

Ensure that singleRun is set to false in your new karma.conf.js file, then pop open the connected browser and go to http://localhost:9876.

Press the debug button, open up your dev tools (web inspector/console), and investigate what happens when you reload the page.

If this doesn't make any difference, I'm at a loss.