Karma Chrome tests failing in Chrome, but passing in PhantomJS

947 views Asked by At

I'm trying to run tests in Chrome browser using Karma. I've been running them in PhantomJS without any issue, but was hoping to switch to ChromeHeadless. I discovered that tests aren't passing in Chrome or ChromeHeadless, but still pass using PhantomJS.

I believe it has something to do with the files being included in the config. I think PhantomJS is loading them as expected, but for some reason, Chrome is not. I've looked into the other questions here, but the solutions weren't particularly relevant and unfortunately did not work.

This is the error I get when trying to run my tests using Chrome:

Uncaught TypeError: Cannot read property 'maps' of undefined

Which is referring to google.maps, derived from window.google, which should be getting declared in the files in the karma.conf.js:

module.exports = function(config) {
  config.set({
    // autoWatch: true,
    browserConsoleLogOptions: {
      terminal: true,
      level: ''
    },
    // Prevent timeout issues when running the tests
    browserDisconnectTimeout: 10000,
    browserDisconnectTolerance: 3,
    browserNoActivityTimeout: 60000,
    browsers: ['Chrome'],
    captureTimeout: 2000,
    // debug: true,
    files: [
    'http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js',
      'http://maps.googleapis.com/maps/api/js?client=client_id',
      { pattern: 'test-context.js', watched: false }
    ],
    frameworks: ['jasmine'],
    // logLevel: config.LOG_DEBUG,
    port: 9876,
    preprocessors: {
      'test-context.js': ['webpack'],
    },
    reporters: ['progress', 'spec'],
    singleRun: true,
    webpack: require('./webpack/config.test'),
    webpackServer: {
      noInfo: true
    },
  });
};

Does anyone know what I may be doing wrong, or why running Chrome browser for testing doesn't seem to be pulling in the external files?

I could potentially mock the entire google maps object, but that seems excessive and unnecessary since it should be getting included in the specified files.

If anyone has any ideas, I'd be most grateful for your help. Thank you!

1

There are 1 answers

3
xveganxxxedgex On BEST ANSWER

I finally figured it out. It ended up being a CORS issue that was preventing the external files from loading. Adding the following code to my karma.conf file resolved the issue for me:

crossOriginAttribute: false,