fix: polyfill File API for Node 18 in tests + improve CI

This commit is contained in:
gdjdkid committed 2026-03-27 11:52:58 +08:00
1 parent ca70cf7c3b
commit 1a1cd9778f
3 files changed
+44 -9

No files matched your search

+5 -9
View File
@@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
node-version: [18.x, 20.x, 22.x] # 你的项目要求 >=18,只测这三个就够
node-version: [18.x, 20.x, 22.x]
steps:
- name: Checkout code
@@ -22,19 +22,15 @@ jobs:
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm' # 加速依赖下载
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
continue-on-error: true # 先让 CI 不因为测试失败而整体失败
continue-on-error: ${{ matrix.node-version == '18.x' }} # 只在 18.x 允许失败
- name: Show test output # 可选,强制显示详细日志
- name: Show detailed test output
if: always()
run: npm test -- --reporter=verbose
# 如果你以后加了 ESLint / Prettier,可以在这里加
# - name: Lint
# run: npm run lint
run: npm test -- --reporter=verbose
+27
View File
@@ -1,3 +1,30 @@
import { beforeAll } from 'vitest';
// 为 Node 18 提供缺失的 Web API(File, FormData 等)
beforeAll(() => {
if (typeof File === 'undefined') {
global.File = class File {
constructor(bits, name, options = {}) {
this.bits = bits;
this.name = name;
this.type = options.type || '';
this.size = bits.length || 0;
}
};
}
if (typeof FormData === 'undefined') {
global.FormData = class FormData {
constructor() {
this.data = new Map();
}
append(key, value) {
this.data.set(key, value);
}
};
}
});
import { describe, it, expect, vi, beforeEach } from 'vitest';
// mock execSync,避免真实的 curl 网络请求
+12
View File
@@ -0,0 +1,12 @@
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
environment: 'node',
include: ['test/**/*.test.js'],
pool: 'forks', // 对 Node 18 更友好
deps: {
inline: ['axios', 'chalk', 'ora'],
},
},
});