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

Undici MockAgent cannot intercept fetch request anymore #6952

Closed
6 tasks done
Guymestef opened this issue Nov 22, 2024 · 2 comments
Closed
6 tasks done

Undici MockAgent cannot intercept fetch request anymore #6952

Guymestef opened this issue Nov 22, 2024 · 2 comments
Labels

Comments

@Guymestef
Copy link

Guymestef commented Nov 22, 2024

Describe the bug

Since the version 2.1.5, we cannot mock anymore our http calls with undici MockAgent due to the bug fix:
vitest:
Use fetch() implementation from happy-dom - by @capricorn86 in #6879 (3c56f)

Reproduction

import { MockAgent, setGlobalDispatcher } from 'undici';

describe('Test mock fetch with undici', () => {
    it('should success', async () => {
       
       const mockAgent = new MockAgent();
       mockAgent.disableNetConnect();
       setGlobalDispatcher(mockAgent);

        const apiMockPool = mockAgent.get('https://localhost');

       apiMockPool
         .intercept({ method: 'GET', path: '/api/v1/test' })
         .reply(200, { success: true })
         .persist();

       const result = await fetch('https://localhost/api/v1/test');
       expect(result).toBeDefined();
    });

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
    Memory: 16.25 GB / 31.71 GB
  Binaries:
    Node: 23.2.0 - ~\bin\node-v23.2.0-win-x64\node.EXE
    Yarn: 4.5.0 - ~\bin\node-v23.2.0-win-x64\yarn.CMD
    npm: 10.9.0 - ~\bin\node-v23.2.0-win-x64\npm.CMD
  Browsers:
    Edge: Chromium (128.0.2739.67)
    Internet Explorer: 11.0.19041.4355
  npmPackages:
    @vitejs/plugin-react: ~4.3.3 => 4.3.3
    @vitest/coverage-v8: ~2.1.5 => 2.1.5
    vite: ~5.4.11 => 5.4.11
    vitest: ~2.1.5 => 2.1.5

Used Package Manager

yarn

Validations

@hi-ogawa
Copy link
Contributor

As this looks more like a feature of happy-dom, can you open an issue there?

On Vitest, you can probably try overwriting fetch (and other) globals back to undici by following

import { fetch } from "undici"
globalThis.fetch = fetch;

@Karthik0521

This comment was marked as spam.

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

No branches or pull requests

4 participants