You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用msw(2.0.9)+Vite(5.0.2)+Vitest遇Response未定义错误求助

求助:MSW 2.0.9 + Vite 5.0.2 + Vitest 0.34.6 中 Response 未定义的问题

使用最新版msw(2.0.9)搭配Vite(5.0.2)和Vitest(0.34.6)时,始终遇到Response未定义的错误。已安装cross-fetch和@types/node,且将原本的TypeScript 5.3.2降级到满足MSW peerDependency要求的5.2.x版本后,问题依然存在。

错误信息

ReferenceError: Response is not defined
 ❯ node_modules/.pnpm/msw@2.0.9_typescript@5.3.2/node_modules/msw/lib/core/HttpResponse.mjs:5:28
 ❯ src/setupTests.ts:3:31
      1| import "@testing-library/jest-dom";
      2| import fetch from "cross-fetch";
      3| import { setupServer } from "msw/node";
       |                               ^

我的vite.config.ts配置

import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";
import { configDefaults } from "vitest/config";

export default defineConfig({
  plugins: [react()],
  server: {
    host: true,
    port: 4000,
  },
  define: {
    "process.env": {
      VERSION: process.env.VERSION,
      PLAYWRIGHT: process.env.PLAYWRIGHT,
      USE_MSW: process.env.USE_MSW,
    },
  },
  resolve: {
    mainFields: ["module"],
    alias: {
      src: "/src",
    },
  },
  test: {
    environment: "jsdom",
    globals: true,
    setupFiles: "./src/setupTests.ts",
    watch: false,
    // Need to concat with config defaults otherwise node_modules get tested
    exclude: [...configDefaults.exclude, "src/e2e/*"],
    coverage: {
      provider: "istanbul",
      exclude: [".eslintrc.cjs", "src/index.tsx", "playwright.config.ts", ".github/*"],
      all: true,
      lines: 100,
      functions: 100,
      branches: 100,
      statements: 100,
      reporter: ["json", "lcov", "text", "cobertura"],
    },
  },
});

求各位提供解决思路或可行方案。

内容的提问来源于stack exchange,提问作者danchez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 10:23:35