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

[vitest-browser-react] vi.mock() with spy: true option breaks MSW interceptors and causes real network requests #7572

Open
6 tasks done
amanape opened this issue Feb 27, 2025 · 0 comments

Comments

@amanape
Copy link

amanape commented Feb 27, 2025

Describe the bug

When using vi.mock() with the spy: true option on a module that makes network requests, it appears to conflict with MSW interceptors. This causes the tests to bypass the MSW mock handlers and attempt real network requests, which leads to test failures.

The issue also has a global effect on the test suite:

  • Even when the vi.mock() call is scoped to a specific describe
  • Even when skipping tests that contain the vi.mock() call using describe.skip

The issue persists and breaks other tests that depend on MSW interception

Reproduction

Set up MSW to intercept network requests in tests

import { http, HttpResponse, delay } from 'msw';

export const handlers = [
  http.get('https://api.example.com/users', async () => {
    await delay();

    return HttpResponse.json([
      { id: 1, name: 'User One' },
      { id: 2, name: 'User Two' },
    ]);
  }),
];

MSW intercepts requests as expected; tests pass

import { render } from 'vitest-browser-react';
import { describe, expect, test } from 'vitest';
import { UserList } from './UserList'

describe('UserList component', () => {
  test('shows users from API', async () => {
    const { getByText } = render(<UserList />)
    
    await expect.element(getByText('User 1')).toBeInTheDocument();
    await expect.element(getByText('User 2')).toBeInTheDocument();
  })
})

MSW intercepts requests conflicted; ALL tests fail

import { render } from 'vitest-browser-react';
import { describe, expect, test } from 'vitest';
import { getUserList } from '~/utils/api-client';
import { UserList } from './UserList'

describe('UserList component', () => {
  /* ... */
})

describe('UserList component 2', () => {
  vi.mock('~/utils/api-client', { spy: true })

  test('shows users from API', async () => {
    const { getByText } = render(<UserList />)

    expect(getUserList).toHaveBeenCalled(); // passes
    
    await expect.element(getByText('User 1')).toBeInTheDocument();
    await expect.element(getByText('User 2')).toBeInTheDocument();
  })
})

System Info

System:
    OS: macOS 15.3.1
    CPU: (8) arm64 Apple M1
    Memory: 98.05 MB / 8.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.13.1 - ~/.nvm/versions/node/v22.13.1/bin/node
    npm: 10.9.2 - ~/.nvm/versions/node/v22.13.1/bin/npm
    bun: 1.2.4 - ~/.bun/bin/bun
  Browsers:
    Safari: 18.3
  npmPackages:
    @vitejs/plugin-react: ^4.3.4 => 4.3.4 
    @vitest/browser: ^3.0.6 => 3.0.6 
    playwright: ^1.50.1 => 1.50.1 
    vite: ^5.4.11 => 5.4.14 
    vitest-browser-react: ^0.1.1 => 0.1.1

Used Package Manager

npm

Validations

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

No branches or pull requests

1 participant