简体   繁体   中英

Karma testing: Error loading JS files behind corporate proxy

I am using the Open WC Karma-ESM plugin and attempting to run my tests. I've added the necessary scripts to my package.json :

    "test": "npx karma start --coverage",
    "test:gate": "npx karma start",
    "test:watch": "npx karma start --coverage --auto-watch=true --single- 
    run=false",

But when I npm run test I'm getting this error in my terminal from Headless Chrome:

SyntaxError: Unexpected token '<'Chrome 79.0.3945 (Mac OS X 10.14.6) ERROR
Uncaught SyntaxError: Unexpected token '<'

In the browser I can see that it successfully loads the initial files for Karma to run its tests:

http://localhost:9876/
http://localhost:9876/socket.io/socket.io.js
http://localhost:9876/karma.js
http://localhost:9876/socket.io/?EIO=3&transport=polling&t=My6P1Ln
http://localhost:9876/socket.io/?EIO=3&transport=polling&t=My6P1M3&sid=Cc2mqcjmMzmq2IN1AAAB
http://localhost:9876/socket.io/?EIO=3&transport=polling&t=My6P1M4&sid=Cc2mqcjmMzmq2IN1AAAB

And then it hits this one which qets a response of 101 Switching Protocols . And the response body has all the Unexpected token < errors with a Page Restricted error:

ws://localhost:9876/socket.io/?EIO=3&transport=websocket&sid=Cc2mqcjmMzmq2IN1AAAB

So, the pages are restricted and rejected by my corporate proxy and they return a 404 so that explains the error. I've updated my Chrome proxy preferences to bypass proxy settings for these hosts and domains : localhost, 127.0.0.1

But that didn't solve the issue.

Just to throw a curveball, when I run sudo npm run test it works just fine. Now that just completely bewilders me.

Any tips are greatly appreciated - thanks in advance. Struggling with this one.

When you have http_proxy and https_proxy defined as environment variables then this error will occur. The fix is to add a new environment variable NO_PROXY=localhost,127.0.0.1. This bug killed me for days. I hope it helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM