Skip to content

Refactor MCP tests to use MSW instead of vi.fn() mocks #170

@ryoppippi

Description

@ryoppippi

Summary

The MCP integration tests in src/toolsets/tests/stackone.mcp-fetch.spec.ts currently use vi.fn() to mock the RpcClient.actions.rpcAction method directly. This approach should be refactored to use MSW (Mock Service Worker) for consistency with other tests in the codebase.

Current Implementation

const rpcClient = {
  actions: {
    rpcAction: vi.fn(async () => ({ actionsRpcResponse: { data: null } })),
  },
} as unknown as RpcClient;

Issues with Current Approach

  1. Type assertion (as unknown as RpcClient) - This bypasses TypeScript's type checking
  2. Inconsistent with other tests - Other tests use MSW handlers for HTTP mocking
  3. Response structure mismatch - The mock returns { actionsRpcResponse: { data: null } } which doesn't match the actual server response structure { data: null, next?: string }
  4. Test is excluded from vitest - The file is in exclude array in vitest.config.ts, so MSW setup doesn't apply

Proposed Changes

  1. Remove the test file from vitest's exclude array
  2. Replace vi.fn() mocks with MSW handlers in mocks/handlers.ts
  3. Update mock responses to match the correct RpcActionResponse type:
    // Correct response structure
    { data: Record<string, unknown> | Record<string, unknown>[] | null, next?: string }
  4. Remove the as unknown as RpcClient type assertions

Related Context

This issue was identified during the RPC client type improvements in #152, where the response type was corrected to match the server's ActionsRpcResponseApiModel.

Acceptance Criteria

  • MCP tests use MSW for HTTP mocking
  • No vi.fn() mocks for RpcClient methods
  • No type assertions for RpcClient
  • Mock responses match actual server response structure
  • All tests pass

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