viteProxy.test.ts
1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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\'');
});
});
});