GithubHelp home page GithubHelp logo

Comments (5)

armfazh avatar armfazh commented on July 16, 2024 3

Here it is a solution that worked for me, but I'm using jest.

Include node-fetch in your devDependencies

devDependencies{
+        "node-fetch": "^2.6.1",
}

Add these lines to a mocha setup Javascript file (not TS), or just before your tests.

+ import nodeFetch from 'node-fetch'
+ // Mocking fetch Web API using node-fetch
+ if (typeof fetch === 'undefined') {
+   global.fetch = nodeFetch
+   global.Request = nodeFetch.Request
+ }

you might be tempted to add @types/node-fetch, I tried without success.

from workers-types.

Stono avatar Stono commented on July 16, 2024

Hmm looks like this might be a ts-node problem rather than worker-types TypeStrong/ts-node#711, and I've created TypeStrong/ts-node#1134 to see if anyone has any ideas there.

from workers-types.

xtuc avatar xtuc commented on July 16, 2024

If you are running the worker in Node. The Request Object doesn't exists. You can polyfill it using https://github.com/node-fetch/node-fetch#class-request locally to fix that issue.

from workers-types.

threepointone avatar threepointone commented on July 16, 2024

The above fix seems about right. But this only patches one global, and you'll have to do the rest of the environment as well. A proper (supported) option is to use miniflare, which is a full simulation of the Workers environment https://miniflare.dev/. Closing this issue.

from workers-types.

waldemarennsaed avatar waldemarennsaed commented on July 16, 2024

Update 2022:

The above fix worked for me as well.

Please note: Using TS will throw TS errors for not-overlapping types.
Make sure to use a plain setup.js file.

from workers-types.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.