Karma: bug: importScripts is not defined error inside web worker script

Created on 27 Jul 2015  路  10Comments  路  Source: karma-runner/karma

I have a web worker worker.js that references a file e.g:

importScripts('file.js');

However Karma does not seem to like it:

INFO [karma]: Karma v0.12.37 server started at http://localhost:9877/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 44.0.2403 (Linux 0.0.0)]: Connected on socket ZecIQTwPUPehOMzVhz-8 with id 85867718
Chrome 44.0.2403 (Linux 0.0.0) LOG: 'pre-main prep time: 2 ms'

Chrome 44.0.2403 (Linux 0.0.0) ERROR
    Uncaught ReferenceError: importScripts is not defined at /home/.../worker.js:1

importScripts is a standard API of the Worker global scope so it should not throw this error

support

Most helpful comment

This sounds like the script is not executed in a web worker, but rather in the general browser scope. Try opening the debug mode and look at what actually happens on execution. (This error is not coming from karma, it's just relaying the error that Chrome sends)

All 10 comments

This sounds like the script is not executed in a web worker, but rather in the general browser scope. Try opening the debug mode and look at what actually happens on execution. (This error is not coming from karma, it's just relaying the error that Chrome sends)

@Dignifiedquire I tried with --log-level debug but it doesn't provide any further information on this.

I did however find another user facing the same problem who solved this with a hack which I modified a little:

if('function' === typeof importScripts) {
    importScripts('file.js');
}

Because this works, it would indicate that when Karma and/or Jasmine is loading the web worker script file, the importScripts function is not yet available since I guess it's only available from within a web worker?

I meant opening the debug mode in the browser window:

  • Run karma start --single-run false
  • Click on the big DEBUG button in the Chrome window
  • Open up the developer console and set breakpoints and activate "Break on Error"
  • Refresh the tab to trigger a run
  • See what happens

How are you loading this file? You might have to set included: false to avoid it being executed in the wrong context. You can read more about file options here: http://karma-runner.github.io/0.13/config/files.html

Again, the additional debug did not show me anything extra, except that Chrome is throwing this error just on the worker file but all the rest of the files/tests are running fine.

I think the right approach is to just NOT include workers as part of the tests as they need to be executed by a script e.g. var worker = new Worker("worker.js"); in order for worker functions like importScripts to become available. In this case, I just added the following rule to karma.conf.js to ignore the worker file from being executed:

{pattern: 'js/worker.js', included: false},

Importantly, I put this rule above where I include all my js files with a * rule:

{pattern: 'js/worker.js', included: false},
'js/**/*.js',

A better approach for me would be to just keep workers and their related scripts in a separate folder which is not included in karma.conf.js at all.

Thoughts?

Good, what you've described confirms what I thought. Re best practice, to be on the safe side how about you name all your worker scripts like this myscript.worker.js, then you can do

{pattern: 'js/**/*.worker.js', included: false}

without having to maintain a different folder structure

That also sounds like a good approach ! Thanks @Dignifiedquire

Where do you include this pattern {pattern: 'js/**/*.worker.js', included: false}. I checked karma.conf.js but I couldn't find an option there to exclude files.

Thanks. My main issue is https://stackoverflow.com/questions/61762954/importscript-and-worker-html-js-are-failing-jasmine-karma-test-cases. Would it be possible for you to look at it please?

I don't see anything in the stackoverflow post related to karma issues.

The karma test is just a web page and if your test configuration includes bad code then the page will fail with errors. The TroubleShooting page gives the recommended approach.
http://karma-runner.github.io/5.0/intro/troubleshooting.html
Basically you start your tests but the server does not exit. Then you use the browser debugger to find issues.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danielsiwiec picture danielsiwiec  路  5Comments

donaldpipowitch picture donaldpipowitch  路  3Comments

IgorMinar picture IgorMinar  路  5Comments

schippie picture schippie  路  5Comments

macjohnny picture macjohnny  路  5Comments