Skip to content

Quick remedy for "Cannot find module 'wretch/middlewares'" ESM error when using Jest #181

@dmudro

Description

@dmudro

Jest is a pretty common part of modern app stack. importing wretch middleware in the (non spec) code fails when running the suite:

Cannot find module 'wretch/middlewares' from...

in my case it fails on retry middleware specifically. similar problems have been reported in #144 and #150.

I spent quite some time finding a relatively clean solution and thought to share a simple one that has worked best for me so far:

  1. create a mock module in <root>/__mocks/wretch/middlewares.ts
  2. ...with file content:
module.exports = {
  retry: jest.fn(),
};

(or as needed).

ultimately this issue seems to be related to experimental nature of Jest ESM imports which I do not even have enabled. or perhaps there is something that wretch can do on the export side?

if nothing else I hope the above mocks fix might help someone else.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions