Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There is some problem with testing code #43

Open
Iezuni opened this issue Jun 15, 2021 · 3 comments
Open

There is some problem with testing code #43

Iezuni opened this issue Jun 15, 2021 · 3 comments

Comments

@Iezuni
Copy link

Iezuni commented Jun 15, 2021

When I use fetch('someUrl') in my code, the jest will report a bug like ReferenceError: fetch is not defined. How can I solve this problem?

@koeninger
Copy link
Contributor

Local tests run in node. Node does not implement the fetch api.

There are a number of packages for mocking the fetch api, jest-fetch-mock has typescipt definitions https://www.npmjs.com/package/jest-fetch-mock#to-setup-for-an-individual-test

Here's an example basic usage
https://github.com/cloudflare/worker-typescript-template/tree/example-jest-fetch-mock

@nelsonjchen
Copy link

nelsonjchen commented Mar 6, 2022

Miniflare is a good replacement for the existing mock. It actually simulates fetch properly.

Example (haven't extracted it back for the PR yet): https://github.com/nelsonjchen/gtr-proxy/tree/snapshot-miniflare-replacement

@nelsonjchen
Copy link

#58

Oh someone beat me to it by 5 days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants