viteProxy.test.ts 1.59 KB
import { describe, it, expect } from 'vitest';
import * as fs from 'fs';
import * as path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const ROOT_DIR = path.resolve(__dirname, '../../');
const VITE_CONFIG_PATH = path.join(ROOT_DIR, 'vite.config.ts');

describe('Vite proxy 配置测试', () => {
  describe('默认代理目标', () => {
    it('vite.config.ts 存在', () => {
      expect(fs.existsSync(VITE_CONFIG_PATH)).toBe(true);
    });

    it('配置包含 server.proxy', () => {
      const content = fs.readFileSync(VITE_CONFIG_PATH, 'utf-8');
      expect(content).toContain('proxy');
    });

    it('/api 代理默认目标为 http://192.168.1.191:8000/', () => {
      const content = fs.readFileSync(VITE_CONFIG_PATH, 'utf-8');
      expect(content).toContain('192.168.1.191:8000');
    });

    it('代理配置包含 changeOrigin', () => {
      const content = fs.readFileSync(VITE_CONFIG_PATH, 'utf-8');
      expect(content).toContain('changeOrigin');
    });
  });

  describe('VITE_API_PROXY_TARGET 覆盖支持', () => {
    it('配置使用环境变量覆盖 proxy target', () => {
      const content = fs.readFileSync(VITE_CONFIG_PATH, 'utf-8');
      // Should reference the env variable for override
      expect(content).toMatch(/VITE_API_PROXY_TARGET|env\.VITE_API_PROXY_TARGET/);
    });
  });

  describe('代理路径', () => {
    it('代理 /api 到后端', () => {
      const content = fs.readFileSync(VITE_CONFIG_PATH, 'utf-8');
      expect(content).toContain('\'/api\'');
    });
  });
});