diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cab819c..9be6f71 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 \ No newline at end of file + run: npm test -- --reporter=verbose \ No newline at end of file diff --git a/test/fetcher.test.js b/test/fetcher.test.js index cbffc8d..f68461d 100644 --- a/test/fetcher.test.js +++ b/test/fetcher.test.js @@ -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 网络请求 diff --git a/vitest.config.js b/vitest.config.js new file mode 100644 index 0000000..da364f8 --- /dev/null +++ b/vitest.config.js @@ -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'], + }, + }, +}); \ No newline at end of file