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

React JS中使用Jest与React Testing Library测试无函数包裹的Switch Case代码问题求助

解决Jest测试依赖window.location的模块问题

你的测试用例没生效的核心原因是:模块在导入时就已经执行了所有顶层代码。也就是说,当你import {CONFIGSERVICE} from '../../config/development'的时候,原代码里的window.location.origin已经被读取,LINK_ENV的计算和CONFIGSERVICE的赋值都完成了,测试里重新定义的ENV_URL、REGEX这些变量根本不会影响已经导出的CONFIGSERVICE。

下面是两种解决方案,优先推荐第一种(代码更可测、更灵活):

方案一:重构原代码,封装逻辑为可测试函数

把依赖window的逻辑抽成一个独立函数,这样既可以在生产环境用window.location.origin调用,也可以在测试时传入自定义值,彻底解耦全局变量依赖。

修改后的原代码:

const REGEX = /(-)\w+/;

// 封装成可复用、可测试的函数
const getLinkEnv = (origin) => {
  let LINK_ENV = origin.match(REGEX)?.[0];
  switch (LINK_ENV) {
    case "-dev":
      return LINK_ENV.concat('1');
    default:
      return '-prd';
  }
};

// 生产环境使用window.location.origin初始化
const ENV_URL = window.location.origin;
export const CONFIGSERVICE = {
  LINK: `http://oppo${getLinkEnv(ENV_URL)}.internal.meta.com`,
};

// 导出函数方便测试
export { getLinkEnv };

对应的测试用例:

import { CONFIGSERVICE, getLinkEnv } from '../../config/development';

// 单独测试核心逻辑函数,覆盖所有场景
test('returns "-dev1" when origin contains "-dev"', () => {
  expect(getLinkEnv('https://note-dev.meta.com')).toBe('-dev1');
});

test('returns "-prd" when origin has no matching env suffix', () => {
  expect(getLinkEnv('https://note.meta.com')).toBe('-prd');
  expect(getLinkEnv('https://note-staging.meta.com')).toBe('-prd');
});

// 验证CONFIGSERVICE的完整生成逻辑
test('CONFIGSERVICE.LINK is correct for dev environment', () => {
  // 模拟window.location.origin
  delete window.location;
  window.location = { origin: 'https://note-dev.meta.com' };
  
  // 重置模块缓存,避免之前的导入结果影响当前测试
  jest.resetModules();
  const { CONFIGSERVICE } = require('../../config/development');
  
  expect(CONFIGSERVICE.LINK).toBe("http://oppo-dev1.internal.meta.com");
});

方案二:不重构原代码,直接模拟window.location

如果你暂时不想修改原代码,可以在测试前模拟window.location.origin,但要注意模块缓存的问题,每次测试前必须重置模块:

修改后的测试用例:

test('success2', () => {
  // 1. 重置模块缓存,清除之前的导入结果
  jest.resetModules();
  
  // 2. 模拟window.location.origin为测试目标值
  delete window.location;
  window.location = { origin: 'https://note-dev.meta.com' };
  
  // 3. 重新导入模块,此时模块会使用模拟的window值初始化
  const { CONFIGSERVICE } = require('../../config/development');
  
  expect(CONFIGSERVICE.LINK).toBe("http://oppo-dev1.internal.meta.com");
});

这个方案可以快速解决当前测试问题,但原代码依然紧耦合window全局变量,后续维护和扩展的灵活性不如方案一。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 11:57:48