Karma --auto-watch no longer works
Asked Answered
S

3

20

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.

Stealthy answered 15/7, 2015 at 11:17 Comment(3)
Did you install karma on the new repository? This is recommended since new version of karmaBesprent
@Besprent I installed Karma in the new workspace folder, yes.Stealthy
Make a copy of your karma.conf, delete it and do a karma init. Check differences between files. If none, test karma with a little project and 1 test to see if it works...Besprent
H
25

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.

Horseplay answered 17/7, 2015 at 13:25 Comment(19)
Okay, here's the pastebin output: pastebin.com/xRxEcjJ0 . I've changed from Chrome to Firefox, no difference. We're not using a task runner. Running the console as administrator doesn't make a difference. Doing a blank slate configuration makes no difference. Chrome is v43.0.2357.132 m, NodeJS is v0.12.7, Karma is v0.13.0, Jasmine is v2.3.4. Thanks for your help.Stealthy
Alrighty - thanks for that! Would it now be possible for you to run the same command again, and while the test run is active, go in and change a single file to see if we get any output in the log.txt file?Horseplay
I assumed you wanted me to do that anyway :) The output in the pastebin? I made two saves to tests while that log was running.Stealthy
I see! Hm. I'm thinking (going out on a limb here) whether the cwd for your karma runner somehow gets changed during the execution of the spec, in such a way that it cannot read from a '../' path. Would you mind trying to execute your tests with a karma configuration looking like my latest edit?Horseplay
I've just done exactly that, copied and pasted those two files. They're in the same directory. I fixed the Jasmine syntax, but other than that, they're the same as you wrote. Same behaviour - when Karma starts up it runs the rests once, then ignores changes to the test file. Killing Karma and restarting it makes it pick up the changes.Stealthy
Here's a pastebin of the debug log from the last experiment: pastebin.com/BtRHskrSStealthy
Did you get a chance to investigate my last edit? It's sort of a long shot but right now, I believe anything goes.Horseplay
Having double checked, my karma.conf.js didn't have that glob pattern in it. I have amended my question to reflect this.Stealthy
I see. Hm. I'm running low on suggestions now, but if you could give my last edit a go that'd be great.Horseplay
Done - I've updated my question to reflect the current state of my karma.conf. Thank you for all your help.Stealthy
@Stealthy I've added a new edit, this is where I suggest you bring out the big guns and nuke the current setup, reinstall it from scratch and give it another go.Horseplay
Ace, I'll give that a go. Don't worry, I speak UNIX - I do most of my work on Windows in git-bash. :) (yes, I have tested Karma from a standard Windows shell to rule that out :) )Stealthy
Alright sweet! :) I scoured through the karma issues related to windows, but I'm not quite sure what to make of it. There is one issue (albeit not a descriptive one) that states what you are experiencing.Horseplay
I've done everything you suggested, and no difference. :( Here's the pastebin of the log file for completeness. pastebin.com/pRmtjiVg Good spot on the bug report - I'll get on to GitHub and add all of these details there, hopefully that'll get the ball rolling. Thank you for your help. If no-one posts an answer involving a magic --start-working-again flag, then I'll grant you the bounty.Stealthy
Come to think of it @Stealthy - you mentioned you are running Karma > v0.13.0. I am running on 0.12.36, and when switching to 0.13.0 - a lot of references to !doNotCache were added to /karma/lib/middleware/common.js. Would you mind attempting to back down a couple of versions (namely to 0.12.36 - as I am 100% positive that --auto-watch is working on my system (OSX, unfortunately)).Horseplay
With the above said, I am not entirely aware of the implications of those changes are, but it could be worth giving a go.Horseplay
Let us continue this discussion in chat.Horseplay
Using sudo npm test solved my problem, I am using ubuntu 16.Gussy
Just use Web Test Runner. No issues. modern-web.dev/docs/test-runner/overviewCarinthia
S
5

For the record, I eventually fixed it by:

  • using a new folder for my local node_modules
  • using the git bash shell
  • making sure the PYTHON environment variable was set using git bash style separators
  • installing Karma version v0.12.0

None of it worked on its own, for me anyway. Hope this helps someone else.

Stealthy answered 20/7, 2015 at 13:49 Comment(1)
An alternative is to use Web Test Runner. Works really well! I have too many issues like above with Karma. modern-web.dev/docs/test-runner/overviewCarinthia
P
0

I also had the same problem. In my IDE it always worked and suddenly stopped.

I found someone who solved the problem. Solution for WebStorm

What happens is that the WebStorm configuration is overwriting some fields of the project's karma.conf.js file. To change the autoWatch to true will have no effect.

You must find the file intellij.conf.js in folder:

C:\Program Files\JetBrains\WebStorm 232.6095.12\plugins\karma\js_reporter\karma-intellij\lib

Then change:

config.singleRun = false;
var originalAutoWatch = config.autoWatch;
//config.autoWatch = false; <-- this line
config.autoWatchBatchDelay = 0;

I use version: WebStorm 232.6095.12

Portative answered 14/6, 2023 at 18:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.