jsdom-worker icon indicating copy to clipboard operation
jsdom-worker copied to clipboard

Error when running with jest

Open AaronVasquez opened this issue 7 years ago • 5 comments

console.error node_modules/jsdom-worker/dist/jsdom-inline-worker.js:1
    Error: only absolute urls are supported
        at /Users/aaronvasquez/Oyster/webnode/directories/node_modules/node-fetch/index.js:54:10
        at new Promise (<anonymous>)
        at new Fetch (/Users/aaronvasquez/Oyster/webnode/directories/node_modules/node-fetch/index.js:49:9)
        at Fetch (/Users/aaronvasquez/Oyster/webnode/directories/node_modules/node-fetch/index.js:37:10)
        at Object.<anonymous>.global.fetch (/Users/aaronvasquez/Oyster/webnode/directories/node_modules/jsdom-worker/dist/jsdom-inline-worker.js:1:876)
        at new Worker (/Users/aaronvasquez/Oyster/webnode/directories/node_modules/jsdom-worker/dist/jsdom-inline-worker.js:1:1780)
        at /Users/aaronvasquez/Oyster/webnode/directories/src/utils/bg-worker/index.js:12:29
        at new Promise (<anonymous>)
        at performTask (/Users/aaronvasquez/Oyster/webnode/directories/src/utils/bg-worker/index.js:11:5)
        at Object.<anonymous> (/Users/aaronvasquez/Oyster/webnode/directories/src/utils/bg-worker/bg-worker.test.js:9:29)
        at resolve (/Users/aaronvasquez/Oyster/webnode/directories/node_modules/jest-jasmine2/build/queueRunner.js:38:12)
        at new Promise (<anonymous>)
        at mapper (/Users/aaronvasquez/Oyster/webnode/directories/node_modules/jest-jasmine2/build/queueRunner.js:31:21)
        at Promise.resolve.then.el (/Users/aaronvasquez/Oyster/webnode/directories/node_modules/p-map/index.js:46:16)
        at <anonymous>
        at process._tickCallback (internal/process/next_tick.js:188:7)

This is what I have in my test file:

import "jsdom-worker";

AaronVasquez avatar May 10 '18 23:05 AaronVasquez

What are you passing to new Worker()?

developit avatar May 18 '18 18:05 developit

I ran into the same issue. Looking here, https://github.com/bitinn/node-fetch/blob/1d4ab5a0de0df991f92f1f30efd5f4815bbcdb71/src/request.js#L161 I see it needs to have a protocol specified. The problem is that file:// isn't supported

Error: only http(s) protocols are supported
          at ...node_modules\jsdom-worker\node_modules\node-fetch\index.js:58:10

r1r11 avatar Sep 08 '18 04:09 r1r11

Ah - we'll need to have self.fetch in the worker context patch URLs it receives to rebase them.

developit avatar Sep 19 '18 13:09 developit

is there a workaround or this possible to fix?

neptunian avatar Dec 05 '18 17:12 neptunian

This is an old issue, but I have the same problem in my code (I opened a similar issue #14 ). In my tests tried to load a module where my Worker code lives but Node fetch seems to work only with absolute urls. I mocked fetch to load my module source and then I compiled it using babel/core. I also had to modify jsdom-worker source code to make it work. That wasn't a clean solution :( Since I started my project using Webpack worker-loader (https://webpack.js.org/loaders/worker-loader/) and using Jest as test framework, I created a little transformer to test workers inspired by jsdom-worker (https://github.com/astagi/workerloader-jest-transformer). I hope this may help someone! (note that workerloader-jest-transformer is highly experimental, any contribution and advice would be greatly appreciated)

astagi avatar Sep 08 '20 15:09 astagi