-
Notifications
You must be signed in to change notification settings - Fork 89
ReferenceError: Request is not defined #65
Comments
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. |
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. |
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 |
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. |
Update 2022:The above fix worked for me as well. Please note: Using TS will throw TS errors for not-overlapping types. |
Having the strangest behaviour that I can't get to the bottom of since trying v2.0.0 of this module.
Given this really simple module,
worker.ts
:And this really simple mocha test:
worker.test.ts
:And the following
tsconfig
:In the GUI (vscode) all intellisense is working fine, and typescript compiles fine too.
However, when running the tests via Mocha, I get:
So specifically on the line:
Therefore it seems fine with the interface use in the function parameters:
Any ideas?
The text was updated successfully, but these errors were encountered: