Skip to content

Commit ea9fc84

Browse files
committed
ci: 修复测试 mock 泄漏与 check-dist 安装失败
- main.test.ts 改用 spyOn 替换 gensvg,避免全局 mock.module 泄漏到 integration 测试 - check-dist.yml 去掉 setup-node 的 npm 缓存(仓库只有 bun.lock),依赖改用 bun ci 安装
1 parent f4d9f62 commit ea9fc84

2 files changed

Lines changed: 21 additions & 10 deletions

File tree

‎.github/workflows/check-dist.yml‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,18 @@ jobs:
3535
uses: actions/setup-node@v6
3636
with:
3737
node-version-file: .node-version
38-
cache: npm
38+
39+
- name: Setup mise
40+
id: setup-mise
41+
uses: jdx/mise-action@v2
3942

4043
- name: Remove dist/ Directory
4144
id: remove-dist
4245
run: npx rimraf ./dist
4346

4447
- name: Install Dependencies
4548
id: install
46-
run: npm ci
49+
run: bun ci
4750

4851
- name: Build dist/ Directory
4952
id: build

‎__tests__/main.test.ts‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@
33
* Physics, network and SVG generation are mocked out; only input wiring is asserted.
44
*/
55

6-
import { afterEach, beforeEach, describe, expect, it, mock } from 'bun:test'
6+
import {
7+
afterEach,
8+
beforeEach,
9+
describe,
10+
expect,
11+
it,
12+
mock,
13+
spyOn
14+
} from 'bun:test'
715
import { mkdtempSync, readFileSync, writeFileSync } from 'node:fs'
816
import { tmpdir } from 'node:os'
917
import { join } from 'node:path'
18+
import * as gensvgModule from '../src/gensvg'
1019
import { run } from '../src/main'
1120

1221
let captured: Record<string, unknown> | null = null
1322

14-
mock.module('../src/gensvg.ts', () => ({
15-
gensvg: async (options: Record<string, unknown>) => {
16-
captured = options
17-
return '<svg />'
18-
}
19-
}))
20-
2123
/** main.ts 读取的全部输入(@actions/core 按 INPUT_<NAME> 大写读取) */
2224
const INPUT_NAMES = [
2325
'USERNAME',
@@ -48,6 +50,11 @@ let workDir = ''
4850
beforeEach(() => {
4951
for (const name of INPUT_NAMES) delete process.env[`INPUT_${name}`]
5052
captured = null
53+
// 只替换 gensvg,避免跑真实物理与网络;spyOn 可被 restore,不会泄漏到其他测试文件
54+
spyOn(gensvgModule, 'gensvg').mockImplementation(async (options) => {
55+
captured = options as Record<string, unknown>
56+
return '<svg />'
57+
})
5158
process.exitCode = 0
5259
workDir = mkdtempSync(join(tmpdir(), 'github-gravity-'))
5360
process.env.GITHUB_OUTPUT = join(workDir, 'output.txt')
@@ -60,6 +67,7 @@ beforeEach(() => {
6067

6168
afterEach(() => {
6269
for (const name of INPUT_NAMES) delete process.env[`INPUT_${name}`]
70+
mock.restore()
6371
process.exitCode = 0
6472
})
6573

0 commit comments

Comments
 (0)