Skip to content

There is some problem with testing code #43

Open

Description

@Iezuni

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?

Activity

  1. koeninger commented on Jun 15, 2021

    @koeninger
    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

  2. nelsonjchen commented on Mar 6, 2022

    @nelsonjchen

    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

  3. nelsonjchen commented on Mar 6, 2022

    @nelsonjchen

    #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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions