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

feat: support global fetch in Node.js #1543

Merged
merged 1 commit into from
Feb 18, 2023
Merged

Conversation

kettanaito
Copy link
Member

@kettanaito kettanaito commented Feb 18, 2023

@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 8921324:

Sandbox Source
MSW React Configuration

@kettanaito kettanaito merged commit 049f21d into main Feb 18, 2023
@kettanaito kettanaito deleted the feat/support-global-fetch branch February 18, 2023 14:10
@christianvuerings
Copy link

@kettanaito Thank you for landing this change! Do you know when we would be able to create a release with it?

@kettanaito
Copy link
Member Author

@christianvuerings, MSW releases automatically but thanks for bumping this. Looks like the latest release failed after I've migrated this repo to PNPM (https://github.com/mswjs/msw/actions/runs/4238043336/jobs/7364679319). Will look into that...

@christianvuerings
Copy link

@kettanaito Great, thank you!

@kettanaito
Copy link
Member Author

Released: v1.1.0 🎉

This has been released in v1.1.0!

Make sure to always update to the latest version (npm i msw@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

@thisjeremiah
Copy link

thisjeremiah commented Mar 2, 2023

It might be worth noting that this update could require a setup change.

In my case, I am mocking a GraphQL API for Apollo Client, and upon updating to msw v1.1.0, I started getting an INVALID_URL error thrown from the msjw FetchInterceptor when I ran my tests (Node v.16.14.2), which was a bit tricky to debug.

The fix for me was to add an explicit require('node-fetch') fetch implementation when setting up Apollo Client.

But it should be noted that this was not required prior to this update (e.g. the code works without this change in v1.0.0).

@anthonyhastings
Copy link

I'm experiencing a similar issue to @thisjeremiah; I'm using Node v18 and Cypress to test a Next.js application. Bumping to MSW v1.1.0 has me experiencing the same INVALID_URL error:

Error: TypeError [ERR_INVALID_URL]: Invalid URL
    at new NodeError (node:internal/errors:399:5)
    at URL.onParseError (node:internal/url:565:9)
    at new URL (node:internal/url:645:5)
    at FetchInterceptor.<anonymous> (/var/www/nextjs-pokedex/node_modules/@mswjs/interceptors/src/interceptors/fetch/index.ts:50:26)

@kettanaito
Copy link
Member Author

@anthonyhastings, I believe I have a fix for that issue. Will be published soon.

@janpe
Copy link

janpe commented Apr 12, 2023

@anthonyhastings, I believe I have a fix for that issue. Will be published soon.

Any news on this? I'm still facing the Invalid URL error

@vojty
Copy link

vojty commented Apr 13, 2023

This PR also breaks the following test:

    it.only('should follow redirect', async () => {
      mswServer.use(
        rest.get('/example-redirect', async (req, res, ctx) => {
          return res.once(ctx.status(302), ctx.set('Location', '/redirect-target'));
        }),
        rest.get('/redirect-target', async (req, res, ctx) => {
          return res.once(ctx.status(200), ctx.json({ data: { foo: 'bar' } }));
        }),
      );
      const response = await fetch('/example-redirect', { redirect: 'follow' }).json();
      expect(response).toEqual({ data: { foo: 'bar' } });
    });

The redirect is not followed, works well in 1.0.1

@u-yas
Copy link

u-yas commented Jul 24, 2023

For those who landed here after upgrading to a version above MSW 1.1.0 and the tests stopped working.

I wrote the workaround implementation in the comment below, so please refer to it

#1563 (comment)

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

Successfully merging this pull request may close these issues.

Support Undici
7 participants